TypeScripttsconfig options
"noImplicitAny"
Reports an error when the compiler would have to silently assume the any type.
Example
#TypeScript
function totalPoints(scores) {
// Error: Parameter 'scores' implicitly has an 'any' type.
return scores.reduce((a, b) => a + b, 0)
}
function totalPointsTyped(scores: number[]) {
return scores.reduce((a, b) => a + b, 0)
}
console.log(totalPointsTyped([10, 20, 30]))Result
60
Definition and usage
#With noImplicitAny on, the compiler doesn’t let you leave a parameter or variable without a type if it can’t be inferred. Without it, such places implicitly get the type any and stop being checked.
The option is part of strict mode. In practice it means every function parameter must have a type, unless it follows from context, for example in a callback passed to map().
Deliberately using any through an explicit annotation is still allowed. The option only catches cases where any appeared by oversight.
Syntax
#Syntax
"noImplicitAny": trueGood practices
#- In callbacks, the parameter type usually follows from context, so you don’t need to write it.
- If you truly need
any, write it explicitly. It will be visible in code review.
Related entries
#- "strict"Turns on a set of the most important, rigorous compiler checks.
- anyThe type that turns off checking: the value can be anything.
- unknownThe safe counterpart to any: a value of an unknown type that you must check before use.
- Function typesTyping parameters, the return value, and functions themselves as values.
Spotted a mistake or missing an example? Write to us.