Skip to content
Tutorials Center
My learning

Topics

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.

TypeScript
function size(v: string | string[]): number {
  return typeof v === "string" ? v.length : v.length;
}

Try it

TypeScript Every typeof result

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.

TypeScript Narrowing with typeof

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

TypeScript
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 null is "object", a historical quirk. Test x === null explicitly.
  • Arrays are "object"; use Array.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 x on a variable in its temporal dead zone throws a ReferenceError, unlike on an undeclared name.
  • Narrowing by typeof x === "object" leaves object | null, so check for null before using properties.

Usage notes

  • A typeof check is the right tool for primitives. For class instances use instanceof; for union members that share a property use in or 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 const references).
  • typeof x === "function" narrows unknown to Function, 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().