tsconfig Strictness Flags
9 min read
tsconfig Strictness Flags
The "strict": true shorthand in tsconfig.json is a family of individual flags. Knowing what each flag does helps you understand the errors you see — and lets you adopt them incrementally in a legacy codebase.
strictNullChecks—nullandundefinedare not assignable to other types; you must handle them explicitly.noImplicitAny— raises an error when TypeScript would inferanyfor a parameter or variable with no annotation.strictFunctionTypes— checks function parameter types contravariantly (prevents unsafe callbacks).strictBindCallApply— ensuresbind,call, andapplyare typed correctly.strictPropertyInitialization— class properties must be assigned in the constructor or have a definite-assignment assertion (!).noImplicitThis— raises an error whenthiswould be typed asanyinside a function.alwaysStrict— emits"use strict"in every output file and parses files in strict mode.useUnknownInCatchVariables— catch clause variables are typedunknowninstead ofany(TypeScript 4.4+).
ts
// tsconfig.json — enabling flags individually
// {
// "compilerOptions": {
// "strictNullChecks": true,
// "noImplicitAny": true,
// "strictFunctionTypes": true,
// "useUnknownInCatchVariables": true
// }
// }
// With strictNullChecks:
function getLength(s: string | null): number {
// return s.length; // Error — s could be null
return s?.length ?? 0; // OK
}
// With useUnknownInCatchVariables:
try {
JSON.parse("{");
} catch (err) {
// err is unknown — must narrow before use
if (err instanceof Error) {
console.error(err.message);
}
}Additional safety flags
noUncheckedIndexedAccess— accessingarr[i]orobj[key]returnsT | undefined, not justT.exactOptionalPropertyTypes— optional properties may not be explicitly set toundefined; they must be omitted.noImplicitReturns— every code path in a function must return a value if the return type is notvoid.noFallthroughCasesInSwitch— switch cases withoutbreakorreturnare flagged.noPropertyAccessFromIndexSignature— dot notation is disallowed on index-signature types; bracket notation is required.
For new projects: turn on "strict": true plus noUncheckedIndexedAccess and exactOptionalPropertyTypes from day one. For migrations: enable flags one at a time, fix errors, commit, repeat — noImplicitAny first, then strictNullChecks.
Ready to test yourself?
Practice TypeScript— quiz & coding exercises