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>> { /* ... */ }
}

Explanation

The final .build() can then return a precise type, and invalid call orders can be prevented.

Further reading

Problem with a question

TypeScript · Senior · question 54

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