Skip to the solution
HardFrontend11 min read

What does NoInfer<T> do and when should you block inference?

704 views

The problem

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": T is inferred from the other sites first, then the argument in the NoInfer position is checked against it
  • The classic example: light<C extends string>(colors: C[], defaultColor?: NoInfer<C>) rejects "blue" instead of adding it to C
  • 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 just X; if NoInfer is the only site, T falls back to unknown or 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.

  • infer
  • NoInfer

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

11 min read Β· 10 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

TypeScriptNoInfer vs inference from every argument, and the old workarounds
TypeScriptEdge cases: only site, literal widening, never and nesting

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 70 of 81 in TypeScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

Why are conditional types distributive and how do you control it with `infer`?

Continue