Sub topic 8
Remove Implicit `any`
Assume you already have a TypeScript project with tsconfig.json, and you understand JavaScript functions, objects, and arrays. This chapter focuses on finding values whose types TypeScript cannot determine and replacing them with useful types.
What implicit `any` means
any is a TypeScript type that allows almost every operation. A value with type any can be called, indexed, or passed anywhere without much checking.
An implicit any occurs when TypeScript assigns any to a value without you writing any. This often happens with untyped function parameters:
function formatName(name) {
return name.trim().toUpperCase();
}TypeScript cannot infer the type of name from the function declaration. If noImplicitAny is disabled, it silently treats name as any. The function may then receive a number or null and fail at runtime.
Turn on the check in tsconfig.json:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"rootDir": "src",
"outDir": "dist",
"strict": false,
"noImplicitAny": true
},
"include": ["src"]
}Run the compiler without emitting JavaScript:
npx tsc --noEmitYou may see an error like this:
Parameter 'name' implicitly has an 'any' type.The error points to a missing contract. Add the narrowest type that describes valid input:
function formatName(name: string): string {
return name.trim().toUpperCase();
}
console.log(formatName("Ada Lovelace"));The parameter annotation says callers must provide a string. The return annotation says the function produces a string. TypeScript can infer this return type, but writing it can document an important boundary.
Fix parameters, callbacks, and empty containers
Check every compiler error rather than adding any everywhere. Annotate callback parameters when their context does not provide a type:
const labels = ["admin", "editor", "reader"];
const upperLabels = labels.map((label: string): string => {
return label.toUpperCase();
});
console.log(upperLabels);In this example, TypeScript can already infer both callback types from labels. The annotations are valid but optional. Prefer inference when it is clear. Add annotations where data enters a function or where inference fails.
Empty arrays can also lose useful information in JavaScript migration work. Declare the element type when the array will be filled later:
const scores: number[] = [];
scores.push(10);
scores.push(20);
// scores.push("thirty"); // Compiler errorFor object parameters, use an interface or type alias instead of several unrelated annotations:
interface User {
name: string;
active: boolean;
}
function describeUser(user: User): string {
return user.active ? `${user.name} is active` : `${user.name} is inactive`;
}
console.log(describeUser({ name: "Grace", active: true }));A practical triage loop
When an error appears, identify where the value came from. Then choose a type based on the real data contract. Run the compiler again. Repeating this loop gradually exposes undocumented assumptions in the JavaScript code.
Exercise
What type should you add to function double(value) { return value * 2; } if the function accepts only numeric input?
Show answer
Add value: number, and optionally add the return annotation : number: function double(value: number): number { return value * 2; }.