TypeScript · Senior · Question 54 of 75
How does a type-safe builder accumulate information in its type?
class Query<T extends object = {}> {
where<K extends string, V>(k: K, v: V): Query<T & Record<K, V>> { /* ... */ }
}- ✓Correct answer: Each method returns the builder with a new type parameter that extends the previous one
- 2It mutates a global type
- 3It uses
anyand casts at the end
Explanation
The final .build() can then return a precise type, and invalid call orders can be prevented.