由于当前的语言限制,这可能无法实现,但我使用的是最新的 TS (1.8.10) 并且遇到了 ui-grid 类型的问题。该isRowSelectable物业上IGridOptions被定义为一个可选的布尔但文件说,这是一个功能(它是)。我试图将布尔属性覆盖为返回布尔值的函数。
通常,我只是扩展打字界面并执行我需要的操作,但这在这种情况下不起作用。这是我所拥有的:
interface ISelectionGridOptions extends IGridOptions {
isRowSelectable: (row: IGridRowOf<Profile>) => boolean;
}
Run Code Online (Sandbox Code Playgroud)
中的相关字段IGridOptions是:
export interface IGridOptions {
...
isRowSelectable?: boolean
...
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
(41,11): error TS2430: Interface 'ISelectionGridOptions' incorrectly extends interface 'IGridOptionsOf<any>'.
Types of property 'isRowSelectable' are incompatible.
Type '(row: IGridRowOf<Profile>) => boolean' is not assignable to type 'boolean'.
Run Code Online (Sandbox Code Playgroud)
缺少修复核心类型定义,有没有办法在我的代码中解决这个问题?
小智 14
您可以使用实用程序类型Omit:https :
//www.typescriptlang.org/docs/handbook/utility-types.html#omitk
interface ISelectionGridOptions extends Omit<IGridOptions, 'isRowSelectable'> {
isRowSelectable: (row: IGridRowOf<Profile>) => boolean;
}
Run Code Online (Sandbox Code Playgroud)
如果类型定义不正确,则不能使用覆盖来修复它们 - 类型系统正确地将其视为错误。在类型化语言中,防止子类更改类型签名是一件好事。这里的问题是损坏的类型定义。如果修复 def 不切实际,并且问题仅出现在代码中的几个地方,您可以使用any强制转换来禁用类型检查,直到 defs 被修复:
var foo = <boolean> (<any>bar).isRowSelectable(args);
bar.isRowSelectable = <any>foo;
Run Code Online (Sandbox Code Playgroud)
只需发表评论解释发生了什么。当然,最好修复类型定义。
注意:它会中断,就像错误所说的那样,因为函数不能分配给布尔值。函数 peg 无法放入布尔孔中,反之亦然
您可以将此实用程序类型添加到您的项目中:
type Modify<T, R> = Omit<T, keyof R> & R;
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
type ISelectionGridOptions = Modify<IGridOptions, {
isRowSelectable: (row: IGridRowOf<Profile>) => boolean;
}>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16905 次 |
| 最近记录: |