TypeScript Reference ยท Type Operators
The typeof type operator
Reference for the typeof type operator in TypeScript: derive a type from a value, combine it with keyof, ReturnType and as const, and avoid the runtime typeof.
typeof has two lives in TypeScript. In an expression it is the JavaScript operator that returns a string such as "number" at runtime. In a type position it is the type query operator: it asks the compiler for the static type of a value and lets you reuse it, so you do not have to write the same shape twice.
The type query only works on identifiers and property paths (typeof config, typeof config.server.port). It cannot take an arbitrary expression such as typeof f(). It is erased at compile time and produces no JavaScript.
const origin = { x: 0, y: 0 }; type Point = typeof origin; // { x: number; y: number }
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
type T1 = typeof identifier; type T2 = typeof object.property.path; type T3 = typeof import("./module"); // type of a module namespace type T4 = typeof ClassName; // the constructor side, not the instance
| Part | Description |
|---|---|
identifier |
A variable, function, class, enum or imported binding that exists as a value. |
object.property.path |
A chain of property names. Element access with computed keys is not allowed. |
| Result | The type the compiler infers (or the annotation, if there is one) for that value. |
Edge cases: typeof on something that only exists as a type (an interface) is an error because there is no value to query. typeof on a class gives the constructor type, so use the class name itself for the instance type.
Usage notes
- Single source of truth. Define the value once and derive the type, so a new key in a config object automatically appears in the type.
- Inference decides the result. A
constobject literal gives widened property types (string,number). Addas constto get literal, readonly types. - Combine with
keyof.keyof typeof objis the standard way to get a union of the keys of a runtime object; seekeyof. - Functions.
ReturnType<typeof fn>andParameters<typeof fn>extract pieces of a function you cannot (or do not want to) re-describe. - Arrays to unions.
(typeof list)[number]turns areadonlyarray of literals into a union of its elements; this is an indexed access type. - Classes.
typeof Widgetis the type of the class object (statics and constructor).Widgetalone is the instance type. - Do not confuse it with runtime
typeof. Inif (typeof x === "string")it narrows the type ofx, but it is the JavaScript operator and its result is one of eight strings. Only the type-position form is the type query. - Enums.
typeof Coloris the object holding the members;keyof typeof Colorgives the member names as a union.