TypeScript 7 reference · Type Operators and Keywords
TypeScript Type[Key] Operator
Look up the type of a property with Type['key'], or of array elements with Type[number].
TypeScript 7 Since 2.1
Syntax
Type[Key]
Examples
interface Order {
id: number;
customer: { name: string; email: string };
}
type Customer = Order["customer"];
const c: Customer = { name: "Ana", email: "[email protected]" };
console.log(c.name);
Output
Ana
const sizes = ["S", "M", "L"] as const;
type Size = (typeof sizes)[number];
const s: Size = "M";
console.log(s);
Output
M
Version history
| Version | Change |
|---|---|
| TypeScript 2.1 | Added |
Related
keyof· Produce a union of an object type's property names: keyof { a: 1; b: 2 } is 'a' | 'b'.typeof (type query)· In a type position, typeof takes the type of a value or function, so you don't have to write it twice.- TypeScript keyof, typeof and Indexed Access · tutorial