TypeScript Reference ยท Keywords and Declarations
The typeof operator (value position)
Reference for the JavaScript typeof operator in TypeScript: the possible results, how it narrows unions, and how it differs from the typeof type query.
In an expression, typeof x is the JavaScript operator that returns a string naming the kind of value at run time: "string", "number", "bigint", "boolean", "symbol", "undefined", "object" or "function". TypeScript understands comparisons against those strings and narrows the type of x inside the branch.
Do not confuse it with the type query typeof x that appears in a type position (type T = typeof config). That one is erased and yields the static type of a variable. Same keyword, different meaning, decided by where it is written.
function size(v: string | string[]): number { return typeof v === "string" ? v.length : v.length; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
typeof expression // value: returns a string typeof x === "string" // narrowing check type T = typeof x; // type query (different feature) type U = typeof import("m"); // module type query
| Result | Produced for |
|---|---|
"string", "number", "bigint", "boolean", "symbol" |
The matching primitives |
"undefined" |
undefined, and undeclared identifiers (no error) |
"object" |
Objects, arrays, null, dates, maps and similar |
"function" |
Functions and classes |
Edge cases and errors:
typeof nullis"object", a historical quirk. Testx === nullexplicitly.- Arrays are
"object"; useArray.isArray(x)to detect them. - Narrowing works for the eight result strings. Comparing to an unknown string such as
"integer"is a compile error because that result is impossible. typeof xon a variable in its temporal dead zone throws aReferenceError, unlike on an undeclared name.- Narrowing by
typeof x === "object"leavesobject | null, so check for null before using properties.
Usage notes
- A
typeofcheck is the right tool for primitives. For class instances useinstanceof; for union members that share a property useinor a discriminant. - Narrowing works on property accesses and element accesses when the reference is stable, such as
typeof obj.id === "string". - You can store the result and narrow later only in simple cases; the check must be directly visible to the compiler (aliased conditions help since TypeScript 4.4 for
constreferences). typeof x === "function"narrowsunknowntoFunction, which is loosely typed; check parameter lengths or use a type guard for stronger typing.- The type-query form accepts only identifiers and property paths, not arbitrary expressions such as
typeof f().