Understanding TypeScript strictNullChecks and Null Assignment Errors

Learn what TypeScript's strictNullChecks option means, why null assignment errors occur, and how to fix them with clear examples and best practices.

If you've started using TypeScript and enabled the strictNullChecks compiler option, you might have run into some confusing errors about null or undefined values. These errors are common when working with types that can or cannot accept null or undefined. In this article, we'll break down what strictNullChecks means, why TypeScript complains about null assignments, and how you can fix those errors to write safer, cleaner code.

The strictNullChecks setting in TypeScript changes how the compiler treats null and undefined. When strictNullChecks is off, null and undefined are considered valid values for every type, so you can assign null to a string or number without errors. However, when strictNullChecks is enabled, null and undefined are distinct types and are not assignable to other types unless explicitly allowed. This helps catch many common runtime errors related to null values but can cause assignment errors if your types don’t allow null or undefined.

typescript
let name: string = "Alice";
name = null; // Error: Type 'null' is not assignable to type 'string'.

// With strictNullChecks enabled, we must explicitly allow null:
let maybeName: string | null = "Bob";
maybeName = null; // OK

// Without strictNullChecks, this would not cause an error.

To fix null assignment errors, you need to explicitly declare if a variable or parameter can be null or undefined by using union types like string | null or number | undefined. This lets TypeScript know you expect these values and forces you to handle them properly in your code, helping avoid unexpected crashes. When reading from or writing to values that could be null, techniques like null checks, optional chaining, and type assertions come in handy. This is closely related to concepts like type guards and non-null assertion operators that help manage types more precisely.

A common mistake is to disable strictNullChecks just to avoid these errors, but that reduces the safety benefits of TypeScript. Another error is forgetting to check for null before using a variable, which can lead to runtime errors even if the compiler doesn’t complain. Also, mixing strictNullChecks with types like any or unknown without proper checks can introduce subtle bugs. Always be explicit about null and undefined in your types and practice safe null handling patterns.

In summary, strictNullChecks makes TypeScript more reliable by distinguishing null and undefined from other types. When you see null assignment errors, it means you need to be explicit about where null values are allowed. By using union types like string | null and handling possible null values carefully, you can avoid these errors. Understanding strictNullChecks will also help you work better with type guards, type narrowing, and optional chaining—key tools in writing robust TypeScript code.