TypeScript's `typeof`: Get a Type from a Value
TypeScript's `typeof` operator grabs the static type from a runtime value, like a variable. It's essential for utilities like `ReturnType`, letting you derive a type from a function's implementation without duplicating definitions.
TypeScript's `typeof` operator bridges the gap between runtime values and compile-time types, letting you grab the static type of a variable or property. It shines when combined with other type utilities, like inferring a function's return type with `ReturnType<typeof myFunc>`. This avoids duplicating type definitions. The main footgun is confusing it with JavaScript's runtime `typeof` or trying to use it on complex expressions; it only works on identifiers at compile time.
Read the original → typescriptlang.org
- #typescript
- #types
- #operators
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.