Q22 of 38 · TypeScript
What does `strict: true` enable in `tsconfig.json`, and which flags matter most for test projects?
Short answer
Short answer: `strict: true` enables a group of strict type-checking flags: `strictNullChecks`, `strictFunctionTypes`, `strictPropertyInitialization`, `noImplicitAny`, `noImplicitThis`, and others. For test projects, `strictNullChecks` and `noImplicitAny` deliver the most value by eliminating silent null bugs and untyped code.
Detail
"strict": true in tsconfig is a shorthand that enables a collection of strictness flags together. Knowing what each does helps you understand what errors you'll see and why they exist.
Key flags included in strict:
- strictNullChecks:
nullandundefinedare not assignable to other types. Prevents a huge class of null-dereference bugs. - noImplicitAny: Variables and parameters without a type annotation are not silently typed as
any. Forces you to be explicit. - strictFunctionTypes: Function parameter types are checked contravariantly. Prevents subtle callback type errors.
- strictPropertyInitialization: Class properties must be initialized in the constructor or declared with definite assignment assertion (
!). - noImplicitThis: Disallows
thiswith an impliedanytype. - useUnknownInCatchVariables: Catch bindings are typed as
unknownrather thanany.
For test projects: Start with strict enabled from day one — retrofitting it onto an existing suite means touching every file. If you're migrating, use // @ts-strict-ignore per file and ratchet in.
Additional useful flags (not in strict but recommended):
noUnusedLocals/noUnusedParameters: catch stale test variablesexactOptionalPropertyTypes: distinguishundefinedfrom "property absent"noUncheckedIndexedAccess: array indexing returnsT | undefined
// EXAMPLE
// tsconfig.json for a Playwright test project
{
"compilerOptions": {
"target": "ES2022",
"module": "commonjs",
"strict": true, // all strict flags
"noUnusedLocals": true, // catch dead variables
"noUnusedParameters": true, // catch unused params
"exactOptionalPropertyTypes": true, // stricter optional props
"noUncheckedIndexedAccess": true, // arr[0] is T | undefined
"moduleResolution": "bundler",
"esModuleInterop": true,
"skipLibCheck": true, // don't check node_modules types
"baseUrl": ".",
"paths": { "@/*": ["./src/*"] }
},
"include": ["tests/**/*.ts", "fixtures/**/*.ts"]
}