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
Syntax
typeof value
Examples
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 }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
Related
keyof· Produce a union of an object type's property names: keyof { a: 1; b: 2 } is 'a' | 'b'.as const· Make a value deeply read-only with literal types: strings stay 'admin' instead of string, arrays become readonly tuples.ReturnType<Type>· Get the return type of a function type, so other code can reuse it without repeating it.- TypeScript keyof, typeof and Indexed Access · tutorial