How TypeScript Infers Generic Types

Where TypeScript gets T from, why it widens or picks the wrong candidate, and NoInfer and const type parameters that fix it.

In this chapter

How generic inference works

More on Generics

Keep learning

Overview

chapters · generics How generic inference works Where TypeScript looks to fill T, how to steer it, and when you did not need T at all.

1. The fallback that should have failed

Reminder: inference means TypeScript fills the type blank T by itself, from the arguments you pass. A pizza size picker takes the valid sizes and a fallback. T extends string means T must be some kind of string. A typo in the fallback gets no error. "small" | "large" | "huge" No error. TypeScript inferred T from both arguments. So 'huge' joined the union (the either-or list of allowed values). Each value here is a literal type: one exact string used as a type.

2. Where TypeScript looks

Every spot where T appears in a parameter is an inference site (a place it reads T from). collect candidates one per argument that mentions T combine them literals → union, or widen (e.g. "a" → string) check the constraint T extends … check every argument against the final T Inferring pickSize(['small', 'large'], 'huge') site 1: options: T[] candidates: "small", "large" site 2: fallback: T candidate: "huge" combine (string constraint keeps literals) check arguments all pass, no error does it infer T? the argument element type the type on the left (Order) is used as a hint for T

3. NoInfer: “don't learn from this one” (TS 5.4)

Wrap a parameter in NoInfer<T>. That argument is still checked against T. But TypeScript does not use it to decide what T is, so it cannot add new values to T. both sites infer Accepted. T grew to include "huge". only options infers Argument of type '"huge"' is not assignable to parameter of type '"small" | "large"'. T is decided from options alone: "small" | "large". Then 'huge' is checked against it and doesn't fit.

4. const type parameters (TS 5.0)

Type parameter plain T Route names are lost. You only know it is some strings. readonly ["home", "cart"] Exact tuple of literals. Callers write nothing extra. Same result, but every caller must remember it.

5. All or nothing: the partial inference problem

A type argument is a type you write yourself in <…> at the call. Pass one and TypeScript stops inferring the rest. There is no “infer the others”. Expected 2 type arguments, but got 1. Why: on has two blanks, Payload and Name. Once you write any <…>, you must fill them all. TypeScript will not infer Name from 'order.paid' anymore. name: "order.paid" on<P>()(…). Odd-looking (), but tiny. Generic class

6. The type parameter you did not need

Golden rule: a type parameter should appear at least twice. Its job is to link two places, like "the input type equals the output type". Used once, it links nothing. T appears once size gains nothing from T. parse<User> is a cast: you just tell TypeScript "trust me, it's a User". Nothing checks the JSON really is one. say what you mean Callers must now check the unknown value (for example with typeof or in) before using it. That check is called narrowing. It is honest. What happens? Error on 'x'

Remember

Every parameter that mentions T is an inference site. NoInfer<T> checks a parameter without letting it grow T. A type parameter used once is noise, or a hidden cast. ! Cheat code Sites feed T. NoInfer blocks a site. A T used once should go. Too wide? Learns from the wrong arg? Need one explicit, rest inferred? curry it

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.