The compiler and runtime
Enable strict checking
Turn on the family of strict compiler checks and fix the contracts they reveal instead of weakening the project globally.
Start with:
{
"compilerOptions": {
"strict": true
}
}
strict enables a family of stronger checks. These include implicit-any, null-related, function, property-initialization, and other checks.
For example, strict checking rejects an untyped parameter:
function uppercase(value) {
return value.toUpperCase()
}
It also rejects a possibly missing value before property access.
Fix the contract instead of turning strictness off globally. Add the real parameter type, narrow nullable values, and validate uncertain data.
When migrating an existing project, enable strict checks in a controlled sequence if necessary. Keep a list of temporary exceptions and remove them. New projects should begin strict.
TypeScript can add stricter behavior under this family in future versions. Read upgrade notes and run the checker before updating the compiler in production code.
Exercise: enable strict, add one untyped parameter and one possibly undefined value, then fix both without any, !, or assertions.
Lesson completed