TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Function expression, which lacks return-type annotation, implicitly has an 'any' return type.

Open giancarloa opened this issue 10 years ago • 4 comments

Hi there...

I am using TS version 1.7.4...

all of a sudden I started getting the error:

Error TS7011 Function expression, which lacks return-type annotation, implicitly has an 'any' return type.

the offending line of code is:

setTimeout(() => this.alertMessage = null, 2000);

not sure why it suddenly started happening... I have been running this version for a while now... anyway... I don't understand why I am getting this error... the field "alertMessage" is defined as such:

alertMessage: string;

I guess I am wondering why the compiler is inferring "any" instead of "string"

Thank you

giancarloa avatar Apr 14 '16 20:04 giancarloa

The issue is that the type of an assignment expression is always the type of the right operand, which in this case is null (which is widened to an implicit any).

We should fix this by adding an overload to setTimeout

declare function setTimeout(handler: (...args: any[]) => any, timeout: number): number;

RyanCavanaugh avatar Apr 14 '16 20:04 RyanCavanaugh

@RyanCavanaugh ... thanks

giancarloa avatar Apr 14 '16 20:04 giancarloa

should not the return type be void? the result is ignored by the caller.

mhegazy avatar May 16 '16 18:05 mhegazy

i meant the return type of the call back, i.e.:

declare function setTimeout(handler: (...args: any[]) => void, timeout: number): number;

mhegazy avatar May 16 '16 18:05 mhegazy