NoInfer and inference control

NoInfer blocks inference from one position without stopping inference of the type parameter from other arguments, or of other type parameters.

テーマページの本文は現在英語です。

NoInfer is an intrinsic marker declared as type NoInfer<T> = intrinsic in TypeScript 5.9.3 lib.es5.d.ts. It was added in TypeScript 5.4. Official typings describe it as a marker for a non-inference type position.

It does not turn off type inference for the whole function. A type parameter can still be inferred from other arguments. Other type parameters in the same signature are still inferred as usual. NoInfer only prevents that occurrence of the type parameter from contributing candidates.

Examples were checked with TypeScript 5.9.3 using tsc --strict. See the TypeScript handbook for the complete utility-type catalog.

TypeScript reference overview →

API index

APISummary
NoInferMark a type position so it does not contribute inference candidates. Other arguments can still infer the type parameter.
NoInferNoInfer on one parameter leaves inference from other parameters, and of other type parameters, unchanged.

Blocking inference from one parameter

In the light function below, the type parameter is inferred from colors. The fallback argument is then checked against that inferred type. A call with "red" as the fallback is fine when colors is "red" and "green". A call with "blue" is a type error, because "blue" is not in that union. Without NoInfer, the second call would succeed by inferring a wider union that includes "blue".

NoInfer

type NoInfer<T> = intrinsic;

Mark a type position so it does not contribute inference candidates. Other arguments can still infer the type parameter.

Type parameterDescription
TThe type that should be inferred from elsewhere, then checked in this position.

Produces: A type that is equivalent to T for checking, but is not a source of inference candidates.

Type errors

  • type error — A value that does not match the type inferred from the other positions is not assignable, such as "blue" when the type parameter was inferred as "red" | "green".
  • NoInfer was added in TypeScript 5.4. It is a compile-time marker, not a runtime wrapper.
typescript
function light<C extends string>(colors: C[], fallback?: NoInfer<C>) {
  return fallback;
}
console.log(light(["red", "green"], "red"));
// @ts-expect-error "blue" is not inferred into C; C comes from colors only
light(["red", "green"], "blue");

function lightOff<C extends string>(colors: C[], fallback?: C) {
  return fallback;
}
console.log(lightOff(["red", "green"], "blue"));

Expected output

red
blue

Related: Other type parameters still infer

Official documentation →

What NoInfer does not stop

pair still infers its type parameter from first. The extends string constraint keeps the inferred type a string literal, so pair("a", "a") works and pair("a", "b") fails. Without that constraint, the literals can widen to string and the second argument would be accepted; NoInfer does not, by itself, prevent that widening. In both, A is inferred from a, B is still inferred from b, and same is only checked against A. both(1, "z", 1) works. both(1, "z", "z") is an error even though "z" inferred B successfully. NoInfer did not freeze the whole signature.

NoInfer

type NoInfer<T> = intrinsic;

NoInfer on one parameter leaves inference from other parameters, and of other type parameters, unchanged.

Type parameterDescription
T / A / BType parameters inferred from the unmarked arguments. The NoInfer position is checked after that inference.

Produces: The function results below are ordinary values; NoInfer itself is erased.

Type errors

  • type error — A mismatch at a NoInfer position is a type error against the type inferred from the other arguments, such as "b" when T was inferred as "a", or "z" when A was inferred as 1.
  • If you need to stop inference entirely, you have to write a different signature, such as supplying the type argument explicitly. NoInfer is not a switch that disables inferencing.
typescript
function pair<T extends string>(first: T, second: NoInfer<T>) {
  return [first, second] as const;
}
console.log(pair("a", "a").join(","));
// @ts-expect-error T is inferred from first, not from second
pair("a", "b");

function both<A, B>(a: A, b: B, same: NoInfer<A>) {
  return [a, b, same] as const;
}
console.log(both(1, "z", 1).join(","));
// @ts-expect-error A is inferred from a as 1; B is still inferred from b
both(1, "z", "z");

Expected output

a,a
1,z,1

Official documentation →