Skip to content

NoInfer

In one line

NoInfer<T> stops TypeScript from using a parameter position to infer T — useful for controlling inference.

In simple words

The NoInfer<T> utility type (TypeScript 5.4+) prevents TypeScript from using a particular position to infer a type parameter. This gives you fine-grained control over type inference.

Without NoInfer, TypeScript infers from all positions. Sometimes you want it to infer from one argument but not another. NoInfer blocks inference from specific positions while still constraining the type.

Common use case: a function with a default value parameter where you don't want the default to influence the inferred type of the main parameter.

Example

typescript
// Without NoInfer — default value pollutes inference
function createList<T>(value: T, defaultVal: T = value): T[] {
  return [value, defaultVal];
}
// createList("a", "b") — T inferred as "a" | "b" (from both args)

// With NoInfer — only value determines T
function createList2<T>(value: T, defaultVal: NoInfer<T> = value): T[] {
  return [value, defaultVal];
}
const list = createList2("a"); // T is "a", not "a" | undefined

// Practical: event handler with default
function on<K extends string>(event: K, handler: (e: NoInfer<K>) => void) {
  // K is inferred from event, not from handler's parameter
}

NoInfer prevents the defaultVal position from participating in T inference. T is inferred only from the value argument, giving more precise types.

Common confusions

  • Confused with: type constraint

    The difference: A type constraint (T extends string) restricts what T can be but doesn't affect inference positions. NoInfer<T> doesn't constrain T but tells TypeScript not to use this position for inference. Constraints limit; NoInfer redirects inference sources.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary