Preventing inference
NoInfer
is a technique used in TypeScript to prevent the compiler from inferring a type in certain situations.
You might use it to ensure that a type is explicitly provided rather than relying on TypeScript's default inference mechanism. While TypeScript doesn't include a built-in NoInfer
type, you can create your own by using the infer
keyword in combination with a conditional type.
This forces TypeScript to "skip" type inference and prompts you to explicitly specify a type.
Using NoInfer too often can make your code more verbose and less flexible. TypeScript's automatic type inference is powerful and often does exactly what you need. Overriding it with NoInfer should be done cautiously, as it removes some of TypeScript's ease of use and adds manual work for the developer.