Strict Mode and noImplicitAny
Strict checking is one of the highest-value choices in a new codebase. strictNullChecks makes null and undefined distinct concerns; noImplicitAny flags values whose types would otherwise silently become any; strictFunctionTypes tightens function parameter compatibility in relevant positions.
Enable strictness early, then fix errors at their source. Do not silence every error with any or as SomeType: doing so removes the evidence the compiler is providing. For legacy migrations, enable checks incrementally and track unsafe areas explicitly.
Handle an optional value
function getDisplayName(name: string | undefined): string {
if (name === undefined) {
return "Guest";
}
return name.toUpperCase();
}💡 The guard narrows
name from string | undefined to string in the remaining branch, making the call safe under strict null checking.Key Takeaways
- Prefer strict mode for new projects.
- Narrow nullable values rather than hiding the error.
- Use
unknownat uncertain boundaries instead of defaulting toany.
Knowledge Check
With `strictNullChecks` enabled, what is true of `string | undefined`?