NoInfer<T>

Blocks the compiler from using T as a source of generic type inference.

Since TS 5.4 Spec ↗

Syntax

NoInfer<Type>

Returns

type — The same type T, but excluded from candidate inference for type parameters.

Examples

function createState<T>(
  values: T[],
  initial: NoInfer<T>
) {
  return { values, initial };
}

createState(['a', 'b'], 'a'); // T inferred from values only
// Without NoInfer, the initial argument would widen T.
function pick<T>(items: T[], fallback: NoInfer<T>) {
  return items[0] ?? fallback;
}

Notes

Use it on parameters that should be checked against, but not contribute to, the inferred type argument. This prevents an argument from accidentally widening a generic that you want inferred from another parameter.

See also