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 parameter | Description |
|---|---|
T | The 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.
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
blueRelated: Other type parameters still infer