TypeScript 7 reference · Type Operators and Keywords

TypeScript typeof (type query) Operator

In a type position, typeof takes the type of a value or function, so you don't have to write it twice.

TypeScript 7

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

typeof value

Examples

The type of an objectTry it yourself
const config = { port: 8080, host: "localhost", debug: false };
type Config = typeof config;
const other: Config = { port: 3000, host: "0.0.0.0", debug: true };
console.log(other);

Output

{ port: 3000, host: '0.0.0.0', debug: true }
A union from an array of valuesTry it yourself
const roles = ["admin", "editor", "viewer"] as const;
type Role = (typeof roles)[number]; // "admin" | "editor" | "viewer"
const r: Role = "editor";
console.log(r, roles.length);

Output

editor 3

Was this page helpful?