Question presented to candidate:
"TypeScript 5.4 added a utility type called NoInfer. What problem does it solve, how does it change type inference, and can you show me a function signature where you would actually reach for it?"
What a strong answer should cover:
- Every occurrence of a type parameter in the parameter list is an inference site; candidates from all sites are combined, so a "secondary" argument (a default, a fallback, a transition target) can silently widen
T NoInfer<T>(TypeScript 5.4) marks one position as "check only":Tis inferred from the other sites first, then the argument in theNoInferposition is checked against it- The classic example:
light<C extends string>(colors: C[], defaultColor?: NoInfer<C>)rejects"blue"instead of adding it toC - Before 5.4: an extra type parameter
D extends C, or a deferred conditional trick;C & {}does not block inference - Edge cases:
NoInfer<X>on a concrete type is justX; ifNoInferis the only site,Tfalls back tounknownor its default; it is erased at run time - When not to use it: when every argument should legitimately contribute, or when the plain signature already reports the mistake
Code / implementation expected: Yes β a generic function where one argument must conform to a type inferred from another, with and without NoInfer.