Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override a property in typescript?

Tags:

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. The isRowSelectable property on IGridOptions is defined as an optional boolean but the documentation says it's a function (and it is). 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:

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?

like image 930
icfantv Avatar asked Jun 08 '16 15:06

icfantv


People also ask

How do I override a function in TypeScript?

To override a class method in TypeScript, extend from the parent class and define a method with the same name. Note that the types of the parameters and the return type of the method have to be compatible with the parent's implementation. Copied! class Parent { doMath(a: number, b: number): number { console.

How do I get all properties optional TypeScript?

Use the Partial utility type to make all of the properties in a type optional, e.g. const emp: Partial<Employee> = {}; . The Partial utility type constructs a new type with all properties of the provided type set to optional. Copied!

Can we have an interface with optional properties in TypeScript?

We can define optional properties on an interface using ? and read-only properties by using the readonly keyword in the property name. The TypeScript compiler also checks for excess properties on an object and gives an error if an object contains a property that is defined in the interface.


2 Answers

You can use the utility type Omit: https://www.typescriptlang.org/docs/handbook/utility-types.html#omittk

interface ISelectionGridOptions extends Omit<IGridOptions, 'isRowSelectable'> {
  isRowSelectable: (row: IGridRowOf<Profile>) => boolean;
}
like image 171
Diego Blattner Avatar answered Nov 03 '22 00:11

Diego Blattner


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:

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.

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

like image 32
mk. Avatar answered Nov 02 '22 22:11

mk.