[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"verticals":3,"quiz-typescript-const-type-parameters":44,"search-suggestions":60,"quiz-article-typescript-const-type-parameters":109,"path-membership-typescript-const-type-parameters":301},[4,20,32],{"id":5,"slug":6,"name":7,"tagline":8,"description":9,"accentFrom":10,"accentTo":11,"icon":12,"defaultLocale":13,"locales":14,"features":16,"position":19},"019fe637-3d33-714b-b57f-23e163ffca0c","dev","Web Development","Read it. Run it. Prove it.","A post a day on modern web development — most with an editable playground and a quiz that explains every answer. Free, no account needed.","violet-500","cyan-400","◇","en",[13,15],"fa",{"courses":17,"paths":17,"articles":17,"exams":18,"flashcards":18,"packages":17,"community":17,"certificates":17,"teams":17,"commerce":17},true,false,0,{"id":21,"slug":22,"name":23,"tagline":24,"description":25,"accentFrom":26,"accentTo":10,"icon":27,"defaultLocale":13,"locales":28,"features":30,"position":31},"019fe637-3dc2-754c-8657-0f175bfee7c6","lang","Languages","Learn a language the way you learn a codebase.","Grammar explained the way good documentation explains an API — one idea at a time, each with a quiz.","amber-400","⌘",[13,15,29],"es",{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":17,"packages":18,"community":17,"certificates":17,"teams":18,"commerce":18},2,{"id":33,"slug":34,"name":35,"tagline":36,"description":37,"accentFrom":38,"accentTo":39,"icon":40,"defaultLocale":13,"locales":41,"features":42,"position":43},"7b3c16f2-931d-410e-802e-e1fa4edab7de","soft","Soft Skills","The half of the job nobody wrote documentation for.","Weekly, on the parts of working life that decide more than your code does — first weeks, meetings, interviews, promotions, and the people around you. Written from what actually happens, and recorded as a podcast you can listen to on the walk.","emerald-400","teal-300","◉",[13],{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":18,"packages":18,"community":17,"certificates":18,"teams":18,"commerce":18},3,{"id":45,"slug":46,"kind":47,"title":48,"description":49,"config":50,"verticalId":5,"vertical":55,"course":52,"_count":56,"access":57,"attempts":59,"questionCount":51},"01a21cc5-7e21-7424-abb4-f80e72d60262","typescript-const-type-parameters","PRACTICE_QUIZ","const Type Parameters","Checks whether you've got the default widening behavior, what the `const` modifier actually changes, where it goes, and the one honest limit on what it can fix.",{"questionCount":51,"timeLimitSec":52,"shuffleQuestions":18,"shuffleOptions":17,"negativeMarking":19,"passScorePct":53,"maxAttempts":52,"revealAnswers":54,"allowFlagging":18,"allowBacktracking":17},8,null,70,"AFTER_SUBMIT",{"slug":6,"name":7},{"questions":51},{"allowed":17,"reason":58},"FREE",[],[61,65,69,73,77,81,85,89,93,97,101,105],{"slug":62,"name":63,"articles":64},"webdev","Webdev",197,{"slug":66,"name":67,"articles":68},"javascript","Javascript",157,{"slug":70,"name":71,"articles":72},"frontend","Frontend",103,{"slug":74,"name":75,"articles":76},"tutorial","Tutorial",87,{"slug":78,"name":79,"articles":80},"css","Css",56,{"slug":82,"name":83,"articles":84},"typescript","Typescript",37,{"slug":86,"name":87,"articles":88},"performance","Performance",33,{"slug":90,"name":91,"articles":92},"programming","Programming",26,{"slug":94,"name":95,"articles":96},"react","React",23,{"slug":98,"name":99,"articles":100},"node","Node",19,{"slug":102,"name":103,"articles":104},"browser","Browser",18,{"slug":106,"name":107,"articles":108},"nextjs","Nextjs",14,{"id":110,"slug":46,"title":111,"subtitle":52,"excerpt":112,"coverUrl":113,"locale":13,"readingMinutes":114,"publishedAt":115,"viewCount":51,"likeCount":19,"commentCount":19,"author":116,"vertical":121,"topic":122,"tags":124,"_count":129,"playground":131,"body":133,"bodyMd":287,"seo":288,"translationGroupId":291,"series":52,"podcastUrl":52,"verticalId":5,"thread":292,"assessments":294,"translations":297,"quiz":299},"01a21cc5-7d63-76c4-94d1-bdb6902fadbe","I Stopped Asking Callers for `as const`. Const Type Parameters Do It.","A generic helper only keeps its literal types if every caller remembers to tack `as const` onto the argument — forget once and it silently widens. TypeScript's const type parameters infer the literal type automatically, with no cooperation from the caller required.","\u002Fmedia\u002Fcovers\u002Ftypescript-const-type-parameters.png",6,"2026-12-01T04:21:15.823Z",{"id":117,"name":118,"username":119,"avatarUrl":52,"headline":120},"019fe637-3c25-7088-9034-39c9f15dc3c8","Parsa Jiravand","parsa","Frontend engineer · building bestpractic",{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":82,"name":123},"TypeScript",[125,126,127,128],{"slug":82,"name":83,"color":52},{"slug":66,"name":67,"color":52},{"slug":62,"name":63,"color":52},{"slug":74,"name":75,"color":52},{"assessments":130},1,{"slug":46,"title":132},"const type parameters — interactive playground",{"blocks":134,"version":130},[135,139,142,146,149,155,158,161,164,168,171,174,177,181,184,188,191,194,197,200,203,206,209,213,216,219,222,226,229,232,235,238,241,244,247,250,253,256,259,262,269,272,275,278,281],{"id":136,"html":137,"type":138},"b1","\u003Cp>A teammate&#39;s PR adds a new event to the bus your \u003Ccode>defineEvents()\u003C\u002Fcode> helper manages. It compiles clean. No red squiggle, no CI failure, no review comment. Three weeks later someone adds a \u003Ccode>switch\u003C\u002Fcode> 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&#39;t \u003Ccode>&#39;login&#39; | &#39;logout&#39; | &#39;refresh&#39;\u003C\u002Fcode> anymore. It&#39;s just \u003Ccode>string\u003C\u002Fcode>. It&#39;s been \u003Ccode>string\u003C\u002Fcode> since the PR that added the event, and nobody noticed.\u003C\u002Fp>","paragraph",{"id":140,"html":141,"type":138},"b2","\u003Cp>I&#39;d seen this exact failure before — I wrote about it when it happened inside a reducer&#39;s own file, and \u003Ccode>as const\u003C\u002Fcode> fixed it there. This time it wasn&#39;t my code going wide. It was everyone else&#39;s code calling mine.\u003C\u002Fp>",{"id":143,"html":144,"text":144,"type":145,"level":31},"b3","The fix that only works if everyone remembers it","heading",{"id":147,"html":148,"type":138},"b4","\u003Cp>Here&#39;s the helper, trimmed down to the part that matters:\u003C\u002Fp>",{"id":150,"code":151,"type":152,"language":153,"highlight":154},"b5","function defineEvents\u003CT extends readonly string[]>(names: T) {\n  return {\n    names,\n    on\u003CK extends T[number]>(name: K, handler: () => void) { \u002F* ... *\u002F },\n  };\n}\n\nconst bus = defineEvents(['login', 'logout', 'refresh']);","code","ts",[],{"id":156,"html":157,"type":138},"b6","\u003Cp>Guess what \u003Ccode>T\u003C\u002Fcode> is inside \u003Ccode>bus\u003C\u002Fcode> before you scroll — the literal union \u003Ccode>&#39;login&#39; | &#39;logout&#39; | &#39;refresh&#39;\u003C\u002Fcode>, or plain \u003Ccode>string[]\u003C\u002Fcode>?\u003C\u002Fp>",{"id":159,"html":160,"type":138},"b7","\u003Cp>It&#39;s \u003Ccode>string[]\u003C\u002Fcode>. Passing an array literal into a generic parameter widens it the same way assigning it to a \u003Ccode>let\u003C\u002Fcode> would — TypeScript keeps the general shape (\u003Ccode>string[]\u003C\u002Fcode>) instead of the three specific strings, because without more information it assumes you might \u003Ccode>.push()\u003C\u002Fcode> a fourth event onto it later. \u003Ccode>bus.on(&#39;login&#39;, ...)\u003C\u002Fcode> still type-checks, but so does \u003Ccode>bus.on(&#39;logign&#39;, ...)\u003C\u002Fcode> — the typo nobody wants to ship. The whole reason to write a typed helper just evaporated.\u003C\u002Fp>",{"id":162,"html":163,"type":138},"b8","\u003Cp>The fix I reached for first was the one from the reducer post: tell callers to add \u003Ccode>as const\u003C\u002Fcode>.\u003C\u002Fp>",{"id":165,"code":166,"type":152,"language":153,"highlight":167},"b9","const bus = defineEvents(['login', 'logout', 'refresh'] as const);\n\u002F\u002F T is now readonly [\"login\", \"logout\", \"refresh\"] — the typo-catching version",[],{"id":169,"html":170,"type":138},"b10","\u003Cp>That works. It also means every one of my helper&#39;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: \u003Ccode>\u002F\u002F remember: always pass as const\u003C\u002Fcode>. 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.\u003C\u002Fp>",{"id":172,"html":173,"text":173,"type":145,"level":31},"b11","Why the widening happens at all",{"id":175,"html":176,"type":138},"b12","\u003Cp>Quick recap, because it&#39;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&#39;s still technically correct — \u003Ccode>string[]\u003C\u002Fcode>, not the three strings you actually wrote. It isn&#39;t being unhelpful; \u003Ccode>string[]\u003C\u002Fcode> is a safe default for a value that could get \u003Ccode>.push()\u003C\u002Fcode>-ed. The problem is that &quot;safe default&quot; is exactly wrong for a config-shaped argument that&#39;s never going to be mutated, which is most of what gets passed to this kind of helper.\u003C\u002Fp>",{"id":178,"html":179,"text":180,"type":145,"level":31},"b13","The actual fix: a \u003Ccode>const\u003C\u002Fcode> type parameter","The actual fix: a const type parameter",{"id":182,"html":183,"type":138},"b14","\u003Cp>TypeScript 5.0 added a modifier for exactly this spot — not on the call site, on the function&#39;s own signature:\u003C\u002Fp>",{"id":185,"code":186,"type":152,"language":153,"highlight":187},"b15","function defineEvents\u003Cconst T extends readonly string[]>(names: T) {\n  return {\n    names,\n    on\u003CK extends T[number]>(name: K, handler: () => void) { \u002F* ... *\u002F },\n  };\n}\n\nconst bus = defineEvents(['login', 'logout', 'refresh']);\n\u002F\u002F T is readonly [\"login\", \"logout\", \"refresh\"] — no \"as const\" anywhere",[],{"id":189,"html":190,"type":138},"b16","\u003Cp>One word, \u003Ccode>const\u003C\u002Fcode>, added where you declare \u003Ccode>T\u003C\u002Fcode>, not where anyone calls the function. It tells the compiler: &quot;when you infer this type parameter, infer it the way you would if the caller had written \u003Ccode>as const\u003C\u002Fcode> on the argument&quot; — literal strings instead of \u003Ccode>string\u003C\u002Fcode>, 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.\u003C\u002Fp>",{"id":192,"html":193,"type":138},"b17","\u003C!-- playground:start -->",{"id":195,"html":196,"text":196,"type":145,"level":31},"b18","🎮 Try it yourself",{"id":198,"html":199,"type":138},"b19","\u003Cp>\u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ftypescript-const-type-parameters\u002Fplayground\">▶️ Open the interactive playground →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":201,"html":202,"type":138},"b20","\u003Cp>\u003Cem>Runs right in your browser — poke at it and watch the concept react live.\u003C\u002Fem>\u003C\u002Fp>",{"id":204,"html":205,"type":138},"b21","\u003C!-- playground:end -->",{"id":207,"html":208,"type":138},"b22","\u003Cp>The payoff shows up downstream, in code that has nothing to do with the helper itself:\u003C\u002Fp>",{"id":210,"code":211,"type":152,"language":153,"highlight":212},"b23","function label(name: 'login' | 'logout' | 'refresh'): string {\n  switch (name) {\n    case 'login': return 'Signed in';\n    case 'logout': return 'Signed out';\n    \u002F\u002F forgot 'refresh'\n  }\n}\n\u002F\u002F Error: Function lacks ending return statement, and return type\n\u002F\u002F does not include 'undefined' — because 'refresh' was never handled.",[],{"id":214,"html":215,"type":138},"b24","\u003Cp>That error only exists because \u003Ccode>name\u003C\u002Fcode>&#39;s type is still the three literal strings by the time it reaches the switch. Widen it to \u003Ccode>string\u003C\u002Fcode> anywhere upstream — which is exactly what used to happen the moment someone forgot \u003Ccode>as const\u003C\u002Fcode> — and the switch above compiles with a silently missing case. The \u003Ccode>const\u003C\u002Fcode> 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.\u003C\u002Fp>",{"id":217,"html":218,"text":218,"type":145,"level":31},"b25","The honest limit — it only helps the moment it widens",{"id":220,"html":221,"type":138},"b26","\u003Cp>\u003Ccode>const T\u003C\u002Fcode> isn&#39;t magic; it only has literal information to preserve if the caller gives it a literal expression directly:\u003C\u002Fp>",{"id":223,"code":224,"type":152,"language":153,"highlight":225},"b27","const bus1 = defineEvents(['login', 'logout', 'refresh']); \u002F\u002F literal types preserved\n\nconst names: string[] = ['login', 'logout', 'refresh'];\nconst bus2 = defineEvents(names); \u002F\u002F T is string[] — already widened before it got here",[],{"id":227,"html":228,"type":138},"b28","\u003Cp>\u003Ccode>names\u003C\u002Fcode> was declared \u003Ccode>string[]\u003C\u002Fcode> the moment it was written. By the time it&#39;s handed to \u003Ccode>defineEvents\u003C\u002Fcode>, the literal information is already gone — \u003Ccode>const T\u003C\u002Fcode> can preserve a literal, it can&#39;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 \u003Ccode>const\u003C\u002Fcode> type parameters were built for.\u003C\u002Fp>",{"id":230,"html":231,"type":138},"b29","\u003Cp>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 \u003Ccode>@supports\u003C\u002Fcode> 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.\u003C\u002Fp>",{"id":233,"html":234,"type":138},"b30","\u003C!-- quiz:start -->",{"id":236,"html":237,"text":237,"type":145,"level":31},"b31","🧠 Test yourself",{"id":239,"html":240,"type":138},"b32","\u003Cp>Think it clicked? \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ftypescript-const-type-parameters\u002Fquiz\">Take the 8-question quiz →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":242,"html":243,"type":138},"b33","\u003Cp>\u003Cem>Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.\u003C\u002Fem>\u003C\u002Fp>",{"id":245,"html":246,"type":138},"b34","\u003C!-- quiz:end -->",{"id":248,"html":249,"text":249,"type":145,"level":31},"b35","The takeaway",{"id":251,"html":252,"type":138},"b36","\u003Cp>That PR that compiled clean three weeks ago wasn&#39;t a reviewer&#39;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&#39;t control their keystrokes — you only control the signature they call into.\u003C\u002Fp>",{"id":254,"html":255,"type":138},"b37","\u003Cp>Go find the generic helper in your codebase with a \u003Ccode>\u002F\u002F remember to add as const\u003C\u002Fcode> comment above it, and check whether \u003Ccode>const T\u003C\u002Fcode> can replace the comment with an enforced guarantee. What&#39;s the oldest &quot;please remember to…&quot; comment in your code that a type system could be doing instead?\u003C\u002Fp>",{"id":257,"html":258,"type":138},"b38","\u003C!-- related:start -->",{"id":260,"html":261,"text":261,"type":145,"level":31},"b39","📚 Read next",{"id":263,"type":264,"items":265,"ordered":18},"b40","list",[266,267,268],"\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ftypescript-as-const-assertions\">My Reducer&#39;s Switch Stopped Being Exhaustive. \u003Ccode>as const\u003C\u002Fcode> Fixed It.\u003C\u002Fa>","\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Finferred-type-predicates-filter-boolean\">Inferred Type Predicates Won&#39;t Save filter(Boolean). Here&#39;s What Will.\u003C\u002Fa>","\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fweekly-typescript-generics\">TypeScript Generics: The Complete Guide (with Cheat Sheet)\u003C\u002Fa>",{"id":270,"html":271,"type":138},"b41","\u003C!-- related:end -->",{"id":273,"type":274},"b42","divider",{"id":276,"html":277,"type":138},"b43","\u003Cp>🚀 \u003Cstrong>Want more like this?\u003C\u002Fstrong> Every guide, playground, and quiz lives on \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">bestpractic.org\u003C\u002Fa>\u003C\u002Fstrong> — open it and \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">sign up free\u003C\u002Fa>\u003C\u002Fstrong> so the next one finds you.\u003C\u002Fp>",{"id":279,"html":280,"type":138},"b44","\u003Cp>\u003Cem>Thanks for reading! Let&#39;s stay connected:\u003C\u002Fem>\u003C\u002Fp>",{"id":282,"type":264,"items":283,"ordered":18},"b45",[284,285,286],"⭐ \u003Cstrong>GitHub\u003C\u002Fstrong> — follow me and star the projects: \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fparsajiravand\">github.com\u002Fparsajiravand\u003C\u002Fa>","💬 \u003Cstrong>Discord\u003C\u002Fstrong> — join the frontend best-practices community: \u003Ca href=\"https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ\">discord.gg\u002Fd9KRhuAwQ\u003C\u002Fa>","📸 \u003Cstrong>Instagram\u003C\u002Fstrong> — frontend best practices, daily: \u003Ca href=\"https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F\">@bestpractice___\u003C\u002Fa>","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.\n\nI'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.\n\n## The fix that only works if everyone remembers it\n\nHere's the helper, trimmed down to the part that matters:\n\n```ts\nfunction defineEvents\u003CT extends readonly string[]>(names: T) {\n  return {\n    names,\n    on\u003CK extends T[number]>(name: K, handler: () => void) { \u002F* ... *\u002F },\n  };\n}\n\nconst bus = defineEvents(['login', 'logout', 'refresh']);\n```\n\nGuess what `T` is inside `bus` before you scroll — the literal union `'login' | 'logout' | 'refresh'`, or plain `string[]`?\n\nIt'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.\n\nThe fix I reached for first was the one from the reducer post: tell callers to add `as const`.\n\n```ts\nconst bus = defineEvents(['login', 'logout', 'refresh'] as const);\n\u002F\u002F T is now readonly [\"login\", \"logout\", \"refresh\"] — the typo-catching version\n```\n\nThat 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: `\u002F\u002F 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.\n\n## Why the widening happens at all\n\nQuick 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.\n\n## The actual fix: a `const` type parameter\n\nTypeScript 5.0 added a modifier for exactly this spot — not on the call site, on the function's own signature:\n\n```ts\nfunction defineEvents\u003Cconst T extends readonly string[]>(names: T) {\n  return {\n    names,\n    on\u003CK extends T[number]>(name: K, handler: () => void) { \u002F* ... *\u002F },\n  };\n}\n\nconst bus = defineEvents(['login', 'logout', 'refresh']);\n\u002F\u002F T is readonly [\"login\", \"logout\", \"refresh\"] — no \"as const\" anywhere\n```\n\nOne 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.\n\n\u003C!-- playground:start -->\n\n## 🎮 Try it yourself\n\n**[▶️ Open the interactive playground →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ftypescript-const-type-parameters\u002Fplayground)**\n\n_Runs right in your browser — poke at it and watch the concept react live._\n\n\u003C!-- playground:end -->\n\nThe payoff shows up downstream, in code that has nothing to do with the helper itself:\n\n```ts\nfunction label(name: 'login' | 'logout' | 'refresh'): string {\n  switch (name) {\n    case 'login': return 'Signed in';\n    case 'logout': return 'Signed out';\n    \u002F\u002F forgot 'refresh'\n  }\n}\n\u002F\u002F Error: Function lacks ending return statement, and return type\n\u002F\u002F does not include 'undefined' — because 'refresh' was never handled.\n```\n\nThat 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.\n\n## The honest limit — it only helps the moment it widens\n\n`const T` isn't magic; it only has literal information to preserve if the caller gives it a literal expression directly:\n\n```ts\nconst bus1 = defineEvents(['login', 'logout', 'refresh']); \u002F\u002F literal types preserved\n\nconst names: string[] = ['login', 'logout', 'refresh'];\nconst bus2 = defineEvents(names); \u002F\u002F T is string[] — already widened before it got here\n```\n\n`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.\n\nThe 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.\n\n\u003C!-- quiz:start -->\n\n## 🧠 Test yourself\n\nThink it clicked? **[Take the 8-question quiz →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ftypescript-const-type-parameters\u002Fquiz)**\n\n_Instant feedback, a hint on every question, and an explanation for each answer — right or wrong._\n\n\u003C!-- quiz:end -->\n\n## The takeaway\n\nThat 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.\n\nGo find the generic helper in your codebase with a `\u002F\u002F 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?\n\n\u003C!-- related:start -->\n\n## 📚 Read next\n\n- [My Reducer's Switch Stopped Being Exhaustive. `as const` Fixed It.](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ftypescript-as-const-assertions)\n- [Inferred Type Predicates Won't Save filter(Boolean). Here's What Will.](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Finferred-type-predicates-filter-boolean)\n- [TypeScript Generics: The Complete Guide (with Cheat Sheet)](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fweekly-typescript-generics)\n\n\u003C!-- related:end -->\n\n---\n\n🚀 **Want more like this?** Every guide, playground, and quiz lives on **[bestpractic.org](https:\u002F\u002Fbestpractic.org\u002F)** — open it and **[sign up free](https:\u002F\u002Fbestpractic.org\u002F)** so the next one finds you.\n\n*Thanks for reading! Let's stay connected:*\n\n- ⭐ **GitHub** — follow me and star the projects: [github.com\u002Fparsajiravand](https:\u002F\u002Fgithub.com\u002Fparsajiravand)\n- 💬 **Discord** — join the frontend best-practices community: [discord.gg\u002Fd9KRhuAwQ](https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ)\n- 📸 **Instagram** — frontend best practices, daily: [@bestpractice___](https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F)",{"title":111,"canonical":289,"description":290},"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ftypescript-const-type-parameters","A generic helper only keeps its literal types if every caller remembers to tack `as const` onto the argument — forget once and it silently widens. TypeScript's const type parameter","01a21cc5-7d63-76c4-94d1-c251b2b6a846",{"id":293,"locked":18},"01a21cc5-7df7-74e8-aff7-c8fa614b648d",[295],{"id":45,"slug":46,"title":48,"_count":296},{"questions":51},[298],{"locale":13,"slug":46},{"id":45,"slug":46,"title":48,"_count":300,"questionCount":51},{"questions":51},[]]