On this page
keyof
Prev: `void` | Up: builtins | Next: `typeof`
Rel: builtins, Structural Typing & Narrowing
Ref: TypeScript Docs — keyof
Tags: #public #web #typescript #builtins #tier/core
Syntax & Overview
type UserKeys = keyof User; // 'id' | 'email'
Produces a union of all public property keys (string, number, or symbol) of a type.
Notes & Pitfalls
- Produces the union of an object type's property keys: Pair with indexed access
T[K]for type-safe property getters.
Example
function getProp<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const port = getProp({ host: "localhost", port: 8080 }, "port"); // inferred as number
Linked from
Related notes
- tuts/typescript/builtins/typeof Linked here; links to this page; shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/void Linked here; links to this page; shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins Linked here; links to this page; shared tags: #builtins, #typescript, #web
- tuts/typescript/type_system/generics_and_constraints links to this page; shared tags: #typescript, #web
- tuts/typescript/type_system/structural_typing_and_narrowing Linked here; shared tags: #typescript, #web
- tuts/typescript/builtins/any shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/as_const shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/asserts shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/enum shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/infer shared tags: #builtins, #tier, #typescript, #web