Careeroza
Strict Flags
What strict Turns On
"strict": true is a shortcut that enables a family of checks. Always start new projects with it.
| Flag | What it catches |
|---|---|
strictNullChecks | null/undefined not allowed unless in the type |
noImplicitAny | parameters and variables that silently become any |
strictFunctionTypes | unsafe function parameter assignments (contravariance) |
strictPropertyInitialization | class properties never assigned |
strictBindCallApply | wrong arguments to bind/call/apply |
useUnknownInCatchVariables | catch (e) is unknown, not any |
noImplicitThis, alwaysStrict | this of type any; emits "use strict" |
The Same Code Without strict
Example
With strict: false this compiles: find may return undefined, but nothing forces a check, so it crashes at runtime. With strict: true the compiler would report TS18048.
const users = [{ id: 1, name: "Asha" }];
function nameOf(id: number) {
const user = users.find((u) => u.id === id);
return user.name.toUpperCase();
}
console.log(nameOf(1));
console.log(nameOf(2));Output
ASHA Uncaught TypeError: Cannot read properties of undefined (reading 'name')
Turning Flags On Gradually
Tip: Migrating an old codebase? You can enable "strict": true and temporarily switch off one flag ("strictNullChecks": false), fix files module by module, then remove the override.