TypeScript 7 reference · Utility Types

TypeScript NoInfer<Type> Utility Type

Stop TypeScript from inferring a type parameter from a particular argument, so other arguments decide it.

TypeScript 7 Since 5.4

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

NoInfer<Type>

Parameters

ParameterDescription
Type The type that shouldn't be used as an inference source.

Return value

The same type.

Examples

Make the fallback match the optionsTry it yourself
function pick<T extends string>(options: T[], fallback: NoInfer<T>): T {
  return options[0] ?? fallback;
}
console.log(pick(["small", "medium"], "small"));
// @ts-expect-error: "huge" is not one of the options
pick(["small", "medium"], "huge");

Output

small
Infer from one argument onlyTry it yourself
function createState<T>(initial: T, allowed: NoInfer<T>[]) {
  return { value: initial, allowed };
}
console.log(createState("idle", ["idle", "busy"]));

Output

{ value: 'idle', allowed: [ 'idle', 'busy' ] }

Version history

VersionChange
TypeScript 5.4Added

Was this page helpful?