Back to Blog
TypeScriptkeyoftypeofType Operators

TypeScript keyof and typeof Operators Guide

Master TypeScript keyof and typeof operators for creating dynamic types from values and keys.

B
Bootspring Team
Engineering
September 12, 2019
7 min read

The keyof and typeof operators enable powerful type manipulation by extracting types from values and keys. Here's how to use them.

Basic keyof

Loading code block...

Basic typeof

Loading code block...

Combining keyof and typeof

Loading code block...

Object Property Access Types

Loading code block...

Enum-like Objects

Loading code block...

Generic Constraints

Loading code block...

Mapped Types with keyof

Loading code block...

Type Guards with keyof

Loading code block...

Function Parameter Types

Loading code block...

Conditional Types with keyof

Loading code block...

Index Signatures

Loading code block...

Class Types

Loading code block...

Template Literal Types

Loading code block...

Real-World Examples

Loading code block...

Best Practices

keyof Usage: ✓ Type-safe property access ✓ Generic constraints ✓ Mapped types ✓ Dynamic key validation typeof Usage: ✓ Extract types from values ✓ Configuration objects ✓ Function return types ✓ Const assertions Combinations: ✓ keyof typeof for objects ✓ ReturnType<typeof fn> ✓ Parameters<typeof fn> ✓ InstanceType<typeof Class> Avoid: ✗ Overcomplicating simple types ✗ Ignoring index signatures ✗ Missing const assertions ✗ Excessive type inference

Conclusion

The keyof and typeof operators are fundamental for TypeScript type manipulation. Use keyof to extract union types of object keys, typeof to derive types from values, and combine them for powerful patterns like enum-like objects and type-safe property access. These operators enable writing generic, reusable code while maintaining full type safety.

Share this article

Help spread the word about Bootspring

Related articles