TypeScript abstract method using lambda / arrow function

I am using TypeScript 1.6 and would like to create an abstract class with an abstract method, but use the lambda / arrow function in a specific class.

Is it possible? The code shown below does not compile as it says

The Base class defines the def member function, but the Concrete extended class defines it as an instance member property "...

abstract class Base { abstract abc(): void; abstract def(): void; } class Concrete extends Base { private setting: boolean; public abc(): void { this.setting = true; } public def = (): void => { this.setting = false; } } 
+6
source share
2 answers

My understanding of Typescript specs is that when you declare

 public def = (): void => { this.setting = false; } 

In fact, you are declaring a property called def , not a method in the Base class.

Properties cannot (unfortunately IMHO) abstract in Typescript: https://github.com/Microsoft/TypeScript/issues/4669

+2
source

You can do this starting with typescript 2.0. To do this, you will need to declare an arrow function type

 type defFuntion = () => void; 

then declare

 abstract class Base { abstract abc(): void; abstract readonly def: defFuntion; } 

here is a link for this function

0
source

All Articles