TypeScript · Senior · Question 38 of 75
What does keyof (A | B) return if A = { id; name } and B = { id; price }?
- ✓Correct answer:
'id' - 2
'id' | 'name' | 'price' - 3
never
Explanation
A union value can be any of its members, so only common keys are safe to access. keyof (A & B) gives the union of all keys. To distribute keyof over union members, use a conditional type: T extends any ? keyof T : never.