Understand strict null checks compiler flag, optional property handling, and safe nullish operations in production TypeScript.
Discover how TypeScript infers types automatically and how let versus const declarations shape wide types versus narrow literal types.
Tony Hoare called the invention of null references "my billion-dollar mistake" due to endless null pointer crashes. TypeScript solves this problem using the strictNullChecks compiler flag.
strictNullChecks DoesWhen strictNullChecks: true is enabled in tsconfig.json, null and undefined are NOT assignable to other types unless explicitly included in a union:
let username: string = "Alex";
// username = null; // Error: Type 'null' is not assignable to type 'string'
let optionalEmail: string | null = null; // Explicit union required!
Adding a ? to a property or parameter marks it as optional, automatically making its type type | undefined:
interface Config {
apiKey: string;
timeout?: number; // Inferred as 'number | undefined'
}
function connect(config: Config) {
// Safe default fallback using Nullish Coalescing (??)
const timeoutMs = config.timeout ?? 5000;
console.log(`Connecting with timeout: ${timeoutMs}ms`);
}
type UserResponse = {
profile?: {
avatarUrl?: string;
};
};
function getAvatar(res: UserResponse): string {
// Safe optional chaining ?.
return res.profile?.avatarUrl ?? "/default-avatar.png";
}
strictNullChecks: Setting strictNullChecks: false turns off null checking globally, re-introducing silent runtime TypeError: Cannot read properties of undefined crashes. Always keep strictNullChecks: true enabled!Keep strict null checks enabled across all projects. Next, let's enter Phase 2 and master Functions, Overloads, and Narrowing!