修复打字稿中的 strictBindCallApply 相关错误
fixing strictBindCallApply related error in typescript
当我在 tsconfig.json
和 运行 ng build
中添加 strictBindCallApply:true
时,我得到以下结果:
ERROR in src/app/hot/hot.component.ts(538,61): error TS2345: Argument of type 'IArguments' is not assignable to parameter of type '[Core, HTMLTableCellElement, number, number, string | number, any, CellProperties]'.
Type 'IArguments' is missing the following properties from type '[Core, HTMLTableCellElement, number, number, string | number, any, CellProperties]': 0, 1, 2, 3, and 32 more.
相关代码段如下:
const cellValidationTempVar = this.cellValidation;
this.cellValidation[i].renderer = function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments); // HERE!
if (instance.getDataAtCell(row, col).length > cellValidationTempVar[col].length ) {
td.classList.add('htInvalid');
} else {
td.classList.remove('htInvalid');
}
}
Handsontable.renderers.TextRenderer.apply(this, arguments);
语句与 documentation 中使用的相同。
我正在使用:
- Angular 7.2
- Handsontable 7.0.0
- 打字稿 3.2.4
没有 strictBindCallApply
构建工作正常。
strictBindCallApply:true
启用对 apply
调用的严格类型检查,这就是它导致错误的原因。 arguments
的类型不是打字稿期望在 apply 调用中获得的类型。文档中的示例是一个 javascript 示例,其中没有类型检查,这就是它工作正常的原因。
您可以通过显式指定参数而不是使用 arguments object 来防止错误。这是更新后的代码:
const cellValidationTempVar = this.cellValidation;
this.cellValidation[i].renderer = function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, [instance, td, row, col, prop, value, cellProperties]); // HERE!
if (instance.getDataAtCell(row, col).length > cellValidationTempVar[col].length ) {
td.classList.add('htInvalid');
} else {
td.classList.remove('htInvalid');
}
}
这不是很优雅,但可以防止类型错误。
另一种选择是通过强制转换为 any
来指定忽略类型,这样它就像 javascript 代码一样。
Handsontable.default.renderers.TextRenderer.apply(this, arguments as any);
如果你想要类型检查,我认为第一种方法是更好的方法。
当我在 tsconfig.json
和 运行 ng build
中添加 strictBindCallApply:true
时,我得到以下结果:
ERROR in src/app/hot/hot.component.ts(538,61): error TS2345: Argument of type 'IArguments' is not assignable to parameter of type '[Core, HTMLTableCellElement, number, number, string | number, any, CellProperties]'.
Type 'IArguments' is missing the following properties from type '[Core, HTMLTableCellElement, number, number, string | number, any, CellProperties]': 0, 1, 2, 3, and 32 more.
相关代码段如下:
const cellValidationTempVar = this.cellValidation;
this.cellValidation[i].renderer = function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments); // HERE!
if (instance.getDataAtCell(row, col).length > cellValidationTempVar[col].length ) {
td.classList.add('htInvalid');
} else {
td.classList.remove('htInvalid');
}
}
Handsontable.renderers.TextRenderer.apply(this, arguments);
语句与 documentation 中使用的相同。
我正在使用:
- Angular 7.2
- Handsontable 7.0.0
- 打字稿 3.2.4
没有 strictBindCallApply
构建工作正常。
strictBindCallApply:true
启用对 apply
调用的严格类型检查,这就是它导致错误的原因。 arguments
的类型不是打字稿期望在 apply 调用中获得的类型。文档中的示例是一个 javascript 示例,其中没有类型检查,这就是它工作正常的原因。
您可以通过显式指定参数而不是使用 arguments object 来防止错误。这是更新后的代码:
const cellValidationTempVar = this.cellValidation;
this.cellValidation[i].renderer = function (instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, [instance, td, row, col, prop, value, cellProperties]); // HERE!
if (instance.getDataAtCell(row, col).length > cellValidationTempVar[col].length ) {
td.classList.add('htInvalid');
} else {
td.classList.remove('htInvalid');
}
}
这不是很优雅,但可以防止类型错误。
另一种选择是通过强制转换为 any
来指定忽略类型,这样它就像 javascript 代码一样。
Handsontable.default.renderers.TextRenderer.apply(this, arguments as any);
如果你想要类型检查,我认为第一种方法是更好的方法。