Sub topic 10
Implicit `any` and Untyped Parameters
This chapter assumes you know basic JavaScript functions and TypeScript annotations, and you can run tsc from a terminal. You will learn why TypeScript reports TS7006, how contextual typing affects parameters, and how to fix the error without hiding useful type information.
What “implicit any” means
A parameter has an implicit type when you do not write its type and TypeScript cannot infer one from context. If the compiler gives that parameter the any type automatically, it is an implicit any.
The any type allows almost every operation. That flexibility removes type safety, so strict TypeScript projects usually reject it. Consider this function:
function greet(name) {
return `Hello, ${name.toUpperCase()}`;
}With noImplicitAny enabled, the compiler reports:
error TS7006: Parameter 'name' implicitly has an 'any' type.The parameter needs an explicit type:
function greet(name: string): string {
return `Hello, ${name.toUpperCase()}`;
}
console.log(greet("Ada"));The return annotation is optional here because TypeScript can infer string, but it makes the function contract clearer.
Why some untyped parameters do not fail
TypeScript can infer a parameter type when the function is used in a typed context. This is called contextual typing. The array tells TypeScript that each callback argument is a number:
const scores: number[] = [80, 92, 75];
const passing = scores.filter((score) => score >= 60);
console.log(passing);The parameter score does not need an annotation because filter supplies its context. By contrast, a standalone function has no surrounding type to guide inference:
function isLong(value) {
return value.length > 10;
}Annotate it at the boundary where the value enters the function:
function isLong(value: string): boolean {
return value.length > 10;
}Configure the check
noImplicitAny is included when strict is true. You can enable it directly or use the full strict family:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"noEmit": true
},
"include": ["src"]
}Run the check with:
npx tsc --noEmit --pretty falseIf your project cannot enable all strict checks yet, setting "noImplicitAny": true isolates this particular rule. Treat that as a migration step, not a reason to annotate every parameter as any.
Choosing the right fix
Use the narrowest useful type:
- Use
string,number, orbooleanfor a single known value. - Use an object type or interface when a parameter has required properties.
- Use a union when several specific shapes are valid.
- Use
unknownfor genuinely untrusted input, then narrow it before using it. - Use
anyonly when a boundary is truly dynamic and the loss of checking is intentional.
For example, unknown forces validation:
function printId(id: unknown): void {
if (typeof id === "string" || typeof id === "number") {
console.log(id);
}
}
printId("user-42");Exercise
What should you add to function double(value) { return value * 2; } to fix TS7006 when the function accepts numeric values?
Show answer
Add value: number. The function can then be written as function double(value: number): number { return value * 2; }.