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.

Frequently asked questions

How does TypeScript handle strictNullChecks?
TypeScript covers this with 2 copy-ready snippets on this page. The "null & undefined" snippet in TypeScript uses `let varName: null = null;`.
Which code does the TypeScript example use?
The "null & undefined" snippet uses `let varName: null = null;`, from the Basic Types section of the TypeScript cheat sheet.
What other TypeScript snippets are shown for "strictNullChecks"?
Besides "null & undefined", this page also shows "strict Mode".
Is there anything to watch out for?
Yes. For "null & undefined": 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.