"noImplicitAny"

Reports an error when the compiler would have to silently assume the any type.

On this page

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": true

Good 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

#

Spotted a mistake or missing an example? Write to us.