Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 'noImplicitAny' not apply to the return value of the function?

Tags:

typescript

I set the value of 'noImplicitAny' true. I thought that 'any' should be applied to the return value of a function, but it was not actually. No errors occurred.

Why doesn't noImplicitAny apply to the return value of the function?

From tsconfig.json:

{
     "noImplicitAny": true 
}

Not Any noImplicitAny errors be raised on function below.

function abc () {
    return '1'
}

Should it be like this?

function abc (): any {
    return '1'
}

Thanks.

like image 515
delbert park Avatar asked Oct 16 '25 19:10

delbert park


1 Answers

The compiler tries to infer as much as it can. noImplictAny will only cause an error if the compiler can't infer the type of something, it will not force you to specify all type annotations.

In your case, the compiler can easily infer the return type of the function as number and it does so (if you hover over the function in your ide you should see the return as number)

like image 86
Titian Cernicova-Dragomir Avatar answered Oct 18 '25 10:10

Titian Cernicova-Dragomir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!