What is the NoInfer utility type in TypeScript 5.4?
Answer
NoInfer<T> (TypeScript 5.4) is a built-in utility type that prevents TypeScript from using a generic type parameter as an inference site. This gives you control over which call-site argument drives type inference. Problem it solves: function createStore<T>(initial: T, fallback: T): T { ... }. If you call createStore("hello", 0), TypeScript infers T as string | number — not the desired behavior. With NoInfer: function createStore<T>(initial: T, fallback: NoInfer<T>): T { ... } — T is inferred only from initial, and fallback must be compatible with the already-inferred T. Calling createStore("hello", 0) now gives a type error because 0 is not assignable to string. NoInfer is implemented as an intrinsic type (not expressible in user-land TypeScript). It is essential for APIs where one argument should constrain others without participating in inference itself.
Previous
What is structural compatibility and how does it affect assignability in TypeScript?
Next
What are type-safe event emitters in TypeScript?
More TypeScript Questions
View all →- Advanced What is structural typing in TypeScript?
- Advanced What are branded types (opaque types) in TypeScript?
- Advanced What is the Variance annotation in TypeScript 4.7?
- Advanced What is the satisfies operator used for in TypeScript?
- Advanced How do you implement a deep partial type in TypeScript?