1.

What is the purpose of noImplicitAny?

Answer»

Usually, when we don’t provide any type on a variable, TypeScript assumes ‘any’ type. For EXAMPLE, TypeScript compiles the following CODE, assuming the parameter ‘s’ is of any type. It WORKS as long as the caller passes a string.

function PARSE(s) {console.log(s.split(' '));}parse("Hello world"); // ["Hello", "world"]

However, the code breaks down as SOON as we pass a number or other type than a string that doesn’t have a split() method on it. For example,

function parse(s) {console.log(s.split(' ')); // [ERR]: s.split is not a function}parse(10);

noImplicitAny is a compiler option that you set in the tsconfig.json file. It forces the TypeScript compiler to raise an error whenever it infers a variable is of any type. This prevents us from accidentally causing similar errors.

Parameter 's' implicitly has an 'any' type.(7006)function parse(s) {console.log(s.split(' ')); // [ERR]: s.split is not a function}


Discussion

No Comment Found