TypeScript · Senior · Question 38 of 75

What does keyof (A | B) return if A = { id; name } and B = { id; price }?

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.

Further reading

Problem with a question

TypeScript · Senior · question 38

Test yourself

Take the TypeScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz