Typedef in TypeScript error: 'expected variableDeclarator'

Running tslint in 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 can I improve this, so I do not see the tslint error?

+4
source share
3 answers

You must explicitly add a type declaration to your variable.

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

You said it looks pretty impenetrable. Well, yes, anonymous types make TS code worse, especially when they are huge. In this case, you can declare the called interface, for example:

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

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

You can check if tslint allows (I can't check it right now) to discard the type declaration in the function definition when using the interface

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

export var getCode: CodeGetter = function(param) { ... }
+7
source

. , tsc . , ?

tslint:

typedef . :

"callSignature" checks return type of functions
"indexSignature" checks index type specifier of indexers
"parameter" checks type specifier of parameters
"propertySignature" checks return types of interface properties
"variableDeclarator" checks variable declarations
"memberVariableDeclarator" checks member variable declarations
+2

Add typedef to getCode:

var getCode: (s: string) => string;

Inline, it should look like this:

export var getCode: (s: string) => string = function (param) {
    // ...
};
+1
source

All Articles