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
Syntax
NoInfer<Type>
Parameters
| Parameter | Description |
|---|---|
Type |
The type that shouldn't be used as an inference source. |
Return value
The same type.
Examples
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
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
| Version | Change |
|---|---|
| TypeScript 5.4 | Added |
Related
Parameters<Type>· Get a function's parameter types as a tuple, useful for wrappers that pass arguments through.- TypeScript Utility Types · tutorial