← blog
TypeScriptNovember 27, 2026 · 5 min read

A Typo Widened My Generic Instead of Erroring. NoInfer<T> Fixes It.

Why a typo'd default argument sailed through a generic function with zero errors — and how TypeScript's NoInfer<T> stops it from silently widening the inferred type.

Parsa Jiravand · Frontend engineer · building bestpractic
A Typo Widened My Generic Instead of Erroring. NoInfer<T> Fixes It.

Someone on the team adds a new feature flag, wires up its default, and opens a PR:

TypeScript
defineFlags(["darkMode", "newCheckout", "betaSearch"], "darkmode");

One lowercase m. Nobody catches it in review — the build is green, the editor shows no red squiggle, defineFlags is fully generic and type-safe by every measure anyone checked. It merges.

Three days later the settings panel that reads this default renders a blank icon, because the lookup table keyed by flag name has no entry for "darkmode" — only "darkMode". The function whose entire job was to make this exact typo impossible let it straight through, silently.

The fix is one word. Understanding why TypeScript needed it took longer.

Here's roughly what defineFlags looked like:

TypeScript
1
2
3
4
5
6
function defineFlags<Name extends string>( names: Name[], defaultFlag?: Name ) { return { names, defaultFlag }; }

This reads like it should be bulletproof. Name is inferred from the array you pass in, so defaultFlag has to be one of those exact strings — that's the whole point of writing it generic instead of typing both parameters as string.

Spot the bug yet? Try it before you scroll: paste this into a .ts file and see what the compiler says.

TypeScript
1
2
defineFlags(["darkMode", "newCheckout", "betaSearch"], "newCheckout"); // fine defineFlags(["darkMode", "newCheckout", "betaSearch"], "darkmode"); // ???

Zero errors. Both lines. The second one — the typo — compiles exactly as cleanly as the first.

Here's the part that's easy to get wrong: TypeScript doesn't treat the array argument as "the source of truth" for Name and then check defaultFlag against it. It collects a candidate type from every position where Name appears unannotated, across the whole call, and only then decides what Name is.

names offers "darkMode" | "newCheckout" | "betaSearch" as a candidate. defaultFlag offers whatever you passed — including "darkmode" — as another candidate. TypeScript doesn't pick one and validate the other against it. It unions them. Name quietly becomes "darkMode" | "newCheckout" | "betaSearch" | "darkmode", and under that definition, passing "darkmode" for a parameter of type Name is completely correct. You can watch it happen by forcing the inferred type into the open:

TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function defineFlagsTyped<Name extends string>( names: Name[], defaultFlag?: Name ): Name { return names[0] as Name; } const result = defineFlagsTyped( ["darkMode", "newCheckout", "betaSearch"], "darkmode" ); // Assigning the "real" union back reveals what Name actually became: const check: "darkMode" | "newCheckout" | "betaSearch" = result; // Type '"darkMode" | "newCheckout" | "betaSearch" | "darkmode"' // is not assignable to type '"darkMode" | "newCheckout" | "betaSearch"'.

Name picked up a fourth member nobody asked for. The typo isn't a value that failed a check — it's part of the type now.

Runs right in your browser — poke at it and watch the concept react live.

defaultFlag was never supposed to help decide what Name is. It was supposed to be checked against a Name that the array had already settled. That's exactly what NoInfer<T> — built into TypeScript's standard library since 5.4 — exists to say:

TypeScript
1
2
3
4
5
6
7
8
9
10
11
function defineFlags<Name extends string>( names: Name[], defaultFlag?: NoInfer<Name> ) { return { names, defaultFlag }; } defineFlags(["darkMode", "newCheckout", "betaSearch"], "newCheckout"); // fine defineFlags(["darkMode", "newCheckout", "betaSearch"], "darkmode"); // Argument of type '"darkmode"' is not assignable to parameter of type // '"darkMode" | "newCheckout" | "betaSearch" | undefined'.

Same call, same arguments — now the typo is a compile error, pointing at the exact argument, before the PR even opens.

NoInfer<T> doesn't change what Name can be; it changes who gets to decide. Wrapping a position in it removes that position from the pool of candidates TypeScript draws Name from, without removing the requirement that whatever you pass there still has to match. names is still the only thing allowed to say what Name is. defaultFlag has to agree with it, not contribute to it.

It's a type-only change — nothing about the function's runtime behavior moves. You're only narrowing which arguments count as evidence to the type checker.

NoInfer<T> only works at compile time, against types TypeScript can actually see. A typed value — even a plain string | undefined from process.env — still gets checked against the real Name and still gets rejected if it's wrong. The gap is narrower than "anything not a literal": it's specifically any. JSON.parse() returns any by definition, and any bypasses every check in the type system, NoInfer included — defineFlags([...], JSON.parse(rawConfig)) compiles clean no matter what string ends up inside it. You still need a runtime check (or a schema validator) at the edge where any-typed data enters your program.

It also needs TypeScript 5.4 or newer — it shipped in March 2024 as a real compiler intrinsic (type NoInfer<T> = intrinsic), not a type you could write yourself. Before 5.4, people approximated it with a conditional-type trick (type NoInfer<T> = [T][T extends any ? 0 : never]), which worked in simple cases but had edge-case bugs the built-in version doesn't share. If you're on an older compiler, that trick is a stopgap — upgrading is the real fix.

And it won't flag a parameter you forgot to mark — this is opt-in, function by function. It fixes the position you wrap, not every generic helper in your codebase that has the same shape.

Any generic helper with a "default," a "fallback," or a "this has to match" parameter sitting next to the argument that actually defines the type is a candidate for this bug — it's letting a position vote that should only ever be checked. The fix is wrapping it in NoInfer<T>, and it's safe to add retroactively: it only makes previously-accepted typos start failing, nothing that was correct before stops compiling.

Go find one of those helpers in your own codebase — a config builder, a reducer map, an event registry — and check whether its "default" parameter is quietly widening instead of being validated. What did you find?

Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.


🚀 Want more like this? Every guide, playground, and quiz lives on bestpractic.org — open it and sign up free so the next one finds you.

Thanks for reading! Let's stay connected:

Keep reading

One post a day, in your inbox

Each one with a runnable playground and a quiz. No pitch, no digest, unsubscribe in one click.

0 comments

Sign in to join the discussion, like comments, and save articles for later.