TypeScript · Senior · Question 12 of 75

What is the NoInfer<T> utility (TS 5.4) for?

function pick<C extends string>(colors: C[], fallback: NoInfer<C>) {}
pick(['red', 'blue'], 'green');

Explanation

Without NoInfer, TS would infer C as 'red' | 'blue' | 'green' from both arguments, and there'd be no error. NoInfer says: "take C from elsewhere and only check this argument".

Further reading

Problem with a question

TypeScript · Senior · question 12

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