TypeScript Reference ยท Error Codes Worth Knowing
TypeScript error TS7006: Parameter implicitly has an any type
Reference for TypeScript error TS7006: why noImplicitAny reports untyped parameters, when context gives a type for free, and the best ways to annotate.
TS7006 appears when a function parameter has no type annotation and the compiler cannot infer one from context. With noImplicitAny (part of strict) it refuses to fall back silently to any, because any switches off checking for everything that touches that value. The message is Parameter 'x' implicitly has an 'any' type.
Parameters usually get a type from context: when a function expression is passed where a function type is expected (an array method callback, an event handler, a typed variable), the parameter types are inferred. A standalone function declaration has no context, so you must write the types yourself.
function double(n) { // TS7006 return n * 2; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
| Situation | What to do |
|---|---|
| Standalone function or method | Annotate every parameter: (n: number). |
| Callback with a known signature | Leave the parameter bare; it is inferred from context. |
| You really accept anything | Write unknown and narrow, rather than any. |
| Default value present | (n = 1) infers number; no annotation needed. |
| Destructured parameter | Annotate the whole pattern: ({ id }: { id: number }). |
| Generic helper | Introduce a type parameter: <T>(items: T[]). |
| Rest parameter | (...args: string[]); an unannotated rest is TS7019. |
Related messages: TS7005 and TS7034 are the same rule for variables; TS7031 is for destructured bindings.
Usage notes
- Annotate function boundaries. Parameters of exported functions are part of your API; writing them gives readers and tools the contract.
- Return types are inferred, but annotating them on public functions catches accidental changes.
- Do not add
: anyjust to quiet it. It compiles, but you lose all checking inside the function and for its callers' results. unknownis the safe "anything". It forces atypeoforinstanceofcheck before use.- JSDoc in
.jsfiles: withcheckJs,@param {number} nsupplies the type. - Migration tip: it is common to start with
noImplicitAnyoff, then enable it and fix files one at a time.
Related
- noImplicitAny
- TS7053
- strict
- Tutorial: Function types, Type inference