Sub topic 3
Narrowing with `typeof`
This chapter assumes basic TypeScript syntax, union types, and JavaScript control flow. You should be comfortable with types such as string | number and with if statements.
Why `typeof` narrows a type
A union type allows a value to have more than one possible type. That flexibility means TypeScript cannot always allow every operation:
function format(value: string | number): string {
return value.toUpperCase(); // Error: number has no toUpperCase method
}A typeof check tests the runtime type of a value. TypeScript uses the result of that test to narrow the value inside each control-flow branch.
function format(value: string | number): string {
if (typeof value === "string") {
return value.toUpperCase();
}
return value.toFixed(2);
}Inside the if block, value is known to be a string. After the early return, the only remaining union member is number, so toFixed is safe.
The supported `typeof` results
JavaScript's typeof operator returns a string. TypeScript understands these common checks:
| Check | Narrowed type |
|---|---|
| `typeof value === "string"` | `string` |
| `typeof value === "number"` | `number` |
| `typeof value === "boolean"` | `boolean` |
| `typeof value === "bigint"` | `bigint` |
| `typeof value === "symbol"` | `symbol` |
| `typeof value === "function"` | function values |
| `typeof value === "object"` | object values, including `null` |
You can also use the inequality form, such as typeof value !== "string". TypeScript then removes string from the possible types on that path.
function describe(value: string | number | boolean): string {
if (typeof value !== "number") {
return `Non-number: ${String(value)}`;
}
return `Number: ${value.toFixed(2)}`;
}How control flow affects the type
TypeScript follows branches, returns, and other control-flow statements. A check can make a value narrower in one branch and wider again after the branches join.
function printLength(value: string | string[] | null): string {
if (value === null) {
return "No value";
}
if (typeof value === "string") {
return `String length: ${value.length}`;
}
return `Array length: ${value.length}`;
}The first check removes null. The second check distinguishes string from string[].
Common pitfalls
A typeof check describes broad JavaScript categories, not every TypeScript type.
function inspect(value: object | null): string {
if (typeof value === "object" && value !== null) {
return "A non-null object";
}
return "Null";
}The expression typeof null is "object", so checking only typeof value === "object" does not exclude null.
Arrays also produce "object":
const result = typeof [1, 2, 3]; // "object"Use Array.isArray(value) when you need to distinguish an array from other objects. For class instances, use instanceof. For objects with different properties, use the in operator or a discriminant property.
Choosing a check
Use typeof when the union contains primitive types or functions. Keep the check close to the operation it enables. If a branch becomes complicated, return early or move the check into a dedicated type guard.
Exercise
What is the type of value in the final return statement? function render(value: string | number | boolean): string { if (typeof value === "string") { return value.toUpperCase(); } if (typeof value === "boolean") { return value ? "yes" : "no"; } return value.toFixed(1); }
Show answer
It is number. The string and boolean branches return early, so number is the only remaining member of the union.