strictNullChecks

2 snippets in TypeScript

TSTypeScript

null & undefined

TS · Basic Types
syntax
let varName: null = null;
let varName: undefined = undefined;
let varName: string | null = null;
example
let resetValue: null = null;
let notAssigned: undefined = undefined;

// Practical usage — nullable types
let selectedUserId: string | null = null;
selectedUserId = "usr_482";
output
// selectedUserId can hold either a string or null

Note With strictNullChecks enabled (recommended), null and undefined are NOT assignable to other types unless you explicitly include them in a union. This catches a huge class of runtime bugs.

strict Mode

TS · Configuration
syntax
// tsconfig.json
{ "compilerOptions": { "strict": true } }
example
// strict: true enables ALL of these at once:
// - strictNullChecks: null/undefined not assignable to other types
// - strictFunctionTypes: contravariant function parameter checking
// - strictBindCallApply: type-check bind, call, apply
// - strictPropertyInitialization: class props must be initialized
// - noImplicitAny: error on implicit any
// - noImplicitThis: error on 'this' with implicit any type
// - useUnknownInCatchVariables: catch variable is unknown, not any
// - alwaysStrict: emit "use strict" in every file
output
// strict: true is the recommended baseline for all new projects

Note Always start new projects with strict: true. You can selectively disable individual checks if needed (e.g., "strictPropertyInitialization": false) while keeping the rest. Enabling strict on a large existing JS-to-TS migration can produce thousands of errors — enable checks incrementally in that case.