简体   繁体   English

如何覆盖打字稿中的属性?

[英]How do I override a property in typescript?

This may not be possible due to current language limitations, but I'm using the latest TS (1.8.10) and am running into an issue with the ui-grid typings.由于当前的语言限制,这可能是不可能的,但我使用的是最新的 TS (1.8.10),并且遇到了 ui-grid 类型的问题。 The isRowSelectable property on IGridOptions is defined as an optional boolean but the documentation says it's a function (and it is). IGridOptions上的isRowSelectable属性被定义为一个可选的布尔值,但文档说它是一个函数(确实如此)。 I am trying to override the boolean property to be a function that returns a boolean.我正在尝试将布尔属性覆盖为返回布尔值的函数。

Normally, I just extend the typing interface and do what I need, but that's not working in this case.通常,我只是扩展打字界面并做我需要的事情,但在这种情况下这不起作用。 Here's what I have:这是我所拥有的:

interface ISelectionGridOptions extends IGridOptions {
  isRowSelectable: (row: IGridRowOf<Profile>) => boolean;
}

Where the relevant field in IGridOptions is: IGridOptions中的相关字段是:

export interface IGridOptions {
  ...
  isRowSelectable?: boolean
  ...
}

And the error I'm getting is:我得到的错误是:

(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'.

Short of fixing the core typings definitions, is there a way to fix this in my code?没有修复核心类型定义,有没有办法在我的代码中解决这个问题?

You can use the utility type Omit : https://www.typescriptlang.org/docs/handbook/utility-types.html#omittk您可以使用实用程序类型Omithttps ://www.typescriptlang.org/docs/handbook/utility-types.html#omittk

interface ISelectionGridOptions extends Omit<IGridOptions, 'isRowSelectable'> {
  isRowSelectable: (row: IGridRowOf<Profile>) => boolean;
}

If the type definitions are incorrect, you can't use overriding in order to fix them - the type system correctly treats this as an error.如果类型定义不正确,则不能使用覆盖来修复它们 - 类型系统正确地将其视为错误。 In a typed language, it's a good thing that subclasses are prevented from changing type signatures.在类型化语言中,防止子类更改类型签名是一件好事。 The problem here is the broken type definition.这里的问题是损坏的类型定义。 If it's not practical to fix the def, and the issue occurs in only a few places in your code, you could use a cast to any to disable typechecking until the defs are fixed:如果修复 def 不切实际,并且问题仅出现在代码中的几个地方,则可以使用强制转换为any来禁用类型检查,直到修复 defs:

var foo = <boolean> (<any>bar).isRowSelectable(args);
bar.isRowSelectable = <any>foo; 

Just leave a comment explaining what's going on.只需发表评论,解释发生了什么。 It is, of course, best to fix the type defs.当然,最好修复类型 defs。

Note: It breaks, like the error says, because a function isn't assignable to boolean.注意:它会中断,就像错误所说的那样,因为函数不能分配给布尔值。 The function peg is failing to fit into the boolean hole, not vice versa函数 peg 无法装入布尔孔,反之亦然

Update for TS 3.5+ TS 3.5+ 的更新

You can add this utility type to your project:您可以将此实用程序类型添加到您的项目中:

type Modify<T, R> = Omit<T, keyof R> & R;

And then use it like this:然后像这样使用它:

type ISelectionGridOptions = Modify<IGridOptions, {
  isRowSelectable: (row: IGridRowOf<Profile>) => boolean;
}>

Demo in TS Playground TS Playground 中的演示

Further Reading延伸阅读

You can use the Merge utility from the excellent type-fest package to override or overwrite any property on a type, regardless of it's original type:您可以使用优秀的type-fest包中的Merge实用程序来覆盖或覆盖类型上的任何属性,无论它是原始类型:

import type {Modify} from 'type-fest'

type SelectionGridOptions = Modify<GridOptions, {
  isRowSelectable: (row: GridRow<Profile>) => boolean;
}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM