Sub topic 7
Compiler Options That Surface `any` and `unknown`
This chapter assumes basic TypeScript syntax, union types, and familiarity with try/catch. It also assumes you can run tsc, the TypeScript compiler.
Compiler options decide how aggressively TypeScript checks your program. Two options are especially important when working with any and unknown:
noImplicitAnyreports values that becomeanywithout an explicit annotation.useUnknownInCatchVariablestypes caught errors asunknown.
These options do not remove any from TypeScript. They make unsafe type decisions visible during compilation.
Enabling the checks
A tsconfig.json file configures the compiler. The strict option enables a group of strict checks, including both options discussed here.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"noEmit": true
}
}noEmit tells tsc to check the code without creating JavaScript files. Run the check with:
npx tscYou can enable the options separately when adopting strict checking gradually:
{
"compilerOptions": {
"noImplicitAny": true,
"useUnknownInCatchVariables": true
}
}The following diagram shows the relationship between strict and the two focused options.
`noImplicitAny`: find accidental `any`
An implicit any appears when TypeScript cannot infer a type and you did not provide one. For example, this function parameter has no usable type information:
function printLength(value) {
console.log(value.length);
}With noImplicitAny: true, the compiler reports an error similar to:
Parameter 'value' implicitly has an 'any' type.Fix the error by choosing the type that matches the function’s contract:
function printLength(value: string): void {
console.log(value.length);
}If the function must accept several kinds of values, use a union or unknown instead:
function printLength(value: unknown): void {
if (typeof value === "string" || Array.isArray(value)) {
console.log(value.length);
}
}An explicit annotation such as value: any does not produce a noImplicitAny error. That annotation is a deliberate escape from checking. Use it only when you have a specific boundary or migration reason.
`useUnknownInCatchVariables`: treat errors as untrusted
JavaScript allows any value to be thrown:
throw "network failed";
throw { code: 503 };
throw new Error("network failed");Because of this, a caught value cannot safely be assumed to be an Error. With useUnknownInCatchVariables: true, this code fails:
try {
JSON.parse("{");
} catch (error) {
console.error(error.message);
}Narrow the value before reading message:
try {
JSON.parse("{");
} catch (error) {
if (error instanceof Error) {
console.error(error.message);
} else {
console.error("Unknown failure:", String(error));
}
}The option applies to catch variables that do not have an explicit type. You can write catch (error: unknown) to make the intent clear, but you usually do not need to do so when the option is enabled.
> callout note > Strict compiler options move failures from runtime to development time. Fixing each reported error is safer than adding as any until the project compiles.
Adoption and lint rules
For an existing project, enable noImplicitAny first or enable strict and fix errors by module. Avoid changing every error to any; that hides the information the compiler is trying to provide.
Lint rules can reinforce the policy. For example, ESLint’s @typescript-eslint/no-explicit-any rule reports explicit any annotations. A practical migration policy is:
| Situation | Preferred type |
|---|---|
| Value has not been validated | `unknown` |
| Value is impossible | `never` |
| Temporary legacy escape hatch | Explicit `any`, documented and limited |
Compiler options catch implicit problems. Lint rules can also limit intentional uses of any.
Exercise
Why does useUnknownInCatchVariables reject error.message until you check the caught value?
Show answer
JavaScript can throw any value, not only Error objects. The value is therefore unknown until a runtime check such as instanceof Error proves its shape.