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');- ✓Correct answer: It prevents inferring
Cfrom this argument - so there's an error - 2It disables type inference for the whole function and its arguments
- 3It makes the
fallbackparameter optional with a default value
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".