undefined vs null
undefined means a value was never set. null means a value was deliberately emptied. Under strictNullChecks, neither is assignable to an ordinary type unless you write it into the union.
Detailed Explanation
JavaScript has two empty values and a long history of using them interchangeably. TypeScript does not invent a third. It types the two that already exist, and then — if you turn on strictNullChecks — it stops pretending they are members of every other type.
Without that flag, string includes null and undefined. user.name.toUpperCase() type-checks even when name might be missing, which is how a large class of production crashes gets past the compiler. With the flag on, string means a string. Absence is a separate type you have to mention: string | undefined, string | null, or string | null | undefined.
The usual convention in TypeScript code is:
undefinedfor "optional" — a property that may be omitted, a function parameter with a default, a map lookup that missed, an uninitialized variable.nullfor "intentionally empty" — a database column that is nullable, a JSON field the server sends asnull, a DOM API that returnsnullwhen a node is missing (document.getElementById).
That convention is social, not enforced. T | null and T | undefined are different types. A function that accepts string | undefined will reject null, and the reverse. == null is the one comparison that catches both, because of JavaScript's loose equality. === null and === undefined do not.
Optional properties (name?: string) are close to name: string | undefined but not identical. An optional property may be omitted from the object. A required property typed string | undefined must be present, even if its value is undefined. exactOptionalPropertyTypes makes that distinction strict: { name?: string } then rejects { name: undefined } unless you also add | undefined.
DOM and Node typings use null heavily. JSON uses null because JSON has no undefined. Optional function parameters use undefined. Mixing the two in one codebase is the usual source of "Argument of type 'null' is not assignable to parameter of type 'string | undefined'" errors. Pick one empty value per boundary and convert at the edge.
Neither null nor undefined exists as a runtime check added by TypeScript. The compiler only forces you to write the JavaScript that already distinguishes them. ?., ??, and explicit === checks are how you narrow. A non-null assertion (value!) tells the compiler to pretend the empty case cannot happen. It does not make that true.
Code Examples
1. The two empty values are not interchangeable.
// 1. The two empty values are not interchangeable.
function greet(name: string | undefined): string {
return name ?? "guest";
}
greet("Ada");
greet(undefined);
// greet(null);
// Error: Argument of type 'null' is not assignable to parameter of type 'string | undefined'.2. Optional property vs explicit undefined.
// 2. Optional property vs explicit undefined.
type User = { nickname?: string };
type Row = { nickname: string | undefined };
const omitted: User = {}; // fine
const present: Row = { nickname: undefined }; // fine
// const missing: Row = {};
// Error: Property 'nickname' is missing in type '{}'.
function label(user: User): string {
return user.nickname?.toUpperCase() ?? "none";
}3. DOM APIs return null; convert at the boundary.
// 3. DOM APIs return null; convert at the boundary.
function inputValue(id: string): string | undefined {
const el = document.getElementById(id); // HTMLElement | null
if (el === null || !(el instanceof HTMLInputElement)) {
return undefined;
}
return el.value;
}
const value = inputValue("email");
if (value !== undefined) {
console.log(value.length);
}Common Gotchas
`== null` is not the same as `=== null`
`value == null` is true for both `null` and `undefined`. `value === null` leaves `undefined` unhandled, and the compiler will still treat the value as possibly `undefined`.
JSON has `null` and no `undefined`
`JSON.stringify({ a: undefined })` drops the key. `JSON.stringify({ a: null })` keeps it. If a server field is nullable, type it `T | null`, not `T | undefined`.
`strictNullChecks` is the flag that makes this distinction real
without it, both empty values assign to everything and the rest of this page is unenforced.
Optional parameters default to `undefined`, not `null`
`function f(x?: string)` receives `undefined` when the argument is omitted. Passing `null` is a type error.