Q64: How do you extract object value types using `typeof` and `keyof` combined?
You can generate a union type of all values in an object constant by combining indexed access ([keyof typeof obj]) with as const assertions.
Extracting Value Types from Object Constants
const Statuses = {
Pending: 'PENDING',
Approved: 'APPROVED',
Rejected: 'REJECTED'
} as const;
type StatusValue = typeof Statuses[keyof typeof Statuses];
// Resulting Union Type: "PENDING" | "APPROVED" | "REJECTED"💡 Indexed access lookup extracts the object's property value types.
Key Takeaways
- Creates type-safe value unions directly from object constants.