Sub topic 9
Containing `any`: Lint Rules and Migration Strategy
This chapter assumes you know TypeScript annotations, union types, unknown, narrowing, and basic ESLint configuration. The goal is not to remove every any immediately. The goal is to stop unexamined any values from spreading through the program.
Why `any` spreads
any disables most type checking for a value. It also weakens nearby code because operations involving any often produce more any values.
declare const input: any;
const user = input.user;
const name = user.name;
const upper = name.toUpperCase();The compiler accepts this chain even when input.user or user.name does not exist at runtime. A single any from a legacy module, unsafe library declaration, or type assertion can therefore hide several errors.
TypeScript distinguishes explicit any from implicit any. Explicit any appears in source code. Implicit any usually occurs when the compiler cannot infer a type for a parameter or variable.
| Problem | Typical source | First response |
|---|---|---|
| Explicit `any` | Annotation or assertion | Replace with a precise type or `unknown` |
| Implicit `any` | Missing annotation or incomplete inference | Enable `noImplicitAny` |
| Unsafe use | Member access, call, or return from `any` | Enable type-aware ESLint rules |
| External `any` | Dependency declaration or generated code | Isolate the boundary |
Lint rules that contain `any`
The TypeScript ESLint project provides rules that understand TypeScript types. The most direct rule is @typescript-eslint/no-explicit-any. It reports declarations such as let value: any, but it does not find every unsafe operation caused by an imported or inferred any.
The no-unsafe-* rules detect how any is used:
no-unsafe-assignmentreports assigninganyto a typed variable.no-unsafe-member-accessreports reading properties fromany.no-unsafe-callreports calling ananyvalue.no-unsafe-argumentreports passinganyinto a typed function.no-unsafe-returnreports returninganyfrom a function.
These rules require type-aware linting. ESLint must know which tsconfig describes the files it checks.
// eslint.config.mjs
import tseslint from "@typescript-eslint/eslint-plugin";
import parser from "@typescript-eslint/parser";
export default [
{
files: ["**/*.ts", "**/*.tsx"],
languageOptions: {
parser,
parserOptions: {
project: "./tsconfig.json",
tsconfigRootDir: import.meta.dirname,
},
},
plugins: {
"@typescript-eslint": tseslint,
},
rules: {
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-unsafe-assignment": "error",
"@typescript-eslint/no-unsafe-member-access": "error",
"@typescript-eslint/no-unsafe-call": "error",
"@typescript-eslint/no-unsafe-argument": "error",
"@typescript-eslint/no-unsafe-return": "error",
},
},
];The exact configuration differs between ESLint versions. Keep generated files, migrations, and vendor code in separate lint overrides rather than weakening rules for the whole project.
A staged migration
A large codebase should migrate in stages.
- Measure the problem. Run the explicit-
anyrule and record locations by package or team. Fix production boundaries before low-risk test utilities. - Stop new implicit
any. EnablenoImplicitAnythrough"strict": true, or enable it separately intsconfig.json. - Protect boundaries. Type JSON, API responses, message payloads, and plugin values as
unknown. Validate them once at the boundary. - Replace internal
any. Use a domain type when the shape is known. Use a generic when a function preserves a caller's type. Useunknownwhen the value is truly not known yet. - Contain unavoidable cases. Keep a narrowly scoped
anyin one adapter, add a comment explaining why, and expose a typed interface to the rest of the application. - Prevent regression. Run ESLint and
tsc --noEmitin continuous integration.
A useful rule is to make unsafe code shallow. One adapter may contain a cast or an any, but callers should receive validated, typed data.
flowchart LR
A[External value] --> B[unknown boundary]
B --> C[Runtime validation]
C --> D[Typed application data]
E[Legacy any] --> F[Small adapter]
F --> Dcaption: Contain uncertain values at the boundary alt: External values enter as unknown and become typed after validation; legacy any is isolated in a small adapter.
Choosing the replacement
Ask what is known about the value.
- Use a specific interface or type when the shape is known.
- Use
unknownwhen the value may have any shape and must be checked. - Use a generic type parameter when the function should preserve type information.
- Keep
anyonly when interoperability makes checking impossible, and isolate it.
function identity<T>(value: T): T {
return value;
}
function logUnknown(value: unknown): void {
console.log(String(value));
}
// The any value is contained in one adapter.
function legacyResult(value: unknown): string {
return (value as any).label ?? "unlabeled";
}The final function still has a risk: the assertion does not validate label. Prefer a runtime check when the value crosses a trust boundary. A temporary adapter can be acceptable during migration, but it should have a removal issue and a small surface area.
Exercise
Which replacement fits a function that receives an API response whose shape has not yet been validated: a domain interface, unknown, or any?
Show answer
Use unknown, then validate and narrow it before application code reads its properties.