Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typedef in TypeScript error: 'expected variableDeclarator'

Running tslint on my code I get this error:

expected variableDeclarator: 'getCode' to have a typedef.

for a TypeScript file:

export var getCode = function (param: string): string {
    // ...
};

How do I improve this so I don't see the tslint error?

like image 771
homaxto Avatar asked Oct 30 '14 13:10

homaxto


1 Answers

You have to explicitely add a type declaration to your variable.

export var getCode : (param: string) => string = function (param: string): string { 
    //...
}

You said this looks pretty unreadable. Well, yes, anonymous types makes TS code look worse, especially when they are huge. In that case, you can declare a callable interface, like this:

export interface CodeGetter {
    (param: string): string;
}

export var getCode: CodeGetter = function(param: string): string { ... }

You can check whether tslint allows you (I can't check it right now) to drop type declaration in definition of the function when using the interface

export interface CodeGetter {
    (param: string): string;
}

export var getCode: CodeGetter = function(param) { ... }
like image 177
Kuba Jagoda Avatar answered Oct 11 '22 15:10

Kuba Jagoda