HenrijsS
HenrijsS

Reputation: 674

TypeScript doesn't throw errors on later initialized ANY variable

I have a function which only accepts a number variable.

function add(n1: number) {
    return n1 + n1;
}

And I initialize an any variable and assign it a string of '5'

let number1;
number1 = '5';

I was wondering, why doesn't it show an error when I want to pass a string to the function?

console.log(add(number1));

(Of course it outputs 55, because the string is concatenated.)

Upvotes: 2

Views: 334

Answers (1)

kol
kol

Reputation: 28688

If the type is not specified (implicit any) or set to any, the uses of the variable won't be type-checked. This is useful if you gradually migrate JavaScript code to TypeScript.

If you want to flag implicit any's (like the one in your example) as error, use the noImplicitAny compiler flag.

Upvotes: 2

Related Questions