A teammate's PR adds a new event to the bus your defineEvents() helper manages. It compiles clean. No red squiggle, no CI failure, no review comment. Three weeks later someone adds a switch over those event names, forgets a case, and TypeScript says nothing about it either — because by the time it reaches that switch, the type isn't 'login' | 'logout' | 'refresh' anymore. It's just string. It's been string since the PR that added the event, and nobody noticed.
I'd seen this exact failure before — I wrote about it when it happened inside a reducer's own file, and as const fixed it there. This time it wasn't my code going wide. It was everyone else's code calling mine.
The fix that only works if everyone remembers it
Here's the helper, trimmed down to the part that matters:
function defineEvents<T extends readonly string[]>(names: T) { return { names, on<K extends T[number]>(name: K, handler: () => void) { /* ... */ }, }; } const bus = defineEvents(['login', 'logout', 'refresh']); Guess what T is inside bus before you scroll — the literal union 'login' | 'logout' | 'refresh', or plain string[]?
It's string[]. Passing an array literal into a generic parameter widens it the same way assigning it to a let would — TypeScript keeps the general shape (string[]) instead of the three specific strings, because without more information it assumes you might .push() a fourth event onto it later. bus.on('login', ...) still type-checks, but so does bus.on('logign', ...) — the typo nobody wants to ship. The whole reason to write a typed helper just evaporated.
The fix I reached for first was the one from the reducer post: tell callers to add as const.
const bus = defineEvents(['login', 'logout', 'refresh'] as const); // T is now readonly ["login", "logout", "refresh"] — the typo-catching version That works. It also means every one of my helper's twelve call sites across the codebase needs that suffix, forever, and a thirteenth call site someone writes next sprint needs it too — with no compiler reminder if they skip it. I put a comment above the function: // remember: always pass as const. Comments are not a type system. Nobody reads them mid-refactor, and the whole point of writing a generic helper was to stop relying on anyone remembering anything.
Why the widening happens at all
Quick recap, because it's the same rule in both places: a CSS transition needs real numbers on both ends to interpolate, and a generic type parameter needs a reason to keep a literal instead of generalizing it. By default, TypeScript treats any array or object literal you hand to an inferred type parameter as a value that might change shape later, so it picks the widest type that's still technically correct — string[], not the three strings you actually wrote. It isn't being unhelpful; string[] is a safe default for a value that could get .push()-ed. The problem is that "safe default" is exactly wrong for a config-shaped argument that's never going to be mutated, which is most of what gets passed to this kind of helper.
The actual fix: a const type parameter
TypeScript 5.0 added a modifier for exactly this spot — not on the call site, on the function's own signature:
function defineEvents<const T extends readonly string[]>(names: T) { return { names, on<K extends T[number]>(name: K, handler: () => void) { /* ... */ }, }; } const bus = defineEvents(['login', 'logout', 'refresh']); // T is readonly ["login", "logout", "refresh"] — no "as const" anywhere One word, const, added where you declare T, not where anyone calls the function. It tells the compiler: "when you infer this type parameter, infer it the way you would if the caller had written as const on the argument" — literal strings instead of string, a readonly tuple instead of a mutable array. Object arguments get the same treatment: literal property values stay literal instead of widening to their base type.
🎮 Try it yourself
▶️ Open the interactive playground →
Runs right in your browser — poke at it and watch the concept react live.
The payoff shows up downstream, in code that has nothing to do with the helper itself:
function label(name: 'login' | 'logout' | 'refresh'): string { switch (name) { case 'login': return 'Signed in'; case 'logout': return 'Signed out'; // forgot 'refresh' } } // Error: Function lacks ending return statement, and return type // does not include 'undefined' — because 'refresh' was never handled. That error only exists because name's type is still the three literal strings by the time it reaches the switch. Widen it to string anywhere upstream — which is exactly what used to happen the moment someone forgot as const — and the switch above compiles with a silently missing case. The const modifier closes the gap at the one place that actually matters: the function signature, which every caller goes through whether they think about types or not.
The honest limit — it only helps the moment it widens
const T isn't magic; it only has literal information to preserve if the caller gives it a literal expression directly:
const bus1 = defineEvents(['login', 'logout', 'refresh']); // literal types preserved const names: string[] = ['login', 'logout', 'refresh']; const bus2 = defineEvents(names); // T is string[] — already widened before it got here names was declared string[] the moment it was written. By the time it's handed to defineEvents, the literal information is already gone — const T can preserve a literal, it can't resurrect one. In practice this is rarely the problem: most calls to a config-shaped helper pass the array or object inline, which is exactly the pattern const type parameters were built for.
The other honest note: this is a compile-time-only feature. It needs TypeScript 5.0+ (shipped March 2023) and does nothing at runtime — no polyfill, no browser support matrix, no @supports check. If your build already uses a TS version from the last couple of years, you already have it; you were just one keyword away from using it.
🧠 Test yourself
Think it clicked? Take the 8-question quiz →
Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.
The takeaway
That PR that compiled clean three weeks ago wasn't a reviewer's mistake — it was a type system with no way to flag what it had already lost. The reducer post fixed widening where the value is written once, by you. This fixes it where the value is written by whoever calls your function next, which is the harder half, because you don't control their keystrokes — you only control the signature they call into.
Go find the generic helper in your codebase with a // remember to add as const comment above it, and check whether const T can replace the comment with an enforced guarantee. What's the oldest "please remember to…" comment in your code that a type system could be doing instead?
📚 Read next
- My Reducer's Switch Stopped Being Exhaustive.
as constFixed It. - Inferred Type Predicates Won't Save filter(Boolean). Here's What Will.
- TypeScript Generics: The Complete Guide (with Cheat Sheet)
🚀 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:
- ⭐ GitHub — follow me and star the projects: github.com/parsajiravand
- 💬 Discord — join the frontend best-practices community: discord.gg/d9KRhuAwQ
- 📸 Instagram — frontend best practices, daily: @bestpractice___

