<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Parsa J</title>
    <description>The latest articles on DEV Community by Parsa J (@parsajiravand).</description>
    <link>https://dev.to/parsajiravand</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F99mvlsfu5tfj9m7ku25d.png</url>
      <title>DEV Community: Parsa J</title>
      <link>https://dev.to/parsajiravand</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/parsajiravand"/>
    <language>en</language>
    <item>
      <title>`use()` Breaks the Rules of Hooks on Purpose. Here's the Catch.</title>
      <dc:creator>Parsa J</dc:creator>
      <pubDate>Fri, 27 Nov 2026 11:49:31 +0000</pubDate>
      <link>https://dev.to/parsajiravand/use-breaks-the-rules-of-hooks-on-purpose-heres-the-catch-2d5i</link>
      <guid>https://dev.to/parsajiravand/use-breaks-the-rules-of-hooks-on-purpose-heres-the-catch-2d5i</guid>
      <description>&lt;p&gt;You rip out a &lt;code&gt;useEffect&lt;/code&gt;, a &lt;code&gt;useState&lt;/code&gt;, and a cleanup flag, replace all three with one line of &lt;code&gt;use()&lt;/code&gt;, and the component looks cleaner than it has in months. Then you refresh the page and the loading spinner never stops. Network tab: the same request, firing again, and again, every few milliseconds, forever.&lt;/p&gt;

&lt;p&gt;Nothing in the code is wrong, exactly. You just found the one rule &lt;code&gt;use()&lt;/code&gt; actually enforces — by being the React API that's allowed to break every other rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  The dance &lt;code&gt;use()&lt;/code&gt; was built to end
&lt;/h2&gt;

&lt;p&gt;Before &lt;code&gt;use()&lt;/code&gt;, reading an async value inside a component meant writing the same fifteen lines everywhere:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Profile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;cancelled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;cancelled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cancelled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Spinner&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UserCard&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two pieces of state tracking one value. A &lt;code&gt;cancelled&lt;/code&gt; flag so a slow response for the &lt;em&gt;old&lt;/em&gt; &lt;code&gt;userId&lt;/code&gt; doesn't overwrite a faster one for the new &lt;code&gt;userId&lt;/code&gt;. None of it is about showing a user card — it's all plumbing to make &lt;code&gt;useEffect&lt;/code&gt; behave like something it was never built to be: a way to read an async value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;use()&lt;/code&gt; reads it directly. No &lt;code&gt;useEffect&lt;/code&gt;, no &lt;code&gt;useState&lt;/code&gt;, no race-condition flag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;use&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Suspense&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;useMemo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"react"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Profile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;userPromise&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userPromise&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UserCard&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;ProfilePage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;userPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Suspense&lt;/span&gt; &lt;span class="n"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Spinner&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Profile&lt;/span&gt; &lt;span class="n"&gt;userPromise&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;userPromise&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Suspense&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;use(userPromise)&lt;/code&gt; either returns the resolved value or, if the promise is still pending, &lt;em&gt;suspends&lt;/em&gt; — the same mechanism &lt;code&gt;React.lazy&lt;/code&gt; uses. React walks up to the nearest &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; and shows its fallback instead of rendering &lt;code&gt;Profile&lt;/code&gt; with nothing. When the promise settles, React re-renders &lt;code&gt;Profile&lt;/code&gt;, and &lt;code&gt;use()&lt;/code&gt; just returns the value like it was there all along. One line replaced the whole dance, and the race-condition flag is gone because there's only one state to track: the promise.&lt;/p&gt;

&lt;p&gt;Before you scroll — does anything in that second version look like it could go wrong? Sit with it for a second.&lt;/p&gt;

&lt;h2&gt;
  
  
  I simplified it one line further. That's when it broke.
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;useMemo&lt;/code&gt; felt like an extra step, so I cut it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 🚫 looks identical, isn't&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Profile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UserCard&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the version that floods the network tab. Here's why: every time &lt;code&gt;Profile&lt;/code&gt; renders, &lt;code&gt;fetchUser(userId)&lt;/code&gt; runs again and hands &lt;code&gt;use()&lt;/code&gt; a &lt;em&gt;brand-new&lt;/em&gt; promise object — even if &lt;code&gt;userId&lt;/code&gt; hasn't changed. React has no way to know that this new promise represents "the same request, still loading." It only knows it was just handed a pending promise it's never seen before, so it suspends again. The fallback shows, the promise eventually resolves, &lt;code&gt;Profile&lt;/code&gt; re-renders, and on &lt;em&gt;that&lt;/em&gt; render it calls &lt;code&gt;fetchUser(userId)&lt;/code&gt; one more time — producing yet another new, unseen, pending promise. The loop doesn't stop because nothing ever breaks the chain.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useMemo&lt;/code&gt; was never decoration. It was the one thing making the promise's &lt;em&gt;identity&lt;/em&gt; stable across renders — the same promise object came back out every time, as long as &lt;code&gt;userId&lt;/code&gt; stayed the same — so React could recognize "I've already seen this one, it's done, here's the value" instead of "new pending work, suspend again."&lt;/p&gt;

&lt;p&gt;That's the actual rule &lt;code&gt;use()&lt;/code&gt; enforces, and it's stricter than anything &lt;code&gt;useEffect&lt;/code&gt; ever asked of you: &lt;strong&gt;the promise has to come from somewhere that caches it.&lt;/strong&gt; That's usually one of three places — a parent component that memoizes it (like the &lt;code&gt;useMemo&lt;/code&gt; above), a Server Component, where the promise can be created fresh each request because the request itself is the cache boundary, or a Suspense-integrated data library (React Query, SWR, Relay) that's already solving this for you. Creating a fresh promise inside the same component that calls &lt;code&gt;use()&lt;/code&gt; on it, every render, is the one pattern that's never safe.&lt;/p&gt;

&lt;p&gt;One thing worth knowing before you lean on the &lt;code&gt;useMemo&lt;/code&gt; version in production: React's own docs skip &lt;code&gt;useMemo&lt;/code&gt; for this and reach straight for a persistent cache (a module-level &lt;code&gt;Map&lt;/code&gt; keyed by the request, so the same promise comes back no matter what re-renders) — because &lt;code&gt;useMemo&lt;/code&gt; is documented as a performance optimization, not a guaranteed one. It works in the &lt;code&gt;ProfilePage&lt;/code&gt;/&lt;code&gt;Profile&lt;/code&gt; split above specifically because the memo lives in the &lt;em&gt;parent&lt;/em&gt;, and the parent never suspends. Move that same &lt;code&gt;useMemo&lt;/code&gt; down into the component that calls &lt;code&gt;use()&lt;/code&gt;, and the first suspend throws the memoized value away with it — recreating the exact loop you were trying to fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  🎮 Try it yourself
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://bestpractic.org/blog/react-use-api-promises-context/playground" rel="noopener noreferrer"&gt;▶️ Open the interactive playground →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Runs right in your browser — poke at it and watch the concept react live.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it's an API, not a hook
&lt;/h2&gt;

&lt;p&gt;Here's the part that's easy to miss if you only skim the React docs: &lt;code&gt;use()&lt;/code&gt; doesn't sit on the "Hooks" page with &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt;. It's listed separately, under React's general APIs — and that placement isn't an accident of alphabetizing.&lt;/p&gt;

&lt;p&gt;Every other hook comes with the Rules of Hooks: call it unconditionally, at the top level, in the same order, every render. Break that rule with &lt;code&gt;useState&lt;/code&gt; and React's internal bookkeeping desyncs — state attaches to the wrong slot. &lt;code&gt;use()&lt;/code&gt; doesn't have that problem, because it isn't tied to a slot in the same way. You can call it inside an &lt;code&gt;if&lt;/code&gt;, after an early return, or inside a loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;isLoggedIn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;messagePromise&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;isLoggedIn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;LoginPrompt&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Calling use() AFTER an early return is exactly what useState forbids.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;messagePromise&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A real hook here would be a lint error. &lt;code&gt;use()&lt;/code&gt; is fine with it, because React re-evaluates each call to &lt;code&gt;use()&lt;/code&gt; on its own, by reading from the thing you passed it, not from a fixed position in a call sequence. The same freedom applies to &lt;code&gt;use(SomeContext)&lt;/code&gt; — unlike &lt;code&gt;useContext&lt;/code&gt;, you can read it conditionally, which is genuinely useful for a component that only cares about a context value in one branch.&lt;/p&gt;

&lt;p&gt;The freedom has a boundary, though: you can't wrap &lt;code&gt;use()&lt;/code&gt; in your own &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt;. If the promise rejects, &lt;code&gt;use()&lt;/code&gt; throws, and React expects an Error Boundary above it to catch that throw — the same contract as a render that throws for any other reason. If you want to handle the rejection yourself instead, attach &lt;code&gt;.catch()&lt;/code&gt; to the promise &lt;em&gt;before&lt;/em&gt; handing it to &lt;code&gt;use()&lt;/code&gt;, so it never actually rejects on React's end.&lt;/p&gt;

&lt;h2&gt;
  
  
  The part worth keeping
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;use()&lt;/code&gt; isn't "useEffect but shorter." It's a different contract: you hand it something that eventually has a value, and it pauses the render until that value exists — but only if what you hand it stays recognizably the same thing across renders. Skip the caching and you get the one failure mode worse than the &lt;code&gt;useEffect&lt;/code&gt; dance you were trying to delete: a spinner that looks broken but is actually working exactly as told, forever.&lt;/p&gt;

&lt;p&gt;Where in your own code is a promise getting created fresh on every render because moving it up one level felt like overkill? That's the one to check first — before &lt;code&gt;use()&lt;/code&gt; finds it for you in production.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Test yourself
&lt;/h2&gt;

&lt;p&gt;Think it clicked? &lt;strong&gt;&lt;a href="https://bestpractic.org/blog/react-use-api-promises-context/quiz" rel="noopener noreferrer"&gt;Take the 6-question quiz →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  📚 Read next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/container-queries-can-check-more-than-size-style-queries-let-components-read-custom-property-3h6h"&gt;Container queries can check more than size — style queries let components read custom property values&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/youre-rethrowing-errors-and-losing-context-errorcause-fixes-that-4l97"&gt;You're rethrowing errors and losing context. &lt;code&gt;Error.cause&lt;/code&gt; fixes that.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/your-custom-hooks-tear-in-react-18-usesyncexternalstore-fixes-it-2ogd"&gt;Your Custom Hooks Tear in React 18. &lt;code&gt;useSyncExternalStore&lt;/code&gt; Fixes It.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🚀 &lt;strong&gt;Want more like this?&lt;/strong&gt; Every guide, playground, and quiz lives on &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;bestpractic.org&lt;/a&gt;&lt;/strong&gt; — open it and &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;sign up free&lt;/a&gt;&lt;/strong&gt; so the next one finds you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading! Let's stay connected:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⭐ &lt;strong&gt;GitHub&lt;/strong&gt; — follow me and star the projects: &lt;a href="https://github.com/parsajiravand" rel="noopener noreferrer"&gt;github.com/parsajiravand&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;strong&gt;Discord&lt;/strong&gt; — join the frontend best-practices community: &lt;a href="https://discord.gg/d9KRhuAwQ" rel="noopener noreferrer"&gt;discord.gg/d9KRhuAwQ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📸 &lt;strong&gt;Instagram&lt;/strong&gt; — frontend best practices, daily: &lt;a href="https://www.instagram.com/bestpractice___/" rel="noopener noreferrer"&gt;@bestpractice___&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Template Literal Types Explained: Catching Path Typos at Compile Time</title>
      <dc:creator>Parsa J</dc:creator>
      <pubDate>Fri, 27 Nov 2026 11:49:01 +0000</pubDate>
      <link>https://dev.to/parsajiravand/template-literal-types-explained-catching-path-typos-at-compile-time-4f7</link>
      <guid>https://dev.to/parsajiravand/template-literal-types-explained-catching-path-typos-at-compile-time-4f7</guid>
      <description>&lt;p&gt;Somewhere in your codebase there's a function that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;primaryColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"theme.colros.primary"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read that path again. &lt;code&gt;colros&lt;/code&gt;, not &lt;code&gt;colors&lt;/code&gt;. TypeScript compiles it without a single complaint. The app ships. &lt;code&gt;primaryColor&lt;/code&gt; is &lt;code&gt;undefined&lt;/code&gt;, some fallback kicks in, and the button is the wrong shade of blue for three weeks before a designer notices and files a ticket nobody can reproduce on the first try.&lt;/p&gt;

&lt;p&gt;The type system had everything it needed to catch this. &lt;code&gt;config&lt;/code&gt; has a real, known shape. The bug is a four-character typo in a string literal sitting right there in the call site. And TypeScript let it through anyway — because as far as the compiler is concerned, &lt;code&gt;path: string&lt;/code&gt; accepts literally any string.&lt;/p&gt;

&lt;p&gt;Here's the part that's easy to miss: fixing this isn't a runtime check. It's a type you can write yourself, today, in about fifteen lines, using a feature TypeScript has shipped since 4.1.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why &lt;code&gt;path: string&lt;/code&gt; can't save you
&lt;/h2&gt;

&lt;p&gt;The naive &lt;code&gt;get&lt;/code&gt; function above type-checks because &lt;code&gt;string&lt;/code&gt; is the type. Every string is a &lt;code&gt;string&lt;/code&gt;. TypeScript has no way to know that &lt;code&gt;"theme.colros.primary"&lt;/code&gt; isn't a real path on &lt;code&gt;config&lt;/code&gt; unless something narrower than &lt;code&gt;string&lt;/code&gt; is doing the asking.&lt;/p&gt;

&lt;p&gt;You could reach for &lt;code&gt;keyof&lt;/code&gt; to type a single level:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getTop&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;K&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;keyof&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;K&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;K&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's genuinely safe — &lt;code&gt;getTop(config, "theem")&lt;/code&gt; is a compile error. But it only goes one property deep. The moment you want &lt;code&gt;"theme.colors.primary"&lt;/code&gt;, you're back to a plain &lt;code&gt;string&lt;/code&gt;, because &lt;code&gt;keyof T&lt;/code&gt; gives you &lt;code&gt;"theme"&lt;/code&gt;, not &lt;code&gt;"theme.colors.primary"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What you actually want is a type that looks at &lt;code&gt;T&lt;/code&gt; and produces the union of &lt;em&gt;every valid dot-path through it&lt;/em&gt; — and that's exactly what template literal types, combined with recursive conditional types, build.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the path type
&lt;/h2&gt;

&lt;p&gt;Template literal types let you glue string literal types together the way you'd glue strings with &lt;code&gt;${}&lt;/code&gt; in a template string, except it happens at the type level:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`Hello, ${string}`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// matches "Hello, world", "Hello, ", but not "Hi there"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the primitive. The trick is combining it with a &lt;strong&gt;mapped type over &lt;code&gt;keyof T&lt;/code&gt;&lt;/strong&gt; that recurses into each property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Primitive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;bigint&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;symbol&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Paths&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Primitive&lt;/span&gt;
  &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="kt"&gt;never&lt;/span&gt;
  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="kt"&gt;never&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;K&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;keyof&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;K&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Primitive&lt;/span&gt;
          &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="no"&gt;K&lt;/span&gt;
          &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;K&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="sb"&gt;`${K}.${Paths&amp;lt;T[K]&amp;gt;}`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;keyof&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read it as: for every key &lt;code&gt;K&lt;/code&gt; on &lt;code&gt;T&lt;/code&gt;, if the value at that key is a primitive, the path stops there — just &lt;code&gt;K&lt;/code&gt;. If it's an object, the path is either &lt;code&gt;K&lt;/code&gt; itself or &lt;code&gt;K&lt;/code&gt; followed by a dot and a valid path &lt;em&gt;through that nested object&lt;/em&gt;. The &lt;code&gt;[keyof T &amp;amp; string]&lt;/code&gt; at the end collapses the mapped type into a plain union.&lt;/p&gt;

&lt;p&gt;Give it this &lt;code&gt;Config&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Config&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;primary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;secondary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;darkMode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Paths&amp;lt;Config&amp;gt;&lt;/code&gt; resolves to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"theme"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"theme.colors"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"theme.darkMode"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"theme.colors.primary"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"theme.colors.secondary"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every single one of those strings is a real path on a real &lt;code&gt;Config&lt;/code&gt;. There's no way to spell a typo and have it type-check, because the union was generated &lt;em&gt;from the object's own declared shape&lt;/em&gt;, not typed by hand.&lt;/p&gt;

&lt;h2&gt;
  
  
  Making &lt;code&gt;get&lt;/code&gt; use it
&lt;/h2&gt;

&lt;p&gt;The path type alone doesn't fix &lt;code&gt;get&lt;/code&gt; — you still need the return type to follow the path, or every call collapses to &lt;code&gt;unknown&lt;/code&gt;. That's a second recursive conditional type, walking the string apart instead of the object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;PathValue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;P&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;P&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="sb"&gt;`${infer Head}.${infer Rest}`&lt;/span&gt;
  &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;Head&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;keyof&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;
    &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;PathValue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Head&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Rest&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;never&lt;/span&gt;
  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;P&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;keyof&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;
    &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;P&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;never&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;P&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Paths&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;P&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PathValue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;P&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;unknown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;unknown&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;PathValue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;P&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;PathValue&lt;/code&gt; pattern-matches the string literal &lt;code&gt;P&lt;/code&gt; against &lt;code&gt;`${infer Head}.${infer Rest}`&lt;/code&gt;. If there's a dot, it peels off the first segment and recurses on the rest; once there's no dot left, it looks the final segment up with &lt;code&gt;T[P]&lt;/code&gt; directly.&lt;/p&gt;

&lt;p&gt;Now run the typo through it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"theme.colors.primary"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// string — correct, and TypeScript knows it&lt;/span&gt;
&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"theme.colros.primary"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// compile error, not a runtime surprise&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That second line doesn't run. It doesn't ship. &lt;code&gt;tsc&lt;/code&gt; stops it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nc"&gt;Argument&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="s1"&gt;'"theme.colros.primary"'&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;assignable&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;
&lt;span class="s1"&gt;'"name" | "theme.colors.primary" | "theme" | "theme.colors" | "theme.darkMode" | "theme.colors.secondary"'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I ran both versions through &lt;code&gt;tsc --strict&lt;/code&gt; before writing this — the naive one takes the typo silently, the typed one refuses to compile it. That's the entire payoff: the same information that used to surface as a confused designer three weeks later now surfaces as a red squiggle while you're still typing the call.&lt;/p&gt;

&lt;p&gt;If you've ever typed a field name into react-hook-form's &lt;code&gt;register("user.address.city")&lt;/code&gt; and had autocomplete walk you through it, you've already used a type built this same way — their &lt;code&gt;Path&amp;lt;T&amp;gt;&lt;/code&gt; helper exists to solve exactly this problem for form fields.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where this gets expensive
&lt;/h2&gt;

&lt;p&gt;Two honest limits, because a type that silently falls back to &lt;code&gt;any&lt;/code&gt; on hard cases is worse than no type at all.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Depth.&lt;/strong&gt; TypeScript caps how deeply it will recurse into a conditional type. For most app config, form, or API-response shapes — three or four levels of nesting — &lt;code&gt;Paths&amp;lt;T&amp;gt;&lt;/code&gt; resolves fast and correctly. Push it at a genuinely deep, non-recursive chain (I tested around sixty levels) and you'll hit &lt;code&gt;Type instantiation is excessively deep and possibly infinite&lt;/code&gt;. At that point the fix is a depth-limited variant (cap the recursion with a counter type), not pushing harder on the same approach.&lt;/p&gt;

&lt;p&gt;A type that references &lt;em&gt;itself&lt;/em&gt; behaves differently, and worse. Give &lt;code&gt;Paths&lt;/code&gt; an &lt;code&gt;interface TreeNode { value: string; children: TreeNode }&lt;/code&gt; and TypeScript refuses outright with a different error — &lt;code&gt;Type of property 'children' circularly references itself in mapped type&lt;/code&gt; — because the mapped type can't even start resolving a property whose type is itself. But the shape real trees actually use, &lt;code&gt;children: TreeNode[]&lt;/code&gt;, doesn't error at all: &lt;code&gt;Paths&amp;lt;T&amp;gt;&lt;/code&gt; already bails out on arrays, so &lt;code&gt;Paths&amp;lt;TreeNode&amp;gt;&lt;/code&gt; quietly resolves to just &lt;code&gt;"value" | "children"&lt;/code&gt; — technically correct, but silently blind to everything nested under &lt;code&gt;children&lt;/code&gt;. That's the failure mode worth losing sleep over: not a loud compiler error, but a type that compiles clean and just stops short of where you assumed it reached.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arrays.&lt;/strong&gt; The &lt;code&gt;Paths&amp;lt;T&amp;gt;&lt;/code&gt; above explicitly bails on arrays (&lt;code&gt;T extends readonly unknown[] ? never&lt;/code&gt;) rather than pretending to support them. Indexing into arrays by position (&lt;code&gt;items.0.name&lt;/code&gt;) is a real, solvable extension — using &lt;code&gt;`${number}`&lt;/code&gt; as a key — but it's a deliberately separate problem from object paths, and bolting it on half-finished is how you end up with a type that compiles but lies.&lt;/p&gt;

&lt;p&gt;Neither of these is a reason to skip the type. They're the reason to know, before you reach for it on a 12-level GraphQL response, that you're past where it comfortably lives.&lt;/p&gt;

&lt;h2&gt;
  
  
  🎮 Try it yourself
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://bestpractic.org/blog/typescript-template-literal-types/playground" rel="noopener noreferrer"&gt;▶️ Open the interactive playground →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Runs right in your browser — poke at it and watch the concept react live.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The next time you write &lt;code&gt;obj[a][b][c]&lt;/code&gt; with a string you typed from memory, ask yourself whether TypeScript actually checked it — or just trusted you. What's the last path typo that made it past your compiler and into production? I'd genuinely like to know I'm not the only one.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Test yourself
&lt;/h2&gt;

&lt;p&gt;Think it clicked? &lt;strong&gt;&lt;a href="https://bestpractic.org/blog/typescript-template-literal-types/quiz" rel="noopener noreferrer"&gt;Take the 8-question quiz →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;🚀 &lt;strong&gt;Want more like this?&lt;/strong&gt; Every guide, playground, and quiz lives on &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;bestpractic.org&lt;/a&gt;&lt;/strong&gt; — open it and &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;sign up free&lt;/a&gt;&lt;/strong&gt; so the next one finds you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading! Let's stay connected:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⭐ &lt;strong&gt;GitHub&lt;/strong&gt; — follow me and star the projects: &lt;a href="https://github.com/parsajiravand" rel="noopener noreferrer"&gt;github.com/parsajiravand&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;strong&gt;Discord&lt;/strong&gt; — join the frontend best-practices community: &lt;a href="https://discord.gg/d9KRhuAwQ" rel="noopener noreferrer"&gt;discord.gg/d9KRhuAwQ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📸 &lt;strong&gt;Instagram&lt;/strong&gt; — frontend best practices, daily: &lt;a href="https://www.instagram.com/bestpractice___/" rel="noopener noreferrer"&gt;@bestpractice___&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>The negotiation doesn't start until there's an offer on paper</title>
      <dc:creator>Parsa J</dc:creator>
      <pubDate>Fri, 27 Nov 2026 11:48:30 +0000</pubDate>
      <link>https://dev.to/parsajiravand/the-negotiation-doesnt-start-until-theres-an-offer-on-paper-5b3j</link>
      <guid>https://dev.to/parsajiravand/the-negotiation-doesnt-start-until-theres-an-offer-on-paper-5b3j</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;👋 &lt;strong&gt;I'm Parsa Jiravand — I work in IT, and this is Best Practice.&lt;/strong&gt; One article every day, one soft-skills episode every week. It all lives at &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;bestpractic.org&lt;/a&gt;&lt;/strong&gt; — come join us.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You get the email, or the call, and there's a number in it. Your first read of the number happens in about four seconds, and it's purely emotional — relief, mostly, because the process is over and somebody wants you. Then the second read starts, the one where you compare it to what you were hoping for, and it's a little lower, or the base is fine but the equity feels thin, or the title is one notch under what you were doing already. And right behind that thought comes the one that stops most people cold this year: &lt;em&gt;everyone says hiring is slow right now, so who am I to push back.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That sentence is doing more work than it should.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the "slow market" headlines actually tell you
&lt;/h2&gt;

&lt;p&gt;The hiring numbers this year are genuinely soft for most engineering roles — fewer companies hiring than at the peak, postings up in some places but actual hires down, and multiple people hearing the same thing from recruiters: the first number they get is lower than it would've been two or three years ago. That part's real, and it's worth taking seriously instead of assuming your own experience was a fluke.&lt;/p&gt;

&lt;p&gt;But "the market is weaker" and "you have zero leverage in this specific conversation" are two different claims, and the second one doesn't automatically follow from the first. By the time there's an offer with your name on it, something has already happened that a weak market doesn't undo: a company went through the actual cost of hiring — screening, interview loops, several people's calendars cleared, probably an internal debate about headcount — and landed on &lt;em&gt;you&lt;/em&gt; specifically. Restarting that process because you asked one question is expensive for them in a way that has nothing to do with whether hiring is hot or cold this quarter. The market sets how many offers exist. It doesn't erase the cost of throwing one away once it's been made.&lt;/p&gt;

&lt;p&gt;So the honest version isn't "you have the leverage you'd have had in 2021." It's narrower than that. It's: you have more leverage in the five minutes after an offer exists than you had at any point before it, and less than you'd have had a few years ago, and the conversation worth having lives inside that narrower room, not outside it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to actually say
&lt;/h2&gt;

&lt;p&gt;The first move is just asking, plainly, whether the number is final — and doing it in a way that doesn't read as ungrateful. Something like: &lt;em&gt;"I'm genuinely excited about this and want to make it work. Before I sign, is there any room to revisit the base, or is this number set?"&lt;/em&gt; Notice what that sentence does — it accepts the role first, in the same breath, so the question can't be read as hesitation about the job itself. A lot of people skip this question entirely because they've decided in advance that the answer is no, and skip the one conversation that might have told them otherwise.&lt;/p&gt;

&lt;p&gt;The second move matters more this year specifically, because offers have gotten more complicated at exactly the moment people are more anxious about them: ask for the breakdown. &lt;em&gt;"Could you break this down into base, bonus, and equity, so I can compare it properly against what I'd be leaving?"&lt;/em&gt; A weaker base is sometimes genuinely offset by something real in the equity or bonus structure, and sometimes it's just a weaker offer dressed in three line items instead of one. You can't tell the difference by looking at a total. You can tell by asking someone to walk you through the pieces, which also, quietly, signals you're the kind of person who reads a contract before signing it.&lt;/p&gt;

&lt;p&gt;The third move is for when the answer to the first question is actually no — the base is fixed, there's a banding system, a recruiter genuinely cannot move it. That's not where the conversation ends, it's where it changes shape: &lt;em&gt;"If the base is set, is there flexibility on sign-on bonus, start date, or title?"&lt;/em&gt; Those three levers often move even when comp bands don't, because they don't touch the number everyone in the building compares against everyone else's. A sign-on bonus is frequently the easiest yes in the building, precisely because it's a one-time cost instead of a permanent one on someone's headcount spreadsheet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where this goes wrong
&lt;/h2&gt;

&lt;p&gt;All three of those moves assume a fairly specific situation: you have an offer in hand, the role clearly wants you, and you're not in immediate financial trouble that forces you to take whatever's offered today. That's not everyone's situation, and pretending otherwise is the part of most negotiation advice that falls apart on contact with an actual bad month.&lt;/p&gt;

&lt;p&gt;It also goes wrong at the opposite end — if you're early-career right now, the honest thing to say is that your leverage in this exact conversation is genuinely thinner than it was for the same role a few years back. Entry-level hiring has dropped hard industry-wide this year, which means a recruiter turning down your counter has more candidates behind you than they would have in 2021. That doesn't mean don't ask — the first question costs you nothing and companies expect it — but it does mean the second and third moves, the ones that assume some room to maneuver, work less reliably here, and pushing hard on a number that's already at the bottom of a tight band can read as not reading the room rather than as confidence.&lt;/p&gt;

&lt;p&gt;And if a recruiter tells you directly that the number reflects a standard band for the level with no exceptions — believe them the first time. That's a different conversation than a number that was set defensively because nobody expected to be asked. Pushing on a band that's genuinely fixed doesn't get you more money, it just spends goodwill on a door that was never going to open.&lt;/p&gt;

&lt;h2&gt;
  
  
  The part that isn't the same for everyone right now
&lt;/h2&gt;

&lt;p&gt;Here's the uncomfortable bit this year specifically: leverage right now is genuinely lopsided by what you're being hired to do. People going into AI-adjacent roles, or into companies built around AI, are seeing real competition for their offers and real room to push — multiple sources this year independently describe pay climbing specifically in that lane while general engineering offers stay flat or soften. If that's you, the three moves above will probably work better than they would have a few years ago, not worse. If you're being hired into a more conventional engineering role at a company that isn't in that lane, the room is smaller, and it's worth knowing which version of this market you're actually standing in before you decide how hard to push.&lt;/p&gt;

&lt;p&gt;None of that is about who worked harder or prepared better. It's mostly about which team you ended up talking to, and that part is closer to luck than anyone likes to admit out loud in a negotiation conversation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Monday
&lt;/h2&gt;

&lt;p&gt;Before you accept — or if you already have, before the next offer comes — write down one sentence you'd actually say out loud: &lt;em&gt;"Before I sign, is there any room to revisit the base, or is this number final?"&lt;/em&gt; Say it over a real offer, not a hypothetical one. The worst answer you'll get is the one you were already expecting to hear.&lt;/p&gt;




&lt;p&gt;🚀 &lt;strong&gt;Want more like this?&lt;/strong&gt; Every guide, playground, and quiz lives on &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;bestpractic.org&lt;/a&gt;&lt;/strong&gt; — open it and &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;sign up free&lt;/a&gt;&lt;/strong&gt; so the next one finds you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading! Let's stay connected:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⭐ &lt;strong&gt;GitHub&lt;/strong&gt; — follow me and star the projects: &lt;a href="https://github.com/parsajiravand" rel="noopener noreferrer"&gt;github.com/parsajiravand&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;strong&gt;Discord&lt;/strong&gt; — join the frontend best-practices community: &lt;a href="https://discord.gg/d9KRhuAwQ" rel="noopener noreferrer"&gt;discord.gg/d9KRhuAwQ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📸 &lt;strong&gt;Instagram&lt;/strong&gt; — frontend best practices, daily: &lt;a href="https://www.instagram.com/bestpractice___/" rel="noopener noreferrer"&gt;@bestpractice___&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🤝 &lt;strong&gt;Want to make something with us?&lt;/strong&gt; Write a piece, come on the podcast, or bring an idea that should exist — &lt;strong&gt;&lt;a href="https://bestpractic.org/collaborate" rel="noopener noreferrer"&gt;bestpractic.org/collaborate&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;And if this one helped, send it to the person you know who needs it this week. That's what keeps these coming.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>career</category>
      <category>discuss</category>
      <category>productivity</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Your WebSocket Reconnects Fine. The Messages It Missed Never Do.</title>
      <dc:creator>Parsa J</dc:creator>
      <pubDate>Fri, 27 Nov 2026 11:48:00 +0000</pubDate>
      <link>https://dev.to/parsajiravand/your-websocket-reconnects-fine-the-messages-it-missed-never-do-924</link>
      <guid>https://dev.to/parsajiravand/your-websocket-reconnects-fine-the-messages-it-missed-never-do-924</guid>
      <description>&lt;p&gt;The badge in the corner goes from green to red to green again. Three seconds, tops — long enough to walk through a subway turnstile, short enough that nobody files a bug. Your reconnect logic did exactly its job: it noticed the drop, waited, and opened a fresh connection before the user even looked up.&lt;/p&gt;

&lt;p&gt;Two days later, someone on your team says "wait, I never got the update you pushed at 2:14." You check the logs. The server sent it. The client was definitely connected by 2:16. The message is just gone.&lt;/p&gt;

&lt;p&gt;It's gone because "reconnected" and "caught up" are two different claims, and the reconnect loop only ever promised the first one.&lt;/p&gt;

&lt;h2&gt;
  
  
  The reconnect code almost everyone writes first
&lt;/h2&gt;

&lt;p&gt;Here's the version that looks obviously correct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ws&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WebSocket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;handleMessage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onclose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// reconnect — done, right?&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it against a flaky connection and it works. The socket drops, &lt;code&gt;onclose&lt;/code&gt; fires, a new &lt;code&gt;WebSocket&lt;/code&gt; opens, messages flow again. Watch the status indicator and you'll see exactly what you wanted: disconnect, brief gap, reconnect.&lt;/p&gt;

&lt;p&gt;The test that never runs is the one where &lt;em&gt;the server&lt;/em&gt; is the one that went away — a deploy, a restart, a load balancer hiccup that drops every open connection at once. Now every client's &lt;code&gt;onclose&lt;/code&gt; fires within the same few hundred milliseconds, and every one of them calls &lt;code&gt;connect()&lt;/code&gt; immediately. The server comes back up to a wall of simultaneous reconnection attempts from everyone it was serving a second ago — the same retry-storm problem that shows up in any naive retry loop, just wearing a WebSocket instead of a &lt;code&gt;fetch()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Backoff and jitter fix the storm
&lt;/h2&gt;

&lt;p&gt;The fix is the same one that works for HTTP retries: wait, and don't all wait the same amount.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createReconnectingSocket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;onMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;baseDelay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maxDelay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15000&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;closedByClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ws&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WebSocket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;onMessage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onopen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onclose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;closedByClient&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// a deliberate, clean close — don't reconnect&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;windowMs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxDelay&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;baseDelay&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;attempt&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nb"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;windowMs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// full jitter — spread attempts, don't synchronize them&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;closedByClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"client closed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details carry the fix:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;event.code === 1000&lt;/code&gt;&lt;/strong&gt; is a clean, intentional close — the server said goodbye on purpose, or you called &lt;code&gt;.close()&lt;/code&gt; yourself. Reconnecting after that would fight your own shutdown logic. Everything else — including &lt;code&gt;1006&lt;/code&gt;, the code the browser assigns when the connection just vanishes with no close frame at all — is the "something broke, try again" case.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Math.random() * windowMs&lt;/code&gt;&lt;/strong&gt; is full jitter: instead of every client waiting exactly 500ms, then exactly 1000ms, then exactly 2000ms (which just re-synchronizes everyone into new, smaller storms), each client picks a random point inside the growing window. The server sees a trickle of reconnects instead of a wave.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🎮 Try it yourself
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://bestpractic.org/blog/websocket-reconnect-exponential-backoff/playground" rel="noopener noreferrer"&gt;▶️ Open the interactive playground →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Runs right in your browser — poke at it and watch the concept react live.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Watch six simulated clients reconnect after a shared outage, with and without jitter, and the storm either does or doesn't happen in front of you.&lt;/p&gt;

&lt;p&gt;Ship that, watch the status line go disconnected → reconnecting → connected smoothly, and it's tempting to call this done. The UI agrees with you. That's exactly the problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reconnecting is not resuming
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;WebSocket&lt;/code&gt; has no concept of "pick up where we left off." When &lt;code&gt;connect()&lt;/code&gt; runs again, it isn't un-pausing the old socket — it's opening a completely new one, with its own handshake, that the server has never seen before. There is no frame in the spec for "resend me everything since sequence 41." If your server tried to &lt;code&gt;send()&lt;/code&gt; a message to a socket that was already closed, that call either no-ops or throws — either way, the message isn't queued anywhere waiting for you to come back.&lt;/p&gt;

&lt;p&gt;So picture the actual timeline from the hook above: connection drops at 2:14:02, your server pushes an update at 2:14:05 to a socket that no longer exists on the other end, your client reconnects at 2:14:06. The new connection is healthy. The update from 2:14:05 was never sent to &lt;em&gt;this&lt;/em&gt; socket, because &lt;em&gt;this&lt;/em&gt; socket didn't exist yet when it was sent. Nothing errored. Nothing logged a failure. The badge is green. The data just isn't there.&lt;/p&gt;

&lt;p&gt;This is the gap every "add exponential backoff and you're done" tutorial skips, because backoff genuinely does solve the problem it's aimed at — the storm. It was never aimed at message loss, and a smooth-looking reconnect makes that easy to forget.&lt;/p&gt;

&lt;h2&gt;
  
  
  The actual fix: let the client ask what it missed
&lt;/h2&gt;

&lt;p&gt;Recovering the gap needs a contract the WebSocket protocol doesn't give you for free — so you build a small one on top of it. The shape is always the same three pieces:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Every message the server pushes carries an increasing sequence number.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The client remembers the last one it successfully processed.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;On reconnect, the client asks for everything after that number, before it trusts the new connection as caught up.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;lastSeq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"resume-complete"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// see below&lt;/span&gt;
  &lt;span class="n"&gt;lastSeq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lastSeq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seq&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;onSocketOpen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"resume"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;since&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;lastSeq&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server's half of the deal is the part that actually matters: it needs to keep a short rolling buffer per connection (or per user) of recently sent messages, so a &lt;code&gt;resume&lt;/code&gt; request can be answered by replaying the ones after &lt;code&gt;since&lt;/code&gt;. That buffer can't be infinite — if a client comes back after being offline for an hour and the server only kept five minutes of history, the honest answer is &lt;em&gt;not&lt;/em&gt; to silently pretend nothing was missed. It's to tell the client its &lt;code&gt;since&lt;/code&gt; is too old and trigger a full resync (a normal REST fetch of current state), the same way a CDN tells you a cache entry expired instead of serving stale data as if it were fresh.&lt;/p&gt;

&lt;p&gt;That buffer-and-replay logic lives on the server and is genuinely more work than the client-side backoff loop. It's also the only part of this that actually answers "did I get everything," which is the question the green badge was never answering in the first place.&lt;/p&gt;

&lt;h2&gt;
  
  
  One more thing backoff doesn't catch: the socket that's dead but doesn't know it
&lt;/h2&gt;

&lt;p&gt;There's a second failure mode worth closing out, because it breaks the resume logic above in a sneaky way: a laptop goes to sleep, or a phone's network stack silently drops a connection without telling the browser. &lt;code&gt;ws.readyState&lt;/code&gt; can report &lt;code&gt;OPEN&lt;/code&gt; for a socket that network hardware gave up on minutes ago — no &lt;code&gt;close&lt;/code&gt; event fires, so none of the reconnect logic above ever triggers, because as far as your JavaScript knows, nothing happened.&lt;/p&gt;

&lt;p&gt;The browser's &lt;code&gt;WebSocket&lt;/code&gt; object gives you no way to send a protocol-level ping to check — there's no &lt;code&gt;.ping()&lt;/code&gt; method in the standard; that exists on server-side libraries like Node's &lt;code&gt;ws&lt;/code&gt; package, not in the browser API. So you build the check into the one channel you do control: your own messages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;startHeartbeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;pongTimer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;pingTimer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readyState&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nc"&gt;WebSocket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;OPEN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ping"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;pongTimer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// no pong in time — force the issue&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;onPong&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pongTimer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pingTimer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nf"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pongTimer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server replies &lt;code&gt;{ type: "pong" }&lt;/code&gt; to every ping it gets. No reply within the timeout, and you call &lt;code&gt;ws.close()&lt;/code&gt; yourself — which hands the problem back to the reconnect loop you already wrote, instead of leaving the app sitting on a socket that looks fine and isn't.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Test yourself
&lt;/h2&gt;

&lt;p&gt;Think it clicked? &lt;strong&gt;&lt;a href="https://bestpractic.org/blog/websocket-reconnect-exponential-backoff/quiz" rel="noopener noreferrer"&gt;Take the 8-question quiz →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What "connected" actually means now
&lt;/h2&gt;

&lt;p&gt;Three pieces, each fixing a different lie the green badge was telling: backoff-with-jitter stops reconnects from synchronizing into a storm, a sequence number and a resume handshake stop a clean reconnect from quietly hiding data loss, and an app-level heartbeat stops a socket that's dead from pretending it's fine. None of them are hard individually — the entire reconnect-plus-resume contract is maybe 60 lines. The hard part is noticing that "it reconnects" was never the same claim as "it didn't lose anything," right up until a teammate asks where their message went.&lt;/p&gt;

&lt;p&gt;Does your reconnect logic actually ask the server what it missed — or does it just trust the green dot the same way everyone's first version did?&lt;/p&gt;

&lt;h2&gt;
  
  
  📚 Read next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/stop-retrying-immediately-exponential-backoff-fixes-it-1hce"&gt;Stop Retrying Immediately. Exponential Backoff Fixes It.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/stop-hardcoding-color-variants-relative-color-syntax-does-it-1f8k"&gt;Stop Hardcoding Color Variants. Relative Color Syntax Does It.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/youre-debouncing-scroll-wrong-requestanimationframe-fixes-it-2pa5"&gt;You're Debouncing Scroll Wrong. requestAnimationFrame Fixes It.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🚀 &lt;strong&gt;Want more like this?&lt;/strong&gt; Every guide, playground, and quiz lives on &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;bestpractic.org&lt;/a&gt;&lt;/strong&gt; — open it and &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;sign up free&lt;/a&gt;&lt;/strong&gt; so the next one finds you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading! Let's stay connected:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⭐ &lt;strong&gt;GitHub&lt;/strong&gt; — follow me and star the projects: &lt;a href="https://github.com/parsajiravand" rel="noopener noreferrer"&gt;github.com/parsajiravand&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;strong&gt;Discord&lt;/strong&gt; — join the frontend best-practices community: &lt;a href="https://discord.gg/d9KRhuAwQ" rel="noopener noreferrer"&gt;discord.gg/d9KRhuAwQ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📸 &lt;strong&gt;Instagram&lt;/strong&gt; — frontend best practices, daily: &lt;a href="https://www.instagram.com/bestpractice___/" rel="noopener noreferrer"&gt;@bestpractice___&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Next.js 16 use cache: remote vs use cache: private</title>
      <dc:creator>Parsa J</dc:creator>
      <pubDate>Fri, 27 Nov 2026 11:47:29 +0000</pubDate>
      <link>https://dev.to/parsajiravand/nextjs-16-use-cache-remote-vs-use-cache-private-5amm</link>
      <guid>https://dev.to/parsajiravand/nextjs-16-use-cache-remote-vs-use-cache-private-5amm</guid>
      <description>&lt;p&gt;A team ships personalized product recommendations on a product page. The data depends on the visitor's session cookie, so a teammate wraps the fetch in a plain &lt;code&gt;'use cache'&lt;/code&gt; function and reads &lt;code&gt;cookies()&lt;/code&gt; inside it. &lt;code&gt;next build&lt;/code&gt; passes. &lt;code&gt;next start&lt;/code&gt; doesn't: every request to that route throws &lt;code&gt;next-request-in-use-cache&lt;/code&gt;, because &lt;code&gt;use cache&lt;/code&gt; cannot see request-time APIs at all, by design.&lt;/p&gt;

&lt;p&gt;The fix looks obvious: switch to &lt;code&gt;'use cache: private'&lt;/code&gt;, since the docs say it's the directive that can read cookies. It compiles, it renders, the recommendations are personalized again — and the team, having just learned there's a "private" cache, applies it everywhere else that used to be a plain &lt;code&gt;fetch&lt;/code&gt;, including the currency-converted price that's genuinely the same for every visitor paying in USD. Three weeks later, the pricing API is getting hammered on every cold start in production, because &lt;code&gt;'use cache: private'&lt;/code&gt; was never meant to deduplicate shared data across users — it isn't that kind of cache at all.&lt;/p&gt;

&lt;p&gt;This is the mix-up this article exists to prevent. Next.js 16 ships &lt;strong&gt;three&lt;/strong&gt; caching directives under Cache Components — &lt;code&gt;use cache&lt;/code&gt;, &lt;code&gt;'use cache: private'&lt;/code&gt;, and &lt;code&gt;'use cache: remote'&lt;/code&gt; — and they answer three different questions, not one question three ways. This article is written against &lt;strong&gt;Next.js 16.4.6&lt;/strong&gt; (verified against the framework's own bundled documentation and npm's &lt;code&gt;latest&lt;/code&gt; dist-tag in November 2026); both private and remote caches were introduced and stabilized together with Cache Components in Next.js 16.0.0.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you'll learn
&lt;/h2&gt;

&lt;p&gt;By the end of this article you'll be able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explain what each of the three &lt;code&gt;use cache&lt;/code&gt; directives actually stores, and where&lt;/li&gt;
&lt;li&gt;Pick the right one for personalized data, for shared-but-expensive data, and for everything else&lt;/li&gt;
&lt;li&gt;Read the nesting rules that decide which directives can wrap which&lt;/li&gt;
&lt;li&gt;Avoid the two opposite mistakes: caching personal data where it could leak, and caching shared data where it never gets reused&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Who this is for
&lt;/h2&gt;

&lt;p&gt;You're already caching something with plain &lt;code&gt;'use cache'&lt;/code&gt; — or you've read &lt;a href="https://dev.to/parsajiravand/nextjs-cache-components-explained-with-cheat-sheet-55ob"&gt;Next.js Cache Components Explained&lt;/a&gt;, which builds that foundation from nothing. This article assumes that part and goes one level deeper, into the two directives that exist specifically for data &lt;code&gt;use cache&lt;/code&gt; can't handle. You don't need to have read it first; the rules below stand on their own.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The problem: one cache, three jobs it can't all do&lt;/li&gt;
&lt;li&gt;The mental model: scope and storage are two different dials&lt;/li&gt;
&lt;li&gt;Stage 1: the baseline — what plain &lt;code&gt;use cache&lt;/code&gt; won't do&lt;/li&gt;
&lt;li&gt;Stage 2: &lt;code&gt;use cache: private&lt;/code&gt; — per-request, never shared&lt;/li&gt;
&lt;li&gt;Stage 3: &lt;code&gt;use cache: remote&lt;/code&gt; — shared across every instance&lt;/li&gt;
&lt;li&gt;Stage 4: the nesting rules&lt;/li&gt;
&lt;li&gt;Edge cases and gotchas&lt;/li&gt;
&lt;li&gt;Best practices&lt;/li&gt;
&lt;li&gt;FAQ&lt;/li&gt;
&lt;li&gt;Cheat sheet&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The problem: one cache, three jobs it can't all do
&lt;/h2&gt;

&lt;p&gt;Plain &lt;code&gt;use cache&lt;/code&gt; does two jobs at once: it tells Next.js what's safe to put in a route's prerendered &lt;strong&gt;static shell&lt;/strong&gt;, and it caches a function's result so repeat calls with the same inputs skip the work. Both jobs assume the same thing — that the cached value is identical for every visitor who triggers the same inputs. That assumption breaks the moment a value depends on &lt;em&gt;who's asking&lt;/em&gt;, which is exactly why &lt;code&gt;use cache&lt;/code&gt; refuses to let you read &lt;code&gt;cookies()&lt;/code&gt;, &lt;code&gt;headers()&lt;/code&gt;, or &lt;code&gt;searchParams&lt;/code&gt; inside it: there'd be no safe single value to put in a shell every visitor shares.&lt;/p&gt;

&lt;p&gt;So the framework splits the "who's asking" problem into two separate directives, one for each half of it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sometimes the answer genuinely differs &lt;strong&gt;per visitor&lt;/strong&gt; and must never be handed to anyone else — that's &lt;code&gt;'use cache: private'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Sometimes the answer is the same for &lt;strong&gt;many&lt;/strong&gt; visitors (same currency, same language, same product category) but you still need to defer reading it until request time, and the default in-memory cache isn't durable enough across your server instances — that's &lt;code&gt;'use cache: remote'&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Neither one is "the private version" or "the bigger version" of the other. They answer different questions, and reaching for the wrong one either leaks nothing useful (over-caching data that was never personal) or hammers your backend (under-caching data that should have been shared).&lt;/p&gt;

&lt;h2&gt;
  
  
  The mental model: scope and storage are two different dials
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The mental model:&lt;/strong&gt; every cache in Next.js is the product of two independent choices — who the cached value is allowed to be &lt;em&gt;shared with&lt;/em&gt; (scope), and &lt;em&gt;where it physically lives&lt;/em&gt; (storage). Conflating these two dials is the whole source of confusion here.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;use cache&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;'use cache: remote'&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;'use cache: private'&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Scope&lt;/strong&gt; — who shares one entry&lt;/td&gt;
&lt;td&gt;Everyone&lt;/td&gt;
&lt;td&gt;Everyone&lt;/td&gt;
&lt;td&gt;Nobody but this one request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Storage&lt;/strong&gt; — where it lives&lt;/td&gt;
&lt;td&gt;In-memory (or a configured handler)&lt;/td&gt;
&lt;td&gt;A remote cache handler (Redis, KV, …)&lt;/td&gt;
&lt;td&gt;Nowhere persistent — not saved across requests in production&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Can read &lt;code&gt;cookies()&lt;/code&gt; / &lt;code&gt;headers()&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Read the table by column, not by row: &lt;code&gt;use cache&lt;/code&gt; and &lt;code&gt;'use cache: remote'&lt;/code&gt; share the same scope (everyone) and differ only in storage durability. &lt;code&gt;'use cache: private'&lt;/code&gt; is the odd one out on scope — it exists purely to let request-specific code participate in caching at all, and the tradeoff for that access is that nothing it produces gets saved across requests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 1: the baseline — what plain &lt;code&gt;use cache&lt;/code&gt; won't do
&lt;/h2&gt;

&lt;p&gt;A quick recap, since both of today's directives are defined by contrast to this one. A plain cached function looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/lib/pricing.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cacheLife&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next/cache'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getBasePrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s1"&gt;'use cache'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;cacheLife&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hours'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is shared across every visitor who asks for the same &lt;code&gt;productId&lt;/code&gt;, and it can contribute to the route's static shell. The constraint: this function, and anything it calls, cannot touch &lt;code&gt;cookies()&lt;/code&gt;, &lt;code&gt;headers()&lt;/code&gt;, or &lt;code&gt;searchParams&lt;/code&gt; — doing so throws &lt;code&gt;next-request-in-use-cache&lt;/code&gt; the moment the route actually runs, even though &lt;code&gt;next build&lt;/code&gt; may not have caught it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key concept:&lt;/strong&gt; &lt;code&gt;use cache&lt;/code&gt; isn't refusing to be "dynamic" out of caution — it genuinely has nowhere safe to put a per-visitor answer. That gap is exactly what the next two directives exist to fill, each for a different reason.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 2: &lt;code&gt;use cache: private&lt;/code&gt; — per-request, never shared
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;'use cache: private'&lt;/code&gt; lifts the ban on request-time APIs, at the cost of never sharing the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/product/[id]/page.tsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Suspense&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'react'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cookies&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next/headers'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cacheLife&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cacheTag&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next/cache'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;ProductPage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Suspense&lt;/span&gt; &lt;span class="n"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt; &lt;span class="n"&gt;recommendations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Recommendations&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Suspense&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;Recommendations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;sessionId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;cookies&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'session-id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s1"&gt;'guest'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;recs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getRecommendations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sessionId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;RecList&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;recs&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getRecommendations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sessionId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s1"&gt;'use cache: private'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;cacheTag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`recommendations-${productId}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;cacheLife&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;stale&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recommendations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;personalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sessionId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In production, calling this function twice &lt;em&gt;within the same request&lt;/em&gt; can reuse the result — that's request-scoped deduplication, not a server cache. It is never written to a cache that a later request, from the same visitor or anyone else, can read. In development, Next.js does keep it in memory, keyed by that request's cookies and headers, so you can watch it serve a near-identical dev request from cache — which is useful for iterating locally, but isn't the production behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key concept:&lt;/strong&gt; the &lt;code&gt;stale&lt;/code&gt; time you set with &lt;code&gt;cacheLife&lt;/code&gt; governs the &lt;em&gt;client-side&lt;/em&gt; router cache, not a server store. Private cache functions are excluded from static-shell generation entirely, so this value only controls how long the browser can reuse what it already streamed down. The docs call out a specific threshold: &lt;code&gt;stale&lt;/code&gt; must be at least 30 seconds for per-link prefetching to work, and at least 5 minutes for the content to be eligible for the route's App Shell. If a function only needs within-request dedup and shouldn't shorten the route's overall client stale time, set &lt;code&gt;cacheLife({ stale: Infinity })&lt;/code&gt; — that opts it out of influencing the route's shortest stale time without claiming server persistence it was never going to get.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 3: &lt;code&gt;use cache: remote&lt;/code&gt; — shared across every instance
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;'use cache: remote'&lt;/code&gt; solves a completely different problem: &lt;code&gt;use cache&lt;/code&gt;'s default in-memory store is just that — in-memory, per instance. In a serverless deployment, each invocation can land on a different, short-lived instance, so the in-memory cache barely helps; every cold instance re-runs the work and re-hits your backend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/product/[id]/page.tsx&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Suspense&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'react'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cookies&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next/headers'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cacheTag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cacheLife&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;'next/cache'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nc"&gt;ProductPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Reading cookies defers this component to request time&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;currency&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;cookies&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'currency'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s1"&gt;'USD'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getProductPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;currency&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nc"&gt;Price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;currency&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getProductPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;currency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s1"&gt;'use cache: remote'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;cacheTag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`product-price-${productId}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;cacheLife&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;expire&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 1 hour&lt;/span&gt;

  &lt;span class="c1"&gt;// Cached per (productId, currency) — a handful of currencies, not one entry per visitor&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;currency&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice what's &lt;code&gt;'use cache: remote'&lt;/code&gt;, not &lt;code&gt;'use cache: private'&lt;/code&gt;, in this example: the price depends on &lt;code&gt;currency&lt;/code&gt;, which is read from a cookie, but currency is shared across potentially thousands of visitors. Caching it as "private" would create one cache miss per visitor for data that's identical across a whole currency; caching it as "remote" creates one shared entry per currency and lets every server instance hit that same entry.&lt;/p&gt;

&lt;p&gt;A remote cache needs a real handler — Redis, a KV store, anything configured through &lt;a href="https://nextjs.org/docs/app/api-reference/config/next-config-js/cacheHandlers" rel="noopener noreferrer"&gt;&lt;code&gt;cacheHandlers&lt;/code&gt;&lt;/a&gt; — which most hosting providers supply automatically, and which you configure yourself if you're self-hosting. That's the tradeoff &lt;code&gt;'use cache: remote'&lt;/code&gt; is explicit about: infrastructure cost and a network round trip on every lookup, in exchange for a cache that's actually shared once a request falls outside the static shell.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key concept:&lt;/strong&gt; reach for &lt;code&gt;'use cache: remote'&lt;/code&gt; only when the dimension you're caching on has &lt;em&gt;few&lt;/em&gt; distinct values relative to your traffic (currencies, languages, categories — not user IDs or arbitrary search filters). The docs frame this as finding "the dimension with fewer unique values" and caching on that, then filtering the rest in memory. Cache on &lt;code&gt;category&lt;/code&gt;, not on &lt;code&gt;category + minPrice&lt;/code&gt;, and the hit rate difference is the entire point of paying for a remote handler at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 4: the nesting rules
&lt;/h2&gt;

&lt;p&gt;Because remote and private caches solve opposite problems, Next.js enforces where each can appear relative to the other:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A remote cache &lt;strong&gt;can&lt;/strong&gt; nest inside another remote cache, or inside a plain &lt;code&gt;use cache&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A remote cache &lt;strong&gt;cannot&lt;/strong&gt; nest inside a private cache, in either direction.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// VALID — remote inside a plain cache&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;outerCache&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s1"&gt;'use cache'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;innerRemote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// fine — resolved when deferred to request time&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// INVALID — remote inside private, or private inside remote, throws&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;outerPrivate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s1"&gt;'use cache: private'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;innerRemote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rule follows directly from the mental model: a private cache's entire contract is "nothing here gets shared." Letting a shared remote cache live inside it (or vice versa) would mean one of the two guarantees has to break. Next.js enforces the rule at the boundary instead of leaving it to be discovered in production.&lt;/p&gt;

&lt;h2&gt;
  
  
  Edge cases and gotchas
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Remote caches don't survive a deploy.&lt;/strong&gt; The cache key for &lt;code&gt;'use cache: remote'&lt;/code&gt; includes your build ID (or &lt;code&gt;deploymentId&lt;/code&gt;, if you've configured one), so a new build produces entirely new keys. This is intentional — a refactored function or an upgraded dependency can quietly change what a cache entry means, and reusing an old entry under a new build risks serving data that doesn't match what the new code expects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Draft Mode bypasses &lt;code&gt;use cache&lt;/code&gt;, no special case needed.&lt;/strong&gt; The docs are explicit about plain &lt;code&gt;use cache&lt;/code&gt;: with Draft Mode enabled, cached functions and components re-execute on every request and their results aren't saved to the cache. &lt;code&gt;'use cache: private'&lt;/code&gt; already never persists across requests in production, so Draft Mode doesn't change anything there either way. The docs don't make the same statement by name for &lt;code&gt;'use cache: remote'&lt;/code&gt; — don't assume it behaves identically without checking your own cache handler.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;connection()&lt;/code&gt; is off-limits in both &lt;code&gt;use cache&lt;/code&gt; and &lt;code&gt;'use cache: private'&lt;/code&gt;.&lt;/strong&gt; It's easy to assume "private" means "anything request-related is now fair game," but &lt;code&gt;connection()&lt;/code&gt; specifically carries connection-level information that isn't safe to cache even per-request, so it stays prohibited in both.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You can't configure a custom handler for &lt;code&gt;'use cache: private'&lt;/code&gt;.&lt;/strong&gt; Unlike &lt;code&gt;use cache&lt;/code&gt; and &lt;code&gt;'use cache: remote'&lt;/code&gt;, there's no &lt;code&gt;cacheHandlers&lt;/code&gt; hook for the private directive — by design, since it's defined by &lt;em&gt;not&lt;/em&gt; persisting anywhere durable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"Private" and "needs a database row per user" aren't the same problem.&lt;/strong&gt; If what you actually need is data that survives across a user's many requests (not just within one), none of these three directives is the tool — that's your own data layer, read directly or behind a conventional cache.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Default to plain &lt;code&gt;use cache&lt;/code&gt; for anything that's genuinely identical for every visitor. It's the cheapest of the three — no extra infrastructure, and it's eligible for the static shell.&lt;/li&gt;
&lt;li&gt;Reach for &lt;code&gt;'use cache: remote'&lt;/code&gt; only once you've confirmed the dimension you're caching on has low cardinality (a handful of currencies or categories, not one value per user) and the operation is slow or rate-limited enough to justify a network round trip to check.&lt;/li&gt;
&lt;li&gt;Reach for &lt;code&gt;'use cache: private'&lt;/code&gt; only for data that's genuinely personal and either can't be refactored to read runtime values outside the cached scope, or has a real compliance reason it must never land in a shared store — the docs call this the narrow case, not the default.&lt;/li&gt;
&lt;li&gt;Before adding any directive, ask whether the runtime value can simply be read &lt;em&gt;outside&lt;/em&gt; the cached function and passed in as an argument. That's the preferred pattern for both &lt;code&gt;use cache&lt;/code&gt; and &lt;code&gt;'use cache: remote'&lt;/code&gt;, and it often removes the need for &lt;code&gt;'use cache: private'&lt;/code&gt; entirely.&lt;/li&gt;
&lt;li&gt;Set an explicit &lt;code&gt;cacheLife&lt;/code&gt; on every cache you write. The implicit &lt;code&gt;default&lt;/code&gt; profile (5 minutes client-side stale, 15 minutes server-side revalidate) is a reasonable fallback, but it hides the lifetime at the call site — and for private caches specifically, forgetting it means forgetting to decide whether this function should shorten the whole route's client stale time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🎮 Try it yourself
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://bestpractic.org/blog/nextjs-weekly-use-cache-remote-vs-private/playground" rel="noopener noreferrer"&gt;▶️ Open the interactive playground →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Runs right in your browser — poke at it and watch the concept react live.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;use cache&lt;/code&gt;, &lt;code&gt;'use cache: private'&lt;/code&gt;, and &lt;code&gt;'use cache: remote'&lt;/code&gt; answer three different questions — shared/durable, personal/ephemeral, and shared/durable-but-deferred — not three strengths of the same cache.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'use cache: private'&lt;/code&gt; lets you read &lt;code&gt;cookies()&lt;/code&gt;, &lt;code&gt;headers()&lt;/code&gt;, and &lt;code&gt;searchParams&lt;/code&gt; inside a cached scope, but in production it never persists past the request that created it. It solves an &lt;em&gt;access&lt;/em&gt; problem, not a &lt;em&gt;sharing&lt;/em&gt; problem.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'use cache: remote'&lt;/code&gt; solves the opposite problem: it shares a cache entry across every server instance, using a real handler, at the cost of infrastructure and network latency — and it only pays off when the cached dimension has few distinct values relative to your traffic.&lt;/li&gt;
&lt;li&gt;Remote and private caches cannot nest inside each other in either direction, because their guarantees are mutually exclusive.&lt;/li&gt;
&lt;li&gt;The cheapest fix for "I need runtime data in a cached function" is still reading it outside the cache and passing it as an argument — reach for &lt;code&gt;private&lt;/code&gt; or &lt;code&gt;remote&lt;/code&gt; only once that's genuinely not an option.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That team from the opening: the actual fix wasn't choosing between &lt;code&gt;'use cache: private'&lt;/code&gt; and &lt;code&gt;'use cache: remote'&lt;/code&gt; for the pricing function — it was recognizing that currency isn't personal data at all. The recommendations stayed on &lt;code&gt;'use cache: private'&lt;/code&gt;, keyed per session, exactly as personal data should be. The price moved to &lt;code&gt;'use cache: remote'&lt;/code&gt;, keyed on currency — a few dozen cache entries instead of one cache miss per visitor, and their pricing API went back to seeing the traffic it was built for.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Test yourself
&lt;/h2&gt;

&lt;p&gt;Think it clicked? &lt;strong&gt;&lt;a href="https://bestpractic.org/blog/nextjs-weekly-use-cache-remote-vs-private/quiz" rel="noopener noreferrer"&gt;Take the 8-question quiz →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Can I use &lt;code&gt;'use cache: remote'&lt;/code&gt; and &lt;code&gt;'use cache: private'&lt;/code&gt; together?
&lt;/h3&gt;

&lt;p&gt;Not nested inside one another, in either direction — Next.js rejects that at the boundary. You can use both in the same route, on different functions, as the pricing-and-recommendations example above does; you just can't wrap one directive's scope inside the other's.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does &lt;code&gt;'use cache: remote'&lt;/code&gt; persist across deployments?
&lt;/h3&gt;

&lt;p&gt;No. Its cache key includes your build ID (or &lt;code&gt;deploymentId&lt;/code&gt; if configured), so every new deploy invalidates every entry by construction. If you need data that survives a deploy, that's a job for &lt;code&gt;unstable_cache&lt;/code&gt; (for non-&lt;code&gt;fetch&lt;/code&gt; functions) or the &lt;code&gt;fetch&lt;/code&gt; cache, not either of today's two directives.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why doesn't my &lt;code&gt;'use cache: private'&lt;/code&gt; data show up for a different request?
&lt;/h3&gt;

&lt;p&gt;Because that's the entire point of the directive. It isn't a bug or a cold-cache miss — &lt;code&gt;'use cache: private'&lt;/code&gt; is explicitly designed to never share its result across requests in production. If you need the same computed value available to a second request, you want &lt;code&gt;use cache&lt;/code&gt; or &lt;code&gt;'use cache: remote'&lt;/code&gt; instead, with the request-specific part extracted and passed in as a plain argument.&lt;/p&gt;

&lt;h3&gt;
  
  
  How is &lt;code&gt;'use cache: remote'&lt;/code&gt; different from &lt;code&gt;unstable_cache()&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;Both can persist data outside the in-memory default, but they come from different eras of the same problem. &lt;code&gt;unstable_cache()&lt;/code&gt; predates Cache Components, has its own key-building rules (notably, no awareness of root params), and the docs now point most use cases toward &lt;code&gt;use cache&lt;/code&gt; and its Cache-Components-era siblings instead. If you're still using &lt;code&gt;unstable_cache()&lt;/code&gt; for something that could be a cached function today, &lt;a href="https://dev.to/parsajiravand/nextjs-1645-how-unstablecache-leaked-data-across-root-params-1om0"&gt;a cold-start cache-key leak between the two is worth reading about&lt;/a&gt; before you decide which one to keep.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do I have to set up &lt;code&gt;cacheHandlers&lt;/code&gt; myself to use &lt;code&gt;'use cache: remote'&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;Usually not — most hosting providers wire up a default remote handler for you. You only need to configure &lt;code&gt;cacheHandlers&lt;/code&gt; yourself if you're self-hosting and want remote caching, or if you want to swap in a specific store (Redis, a particular KV service) instead of whatever your platform provides by default.&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens if I forget &lt;code&gt;cacheLife&lt;/code&gt; on a private or remote cache?
&lt;/h3&gt;

&lt;p&gt;The implicit &lt;code&gt;default&lt;/code&gt; profile applies: 5 minutes &lt;code&gt;stale&lt;/code&gt; (client-side), 15 minutes &lt;code&gt;revalidate&lt;/code&gt; (server-side), and no time-based &lt;code&gt;expire&lt;/code&gt;. It's a safe fallback, but for a private cache specifically, it also means you haven't deliberately decided whether this function should be allowed to shorten the whole route's client-side stale time — set it explicitly once you know the answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NEXT.JS 16 CACHE DIRECTIVES — WHICH ONE?

                         use cache     use cache: remote   use cache: private
Shared across users?       yes              yes                  no
Can read cookies/headers?  no               no                   yes
Where it's stored          in-memory*       remote handler       nowhere durable
Survives a deploy?         no               no                   n/a
Needs cacheHandlers?       optional         yes (or platform)    not configurable
Contributes to static shell? yes            when static          never

* or a configured handler — see cacheHandlers

DECISION ORDER (ask in this order, stop at the first "yes"):
1. Can I read the runtime value OUTSIDE the cache and pass it in as an argument?
   → yes: use plain `use cache`. Done — you probably don't need private or remote.
2. Is the value genuinely personal / per-user, with no low-cardinality dimension
   to cache on instead?
   → yes: `use cache: private`. Set cacheLife({ stale: Infinity }) if it's pure
     within-request dedup and shouldn't shorten the route's client stale time.
3. Is the value shared across many users, deferred to request time, and worth
   paying infra + latency for a real cache hit (slow/rate-limited/expensive)?
   → yes: `use cache: remote`, keyed on the LOW-cardinality dimension
     (currency, locale, category) — never on something unique per request.
4. Otherwise: plain `use cache` with an explicit cacheLife.

NESTING
  remote  inside remote   -&amp;gt; OK
  remote  inside use cache -&amp;gt; OK
  remote  inside private   -&amp;gt; ERROR
  private inside remote    -&amp;gt; ERROR

next.config.ts (required for all three)
  export default {
    cacheComponents: true,
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  📚 Read next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/nextjs-cache-components-explained-with-cheat-sheet-55ob"&gt;Next.js Cache Components Explained (with Cheat Sheet)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/nextjs-1645-how-unstablecache-leaked-data-across-root-params-1om0"&gt;Next.js 16.4.5: How unstable_cache Leaked Data Across Root Params&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/nextjs-proxyts-explained-with-cheat-sheet-j4i"&gt;Next.js proxy.ts Explained (with Cheat Sheet)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🚀 &lt;strong&gt;Want more like this?&lt;/strong&gt; Every guide, playground, and quiz lives on &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;bestpractic.org&lt;/a&gt;&lt;/strong&gt; — open it and &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;sign up free&lt;/a&gt;&lt;/strong&gt; so the next one finds you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading! Let's stay connected:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⭐ &lt;strong&gt;GitHub&lt;/strong&gt; — follow me and star the projects: &lt;a href="https://github.com/parsajiravand" rel="noopener noreferrer"&gt;github.com/parsajiravand&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;strong&gt;Discord&lt;/strong&gt; — join the frontend best-practices community: &lt;a href="https://discord.gg/d9KRhuAwQ" rel="noopener noreferrer"&gt;discord.gg/d9KRhuAwQ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📸 &lt;strong&gt;Instagram&lt;/strong&gt; — frontend best practices, daily: &lt;a href="https://www.instagram.com/bestpractice___/" rel="noopener noreferrer"&gt;@bestpractice___&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>nextjs</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>javascript</category>
    </item>
    <item>
      <title>TypeScript Type Narrowing: The Complete Guide</title>
      <dc:creator>Parsa J</dc:creator>
      <pubDate>Fri, 27 Nov 2026 11:46:59 +0000</pubDate>
      <link>https://dev.to/parsajiravand/typescript-type-narrowing-the-complete-guide-52mi</link>
      <guid>https://dev.to/parsajiravand/typescript-type-narrowing-the-complete-guide-52mi</guid>
      <description>&lt;p&gt;You write &lt;code&gt;function greet(value: string | null)&lt;/code&gt;, guard it with &lt;code&gt;if (value === null) return;&lt;/code&gt;, and schedule a &lt;code&gt;setTimeout&lt;/code&gt; callback that uses &lt;code&gt;value&lt;/code&gt;. Right after scheduling it, you reset &lt;code&gt;value&lt;/code&gt; back to &lt;code&gt;null&lt;/code&gt; — a one-line cleanup, nothing dramatic. TypeScript suddenly reports "&lt;code&gt;value&lt;/code&gt; is possibly &lt;code&gt;null&lt;/code&gt;" &lt;strong&gt;inside the callback&lt;/strong&gt;, on a check you already passed three lines earlier. Nothing about the check was wrong. You just ran into one of the specific, learnable rules of &lt;strong&gt;type narrowing&lt;/strong&gt;, the mechanism TypeScript uses to track which members of a union type are still possible at each line of your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you'll learn
&lt;/h2&gt;

&lt;p&gt;By the end of this guide you'll be able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explain what control flow analysis actually tracks as it reads your code&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;typeof&lt;/code&gt;, &lt;code&gt;instanceof&lt;/code&gt;, the &lt;code&gt;in&lt;/code&gt; operator, and equality checks to narrow a union correctly&lt;/li&gt;
&lt;li&gt;Model variants with &lt;strong&gt;discriminated unions&lt;/strong&gt; instead of a pile of optional fields&lt;/li&gt;
&lt;li&gt;Write your own type guard (&lt;code&gt;x is T&lt;/code&gt;) and assertion functions, and know when a plain boolean function won't narrow anything&lt;/li&gt;
&lt;li&gt;Recognize the specific situations where narrowing is lost — closures and repeated function calls — and fix each one, plus why a generic type parameter narrows differently rather than losing narrowing at all&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Who this is for:&lt;/strong&gt; you've written union types and &lt;code&gt;if&lt;/code&gt; checks in TypeScript, and you've either hit a narrowing error that didn't make sense, or you want to know what the compiler is actually doing instead of treating it as a black box.&lt;/p&gt;

&lt;h2&gt;
  
  
  Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Why type narrowing exists&lt;/li&gt;
&lt;li&gt;The mental model&lt;/li&gt;
&lt;li&gt;Stage 1: typeof, truthiness, and equality narrowing&lt;/li&gt;
&lt;li&gt;Stage 2: instanceof and the in operator&lt;/li&gt;
&lt;li&gt;Stage 3: discriminated unions&lt;/li&gt;
&lt;li&gt;Stage 4: user-defined type guards and assertion functions&lt;/li&gt;
&lt;li&gt;Edge cases and gotchas&lt;/li&gt;
&lt;li&gt;Best practices&lt;/li&gt;
&lt;li&gt;FAQ&lt;/li&gt;
&lt;li&gt;Cheat sheet&lt;/li&gt;
&lt;li&gt;Key takeaways&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why type narrowing exists
&lt;/h2&gt;

&lt;p&gt;Here's the naïve version of the opening story, written out in full:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;shout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error: 'value' is possibly 'null'.&lt;/span&gt;
  &lt;span class="c1"&gt;// Error: Property 'toUpperCase' does not exist on type 'number'.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;value&lt;/code&gt;'s declared type is &lt;code&gt;string | number | null&lt;/code&gt; — a union of three possibilities. TypeScript has no way to know, at this line, which one you actually have, so it only allows operations that are valid on &lt;strong&gt;every&lt;/strong&gt; member of the union. &lt;code&gt;toUpperCase&lt;/code&gt; exists on &lt;code&gt;string&lt;/code&gt;, not on &lt;code&gt;number&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;, so the call is rejected no matter which branch is "probably" true at runtime.&lt;/p&gt;

&lt;p&gt;The tempting fix is to make the compiler stop complaining:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;shout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// compiles — and crashes if value is null&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;as string&lt;/code&gt; doesn't check anything — it tells TypeScript to trust you, and TypeScript does, even when you're wrong. This is the thing to avoid for this entire guide: a cast silences the error without resolving the uncertainty it was reporting.&lt;/p&gt;

&lt;p&gt;The real fix is to &lt;strong&gt;narrow&lt;/strong&gt; the union — write a check whose outcome TypeScript can use to rule members out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;shout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// value: string here — no cast needed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing about &lt;code&gt;value&lt;/code&gt;'s declared type changed. What changed is TypeScript's &lt;em&gt;belief&lt;/em&gt; about which members of that union are still possible at this specific line, given the &lt;code&gt;typeof&lt;/code&gt; check that just ran and returned early.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mental model: a type environment that forks and merges
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The mental model:&lt;/strong&gt; as TypeScript reads your function from top to bottom, it maintains a type for every variable at &lt;em&gt;that exact point in the code&lt;/em&gt; — not one fixed type for the whole function. This per-line tracking is called &lt;strong&gt;control flow analysis&lt;/strong&gt;. Every &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;else&lt;/code&gt;, &lt;code&gt;return&lt;/code&gt;, &lt;code&gt;throw&lt;/code&gt;, &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;, &lt;code&gt;||&lt;/code&gt;, and &lt;code&gt;switch&lt;/code&gt; forks that tracking into branches, and every point where branches rejoin (the line after an &lt;code&gt;if&lt;/code&gt; block, say) merges the types back together as a union of whatever each branch produced.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;example&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// value: string | number | null&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// value: null&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// value: string | number   (the null branch returned, so it's gone)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// value: string&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// value: number           (the only thing left once string is ruled out)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// value: string | number   (merged back after the if/else)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Key concept:&lt;/strong&gt; narrowing is not a special annotation you add — it's TypeScript running the same reasoning you would, mechanically, on every condition in your code, and remembering the result line by line.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Stage 1: typeof, truthiness, and equality narrowing
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;typeof&lt;/code&gt;&lt;/strong&gt; narrows unions of primitives — &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;, &lt;code&gt;bigint&lt;/code&gt;, &lt;code&gt;symbol&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;function&lt;/code&gt;, and &lt;code&gt;object&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"number"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// value: number&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// value: string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Truthiness narrowing&lt;/strong&gt; uses any value in boolean position — an &lt;code&gt;if&lt;/code&gt;, a &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;, a ternary — and rules out the falsy members of a union (&lt;code&gt;null&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;""&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt;, &lt;code&gt;NaN&lt;/code&gt;) that the &lt;em&gt;type&lt;/em&gt; can represent, not every falsy value of the remaining type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;printLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// value: string — null and undefined are ruled out&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Watch the gap here: &lt;code&gt;if (value)&lt;/code&gt; also excludes the empty string &lt;code&gt;""&lt;/code&gt;, which is a perfectly valid &lt;code&gt;string&lt;/code&gt;. Truthiness narrowing removes &lt;code&gt;null | undefined&lt;/code&gt; from the &lt;em&gt;type&lt;/em&gt;, but it does not mean "every string reached this branch" — an empty string never gets here at runtime either. For that reason, prefer an explicit check when the empty string matters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// value: string, including ""&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Equality narrowing&lt;/strong&gt; uses &lt;code&gt;===&lt;/code&gt;, &lt;code&gt;!==&lt;/code&gt;, &lt;code&gt;==&lt;/code&gt;, and &lt;code&gt;!=&lt;/code&gt; against a specific value or another variable, and also works inside a &lt;code&gt;switch&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"idle"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"loading"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"success"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"idle"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Not started"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// status: "idle"&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Something went wrong"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// status: "error"&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"In progress"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// status: "loading" | "success"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Stage 2: instanceof and the in operator
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;instanceof&lt;/code&gt;&lt;/strong&gt; narrows by checking the prototype chain — the standard way to distinguish classes, including the built-in &lt;code&gt;Error&lt;/code&gt; hierarchy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Network failure:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// err: TypeError&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This matters because a &lt;code&gt;catch&lt;/code&gt; clause's variable is typed &lt;code&gt;unknown&lt;/code&gt; by default in modern TypeScript (&lt;code&gt;useUnknownInCatchVariables&lt;/code&gt;) — you're required to narrow it, with &lt;code&gt;instanceof&lt;/code&gt; or &lt;code&gt;typeof&lt;/code&gt;, before touching &lt;code&gt;.message&lt;/code&gt; or anything else on it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;in&lt;/code&gt; operator&lt;/strong&gt; narrows object unions by checking whether a property exists, which is the tool to reach for when the members aren't class instances:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;bark&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;meow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"bark"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bark&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// animal: Dog&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;meow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// animal: Cat&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Stage 3: discriminated unions
&lt;/h2&gt;

&lt;p&gt;This is the single most important pattern in this guide. Instead of one type with several optional fields — which lets you construct impossible combinations and gives &lt;code&gt;in&lt;/code&gt;/&lt;code&gt;typeof&lt;/code&gt; nothing clean to check — give every variant a shared, literal-typed property (the &lt;strong&gt;discriminant&lt;/strong&gt;) and let the value of that one property select the whole shape:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// the shape that invites bugs: are width/height and radius ever both set? neither?&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;BadShape&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"circle"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"rectangle"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// the discriminated union: each variant is its own exact type&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"circle"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"rectangle"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"circle"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// shape: Circle — width/height don't exist here&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"rectangle"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// shape: Rectangle&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Checking &lt;code&gt;shape.kind&lt;/code&gt; against a specific literal narrows &lt;code&gt;shape&lt;/code&gt; itself to the one member of the union whose &lt;code&gt;kind&lt;/code&gt; field has that literal type — this works because every variant's discriminant is a distinct literal, not the general &lt;code&gt;string&lt;/code&gt;. Add a &lt;code&gt;default&lt;/code&gt; branch that returns &lt;code&gt;shape satisfies never&lt;/code&gt; (or assigns &lt;code&gt;shape&lt;/code&gt; to a variable typed &lt;code&gt;never&lt;/code&gt;) and the compiler will flag any future variant you forget to handle — that's &lt;strong&gt;exhaustiveness checking&lt;/strong&gt;, a direct payoff of narrowing a discriminated union down to nothing, worth its own guide.&lt;/p&gt;

&lt;h2&gt;
  
  
  🎮 Try it yourself
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://bestpractic.org/blog/weekly-typescript-type-narrowing/playground" rel="noopener noreferrer"&gt;▶️ Open the interactive playground →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Runs right in your browser — poke at it and watch the concept react live.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 4: user-defined type guards and assertion functions
&lt;/h2&gt;

&lt;p&gt;Sometimes the check you need isn't &lt;code&gt;typeof&lt;/code&gt;, &lt;code&gt;instanceof&lt;/code&gt;, or &lt;code&gt;in&lt;/code&gt; — it's custom logic. A plain function returning &lt;code&gt;boolean&lt;/code&gt; does &lt;strong&gt;not&lt;/strong&gt; narrow, even if it's correct at runtime:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;example&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error: 'toUpperCase' does not exist on type 'string | number'&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TypeScript doesn't inline the function to see what it checks — it only looks at the declared return type, and &lt;code&gt;boolean&lt;/code&gt; carries no information about &lt;code&gt;value&lt;/code&gt;. The fix is a &lt;strong&gt;type predicate&lt;/strong&gt;, written as &lt;code&gt;parameterName is Type&lt;/code&gt; in the return position:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;example&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// value: string — the predicate narrows it&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Key concept:&lt;/strong&gt; &lt;code&gt;value is string&lt;/code&gt; is a promise you're making to the compiler about what a &lt;code&gt;true&lt;/code&gt; result means. TypeScript trusts the annotation, not the implementation — write the check carefully, because a wrong predicate narrows to the wrong type with no error.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Assertion functions&lt;/strong&gt; (TypeScript 3.7+) are the &lt;code&gt;assert&lt;/code&gt;-style sibling: instead of returning a boolean to branch on, they throw when the condition fails, and everything &lt;em&gt;after&lt;/em&gt; the call is narrowed for the rest of the scope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;assertIsString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;asserts&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Expected a string"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;example&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;assertIsString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// value: string — narrowed for the rest of this scope, no if needed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Edge cases and gotchas
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Narrowing is lost inside a closure if the variable can be reassigned.&lt;/strong&gt; TypeScript can't know when a nested function will actually run, so if the outer variable is reassigned &lt;em&gt;anywhere&lt;/em&gt; in the enclosing scope, the compiler reverts to its declared type inside the closure — even above the reassignment in reading order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error: 'value' is possibly 'undefined'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler can't model what happens inside an embedded function in every case — it can't know when that function will actually be called, or whether the function itself is what reassigns the variable. Note that this only kicks in when the variable genuinely &lt;em&gt;can&lt;/em&gt; be reassigned somewhere in the enclosing scope; a parameter or &lt;code&gt;let&lt;/code&gt; that's never reassigned stays narrowed inside a closure without any extra work. The fix for the reassignable case is to copy the narrowed value into a local &lt;code&gt;const&lt;/code&gt; before the closure, since a &lt;code&gt;const&lt;/code&gt; can't be reassigned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;narrowed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// const: always string, safe to capture&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;narrowed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Narrowing doesn't survive calling the same function twice.&lt;/strong&gt; It's tempting to assume TypeScript tracks &lt;em&gt;values&lt;/em&gt;, but it tracks &lt;em&gt;expressions&lt;/em&gt; — and two calls to the same function are two separate expressions that could return two different things:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error: this is a second, independent call&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TypeScript has no way to know &lt;code&gt;getValue&lt;/code&gt; is pure — nothing stops it from returning a &lt;code&gt;number&lt;/code&gt; the second time, even if it happens to be deterministic in your code. Store the result in a local variable right after the check instead of calling it again, and the narrowing holds for the rest of the block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// fine — one call, one narrowed variable&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Narrowing a generic type parameter narrows the value, not the parameter.&lt;/strong&gt; Inside a function generic over &lt;code&gt;T&lt;/code&gt;, a &lt;code&gt;typeof&lt;/code&gt; check on a parameter typed &lt;code&gt;T&lt;/code&gt; doesn't make TypeScript believe &lt;code&gt;T&lt;/code&gt; itself is &lt;code&gt;string&lt;/code&gt; — it produces an intersection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// value: T &amp;amp; string — not string&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// fine, T &amp;amp; string still has toUpperCase&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is usually harmless: &lt;code&gt;T &amp;amp; string&lt;/code&gt; still has every &lt;code&gt;string&lt;/code&gt; method, and — because an intersection is a subtype of each of its parts — it's also still assignable back to a plain &lt;code&gt;T&lt;/code&gt;, so returning &lt;code&gt;value&lt;/code&gt; from inside that branch compiles fine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The non-null assertion operator (&lt;code&gt;!&lt;/code&gt;) is not narrowing.&lt;/strong&gt; It's an unchecked assertion, exactly like &lt;code&gt;as&lt;/code&gt; — it silences the error without verifying anything, and it's wrong the moment the value actually is &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt; at runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best practices
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Reach for a discriminated union&lt;/strong&gt; the moment a type has more than one optional field and the valid combinations aren't "any of the above." It turns invalid states into compile errors at the construction site instead of runtime bugs at the access site.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Write a type predicate&lt;/strong&gt; when the same non-trivial check — more than a single &lt;code&gt;typeof&lt;/code&gt;/&lt;code&gt;instanceof&lt;/code&gt;/&lt;code&gt;in&lt;/code&gt; — is repeated across a codebase, so the narrowing logic and the runtime logic can never drift apart. Keep the predicate's body honest: it is trusted completely, so an incorrect one is worse than no check at all.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Avoid &lt;code&gt;as&lt;/code&gt; and &lt;code&gt;!&lt;/code&gt;&lt;/strong&gt; wherever a narrowing check would do the same job. They're appropriate at real boundaries — parsing untyped JSON, interop with an untyped library — not as a shortcut past a union the compiler is right to flag.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Copy before you capture or re-call.&lt;/strong&gt; If a narrowed value needs to survive a closure, or you need to use the result of a function call more than once, assign it to a local &lt;code&gt;const&lt;/code&gt; immediately after the check. It costs one line and removes an entire category of "but I already checked this" errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why does TypeScript say a value is still possibly null after I checked it?
&lt;/h3&gt;

&lt;p&gt;Usually one of three reasons: the check and the use are two separate calls to the same function, which TypeScript can't assume return the same thing; the check happens outside a closure that captures a variable which can be reassigned later in the same scope; or the check is a plain boolean-returning function instead of a type predicate (&lt;code&gt;x is T&lt;/code&gt;). See "Edge cases and gotchas" above for each fix.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does a &lt;code&gt;Boolean()&lt;/code&gt; or custom &lt;code&gt;isTruthy()&lt;/code&gt; function narrow like &lt;code&gt;typeof&lt;/code&gt; does?
&lt;/h3&gt;

&lt;p&gt;No, unless its return type is explicitly a type predicate (&lt;code&gt;x is T&lt;/code&gt;). A function declared to return plain &lt;code&gt;boolean&lt;/code&gt; carries no information back to the call site about what became true, no matter how correct its internal logic is.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is &lt;code&gt;Array.isArray()&lt;/code&gt; a type guard?
&lt;/h3&gt;

&lt;p&gt;Yes — it's declared in TypeScript's standard library as &lt;code&gt;isArray(arg: any): arg is any[]&lt;/code&gt;, so &lt;code&gt;if (Array.isArray(value))&lt;/code&gt; narrows &lt;code&gt;value&lt;/code&gt; to an array type inside that branch, the same as any user-defined predicate.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's the difference between a type guard and an assertion function?
&lt;/h3&gt;

&lt;p&gt;A type guard (&lt;code&gt;x is T&lt;/code&gt;) returns a boolean you branch on with &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt;; both branches continue. An assertion function (&lt;code&gt;asserts x is T&lt;/code&gt;) throws when the condition is false, so there's no &lt;code&gt;else&lt;/code&gt; — everything after the call, for the rest of that scope, is narrowed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I narrow a value typed &lt;code&gt;any&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;Narrowing checks still run, but &lt;code&gt;any&lt;/code&gt; opts a value out of type checking entirely, so there's nothing for TypeScript to narrow &lt;em&gt;from&lt;/em&gt; — the result of a check on an &lt;code&gt;any&lt;/code&gt; is still &lt;code&gt;any&lt;/code&gt;. Narrowing is only meaningful on &lt;code&gt;unknown&lt;/code&gt; or a specific union.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Technique&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;th&gt;Narrows to&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;typeof&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;typeof value === "string"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The matching primitive type&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Truthiness&lt;/td&gt;
&lt;td&gt;&lt;code&gt;if (value)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Removes &lt;code&gt;null&lt;/code&gt;/&lt;code&gt;undefined&lt;/code&gt;/other falsy members — not every falsy value of what's left&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Equality&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;value === "idle"&lt;/code&gt; / &lt;code&gt;switch (value)&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;The matching literal member&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;instanceof&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;err instanceof TypeError&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The matching class&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;in&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"bark" in animal&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The union member that has the property&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Discriminant&lt;/td&gt;
&lt;td&gt;&lt;code&gt;switch (shape.kind)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The exact variant whose literal discriminant matches&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;User-defined guard&lt;/td&gt;
&lt;td&gt;&lt;code&gt;function isX(v): v is X&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;X&lt;/code&gt;, inside the &lt;code&gt;true&lt;/code&gt; branch only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Assertion function&lt;/td&gt;
&lt;td&gt;&lt;code&gt;function assertX(v): asserts v is X&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;X&lt;/code&gt;, for the rest of the scope after the call&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Not narrowing&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;as T&lt;/code&gt;, &lt;code&gt;value!&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Nothing — unchecked, trusted blindly&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// the whole pattern, copy-paste ready&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"circle"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"rectangle"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isShape&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"object"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="s2"&gt;"kind"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"circle"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"rectangle"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"circle"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"rectangle"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;exhaustive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;never&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// compile error if a variant is ever added and unhandled&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;exhaustive&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🧠 Test yourself
&lt;/h2&gt;

&lt;p&gt;Think it clicked? &lt;strong&gt;&lt;a href="https://bestpractic.org/blog/weekly-typescript-type-narrowing/quiz" rel="noopener noreferrer"&gt;Take the 8-question quiz →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Narrowing is control flow analysis: TypeScript tracks the most specific type each variable could have at every line, forking on every condition and merging back where branches rejoin.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;typeof&lt;/code&gt;, &lt;code&gt;instanceof&lt;/code&gt;, &lt;code&gt;in&lt;/code&gt;, and equality checks narrow built-in and class unions directly; a plain boolean function never narrows — only a type predicate (&lt;code&gt;x is T&lt;/code&gt;) or an assertion function (&lt;code&gt;asserts x is T&lt;/code&gt;) does.&lt;/li&gt;
&lt;li&gt;Discriminated unions turn "which optional fields are set" bugs into compile errors, and a &lt;code&gt;never&lt;/code&gt;-typed default branch gets you exhaustiveness checking for free.&lt;/li&gt;
&lt;li&gt;Narrowing is lost — not just weakened — by reassignable closures and repeated calls to the same function. Copying the narrowed value into a local &lt;code&gt;const&lt;/code&gt; fixes both. A generic type parameter is different: narrowing still applies, it just produces an intersection (&lt;code&gt;T &amp;amp; string&lt;/code&gt;) instead of collapsing &lt;code&gt;T&lt;/code&gt; itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That &lt;code&gt;setTimeout&lt;/code&gt; error from the opening isn't the compiler being difficult — it's the compiler correctly refusing to promise something it can't verify about code that runs later. Once you can see narrowing as a type environment that forks and merges line by line, every one of these errors stops being mysterious and starts being something you can predict before you even hit compile.&lt;/p&gt;

&lt;p&gt;Which rule bit you — the closure, the repeated function call, or the generic parameter? Tell me in the comments.&lt;/p&gt;

&lt;h2&gt;
  
  
  📚 Read next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/typescript-generics-the-complete-guide-with-cheat-sheet-45a9"&gt;TypeScript Generics: The Complete Guide (with Cheat Sheet)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/inferred-type-predicates-wont-save-filterboolean-heres-what-will-2dok"&gt;Inferred Type Predicates Won't Save filter(Boolean). Here's What Will.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/web-workers-in-javascript-the-complete-guide-5f17"&gt;Web Workers in JavaScript: The Complete Guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🚀 &lt;strong&gt;Want more like this?&lt;/strong&gt; Every guide, playground, and quiz lives on &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;bestpractic.org&lt;/a&gt;&lt;/strong&gt; — open it and &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;sign up free&lt;/a&gt;&lt;/strong&gt; so the next one finds you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading! Let's stay connected:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⭐ &lt;strong&gt;GitHub&lt;/strong&gt; — follow me and star the projects: &lt;a href="https://github.com/parsajiravand" rel="noopener noreferrer"&gt;github.com/parsajiravand&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;strong&gt;Discord&lt;/strong&gt; — join the frontend best-practices community: &lt;a href="https://discord.gg/d9KRhuAwQ" rel="noopener noreferrer"&gt;discord.gg/d9KRhuAwQ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📸 &lt;strong&gt;Instagram&lt;/strong&gt; — frontend best practices, daily: &lt;a href="https://www.instagram.com/bestpractice___/" rel="noopener noreferrer"&gt;@bestpractice___&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>CSS Transitions Skip display: none. transition-behavior Fixes That.</title>
      <dc:creator>Parsa J</dc:creator>
      <pubDate>Fri, 27 Nov 2026 11:46:28 +0000</pubDate>
      <link>https://dev.to/parsajiravand/css-transitions-skip-display-none-transition-behavior-fixes-that-14h5</link>
      <guid>https://dev.to/parsajiravand/css-transitions-skip-display-none-transition-behavior-fixes-that-14h5</guid>
      <description>&lt;p&gt;You build a dialog. You add &lt;code&gt;transition: opacity 0.2s, transform 0.2s&lt;/code&gt;. You open it.&lt;/p&gt;

&lt;p&gt;It just appears. No fade, no slide — a jump-cut, like someone spliced the film.&lt;/p&gt;

&lt;p&gt;You check the transition is spelled right. It is. You check the element is actually changing opacity. It is — from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;1&lt;/code&gt;, exactly like you wrote it. So why does it still snap?&lt;/p&gt;

&lt;p&gt;Because the element isn't only changing opacity. It's also going from &lt;code&gt;display: none&lt;/code&gt; to &lt;code&gt;display: block&lt;/code&gt;, and CSS has never known how to transition that.&lt;/p&gt;

&lt;h2&gt;
  
  
  The property transitions can't touch
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;display&lt;/code&gt; isn't a number, a color, or a length. You can interpolate &lt;code&gt;opacity: 0&lt;/code&gt; to &lt;code&gt;opacity: 1&lt;/code&gt; because the browser can compute every value in between — &lt;code&gt;0.3&lt;/code&gt;, &lt;code&gt;0.7&lt;/code&gt;, whatever the easing curve wants at that frame. There is no value "in between" &lt;code&gt;none&lt;/code&gt; and &lt;code&gt;block&lt;/code&gt;. An element is either laid out or it isn't.&lt;/p&gt;

&lt;p&gt;Properties like this are &lt;strong&gt;discrete&lt;/strong&gt;: &lt;code&gt;display&lt;/code&gt;, &lt;code&gt;content-visibility&lt;/code&gt;, &lt;code&gt;overlay&lt;/code&gt;, &lt;code&gt;visibility&lt;/code&gt; (sort of — more on that below). The browser's only option has always been to flip them the instant the transition starts, which for &lt;code&gt;display&lt;/code&gt; means &lt;code&gt;none → block&lt;/code&gt; happens at frame zero, before your opacity transition even gets going.&lt;/p&gt;

&lt;p&gt;So the classic workaround was: don't touch &lt;code&gt;display&lt;/code&gt; at all.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.modal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;visibility&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;hidden&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;opacity&lt;/span&gt; &lt;span class="mf"&gt;0.2s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;visibility&lt;/span&gt; &lt;span class="mf"&gt;0.2s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;.modal.open&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;visibility&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;visible&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;visibility&lt;/code&gt; gets special-cased by the spec — browsers flip it to &lt;code&gt;visible&lt;/code&gt; at the &lt;em&gt;start&lt;/em&gt; of the duration when opening, and to &lt;code&gt;hidden&lt;/code&gt; at the &lt;em&gt;end&lt;/em&gt; when closing, so it doesn't clip the fade. It works, and it's not even an accessibility bug: &lt;code&gt;visibility: hidden&lt;/code&gt; does pull the element out of the accessibility tree and the tab order while it's hidden. The real downside is layout — &lt;code&gt;visibility: hidden&lt;/code&gt; never collapses the box, so the element still occupies its space the whole time, leaving a gap or pushing siblings around until you cover it with something else. The actual fix, for years, meant &lt;code&gt;display: none&lt;/code&gt; toggled from JavaScript on a &lt;code&gt;transitionend&lt;/code&gt; listener, timed by hand, one more thing to get wrong.&lt;/p&gt;

&lt;p&gt;Guess which part people got wrong most. If you said "the timeout that doesn't match the CSS duration," you've shipped this bug too.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changed
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;transition-behavior: allow-discrete&lt;/code&gt; tells the browser: include this property in the transition anyway. Instead of flipping at frame zero, a discrete property now flips at the very end of the transition when going from a value to &lt;code&gt;none&lt;/code&gt;-equivalent, or at the very start when going the other way — same rule &lt;code&gt;visibility&lt;/code&gt; already followed, just available to properties that never had it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.modal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;opacity&lt;/span&gt; &lt;span class="mf"&gt;0.2s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="mf"&gt;0.2s&lt;/span&gt; &lt;span class="n"&gt;allow-discrete&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;.modal.open&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try that alone, though, and you'll hit the next wall. The modal now fades out correctly — &lt;code&gt;display&lt;/code&gt; stays &lt;code&gt;block&lt;/code&gt; for the full duration while opacity eases to &lt;code&gt;0&lt;/code&gt;, then flips to &lt;code&gt;none&lt;/code&gt;. But fading &lt;em&gt;in&lt;/em&gt; still snaps, because on the very first frame the element goes straight to &lt;code&gt;display: block; opacity: 1&lt;/code&gt;. There's no "starting point" to ease from — the element didn't exist in a transitionable state a moment ago.&lt;/p&gt;

&lt;p&gt;That's what &lt;code&gt;@starting-style&lt;/code&gt; is for.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.modal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;opacity&lt;/span&gt; &lt;span class="mf"&gt;0.2s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;display&lt;/span&gt; &lt;span class="mf"&gt;0.2s&lt;/span&gt; &lt;span class="n"&gt;allow-discrete&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;@starting-style&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;.modal.open&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;@starting-style&lt;/code&gt; defines the values the browser should treat as "the frame before this element entered the DOM or became displayable." Without it, there's nothing before frame zero to transition from — the browser has no prior value, so it has nothing to interpolate. With it, the moment &lt;code&gt;display&lt;/code&gt; flips to &lt;code&gt;block&lt;/code&gt;, the element starts at the &lt;code&gt;@starting-style&lt;/code&gt; values and transitions toward its real ones.&lt;/p&gt;

&lt;p&gt;Three pieces, each covering a gap the others leave:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;allow-discrete&lt;/code&gt; lets &lt;code&gt;display&lt;/code&gt; itself sit in the transition queue instead of jumping&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@starting-style&lt;/code&gt; gives the element an entry state to animate &lt;em&gt;from&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;your normal &lt;code&gt;transition&lt;/code&gt; and target values do the actual easing, same as always&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Drop any one and you're back to a jump-cut on either the way in, the way out, or both.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it before you believe it
&lt;/h2&gt;

&lt;p&gt;Toggle the panel below with and without &lt;code&gt;allow-discrete&lt;/code&gt; wired up. Same markup, same duration — the only difference is whether &lt;code&gt;display&lt;/code&gt; is allowed into the transition.&lt;/p&gt;

&lt;h2&gt;
  
  
  🎮 Try it yourself
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://bestpractic.org/blog/css-transition-behavior-allow-discrete/playground" rel="noopener noreferrer"&gt;▶️ Open the interactive playground →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Runs right in your browser — poke at it and watch the concept react live.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The part that still trips people up
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;content-visibility: hidden&lt;/code&gt; and the native &lt;code&gt;&amp;lt;dialog&amp;gt;&lt;/code&gt;/popover &lt;code&gt;overlay&lt;/code&gt; property are discrete too, and they're exactly where this combination earns its keep — you get the rendering-skip benefits of &lt;code&gt;content-visibility&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; an entrance animation, which used to be mutually exclusive. If you're already using &lt;code&gt;content-visibility: auto&lt;/code&gt; to skip offscreen rendering, &lt;code&gt;allow-discrete&lt;/code&gt; is how you animate the same element's reveal without giving that performance win back.&lt;/p&gt;

&lt;p&gt;One honest caveat: this is about &lt;code&gt;transition&lt;/code&gt;, not &lt;code&gt;animation&lt;/code&gt;. Keyframe animations have had &lt;code&gt;@starting-style&lt;/code&gt;-adjacent control for longer via &lt;code&gt;animation-fill-mode&lt;/code&gt;, and discrete properties behave a little differently inside &lt;code&gt;@keyframes&lt;/code&gt; than inside a plain &lt;code&gt;transition&lt;/code&gt;. If your modal uses &lt;code&gt;animation&lt;/code&gt; instead of &lt;code&gt;transition&lt;/code&gt;, check the specific property's behavior before assuming &lt;code&gt;allow-discrete&lt;/code&gt; is even the piece you're missing.&lt;/p&gt;

&lt;p&gt;And it's not magic for every discrete property — &lt;code&gt;transition-behavior: allow-discrete&lt;/code&gt; only changes &lt;em&gt;when&lt;/em&gt; the flip happens relative to the transition's duration. It still can't interpolate &lt;code&gt;display: block&lt;/code&gt; to &lt;code&gt;display: flex&lt;/code&gt;, because those aren't "on vs off," they're two different values with no path between them. The trick only works for the none ↔ something-else case.&lt;/p&gt;

&lt;p&gt;Support is more uneven than it looks. Chrome 117 shipped the full combination — including animating &lt;code&gt;display&lt;/code&gt; itself — soon after &lt;code&gt;transition-behavior&lt;/code&gt; landed. Safari took two steps: 17.4 added &lt;code&gt;transition-behavior&lt;/code&gt; and parsed &lt;code&gt;allow-discrete&lt;/code&gt;, but didn't extend the discrete-property flip to &lt;code&gt;display&lt;/code&gt; until Safari 18. Firefox shipped &lt;code&gt;transition-behavior&lt;/code&gt; and &lt;code&gt;@starting-style&lt;/code&gt; in 129, but as of late 2026 it still doesn't reliably transition &lt;code&gt;display&lt;/code&gt; itself — closing transitions in particular can cancel and jump straight to &lt;code&gt;none&lt;/code&gt; instead of running the full duration, a long-standing, still-open limitation rather than a recent regression. So build for the gap: the worst case should be the old jump-cut, not broken layout, but on today's Firefox that jump-cut can still show up on close.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test what you actually know
&lt;/h2&gt;

&lt;h2&gt;
  
  
  🧠 Test yourself
&lt;/h2&gt;

&lt;p&gt;Think it clicked? &lt;strong&gt;&lt;a href="https://bestpractic.org/blog/css-transition-behavior-allow-discrete/quiz" rel="noopener noreferrer"&gt;Take the 8-question quiz →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That jump-cut modal isn't a &lt;code&gt;display&lt;/code&gt; bug you work around anymore — it's three CSS features doing exactly what they say: &lt;code&gt;allow-discrete&lt;/code&gt; admits the property into the transition, &lt;code&gt;@starting-style&lt;/code&gt; gives it a place to start from, and your existing &lt;code&gt;transition&lt;/code&gt; line does the rest.&lt;/p&gt;

&lt;p&gt;Next time you reach for a &lt;code&gt;setTimeout&lt;/code&gt; to delay a &lt;code&gt;display: none&lt;/code&gt;, try deleting it instead. What's the last place in your codebase where JavaScript is still faking something CSS can now do natively? I'd genuinely like to know — drop it in the comments.&lt;/p&gt;

&lt;h2&gt;
  
  
  📚 Read next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/youre-blocking-touchmove-events-to-contain-scroll-overscroll-behavior-does-it-natively-223o"&gt;You're blocking touchmove events to contain scroll. &lt;code&gt;overscroll-behavior&lt;/code&gt; does it natively.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/stop-using-a-js-masonry-library-css-grid-lanes-does-it-natively-n42"&gt;Stop Using a JS Masonry Library. CSS Grid Lanes Does It Natively.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/stop-writing-javascript-to-fix-100vh-on-mobile-42bk"&gt;Stop Writing JavaScript To Fix &lt;code&gt;100vh&lt;/code&gt; On Mobile&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🚀 &lt;strong&gt;Want more like this?&lt;/strong&gt; Every guide, playground, and quiz lives on &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;bestpractic.org&lt;/a&gt;&lt;/strong&gt; — open it and &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;sign up free&lt;/a&gt;&lt;/strong&gt; so the next one finds you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading! Let's stay connected:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⭐ &lt;strong&gt;GitHub&lt;/strong&gt; — follow me and star the projects: &lt;a href="https://github.com/parsajiravand" rel="noopener noreferrer"&gt;github.com/parsajiravand&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;strong&gt;Discord&lt;/strong&gt; — join the frontend best-practices community: &lt;a href="https://discord.gg/d9KRhuAwQ" rel="noopener noreferrer"&gt;discord.gg/d9KRhuAwQ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📸 &lt;strong&gt;Instagram&lt;/strong&gt; — frontend best practices, daily: &lt;a href="https://www.instagram.com/bestpractice___/" rel="noopener noreferrer"&gt;@bestpractice___&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>frontend</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>TypeScript Can't Tell userId From orderId. Branded Types Can.</title>
      <dc:creator>Parsa J</dc:creator>
      <pubDate>Fri, 27 Nov 2026 11:45:57 +0000</pubDate>
      <link>https://dev.to/parsajiravand/typescript-cant-tell-userid-from-orderid-branded-types-can-3hhi</link>
      <guid>https://dev.to/parsajiravand/typescript-cant-tell-userid-from-orderid-branded-types-can-3hhi</guid>
      <description>&lt;p&gt;A support engineer is looking at a refund ticket. They copy an ID from one browser tab, paste it into another, and call &lt;code&gt;issueRefund(id)&lt;/code&gt;. The function signature says &lt;code&gt;id: string&lt;/code&gt;. The ID they pasted is a &lt;code&gt;string&lt;/code&gt;. TypeScript has nothing to say about it.&lt;/p&gt;

&lt;p&gt;The refund goes out. Just not to the customer who filed the ticket — to whoever that ID actually belonged to.&lt;/p&gt;

&lt;p&gt;Nobody wrote a bug here. The types were never wrong. That's the part that should bother you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two IDs, one type, zero complaints
&lt;/h2&gt;

&lt;p&gt;Here's the function, trimmed to the part that matters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;issueRefund&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amountCents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// looks up the user, credits their account&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// fetches the order&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ord_9f21"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;issueRefund&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customerOrderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ord_9f21, not a user id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That last line compiles clean. &lt;code&gt;order.customerOrderId&lt;/code&gt; is a &lt;code&gt;string&lt;/code&gt;. &lt;code&gt;issueRefund&lt;/code&gt; wants a &lt;code&gt;string&lt;/code&gt;. TypeScript checks shapes, not names — to the compiler, &lt;code&gt;UserId&lt;/code&gt; and &lt;code&gt;OrderId&lt;/code&gt; were never distinct in the first place, because nobody declared them. They're both just &lt;code&gt;string&lt;/code&gt;, and every &lt;code&gt;string&lt;/code&gt; is interchangeable with every other &lt;code&gt;string&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is structural typing, and 95% of the time it's exactly what you want — it's why you can pass a plain object literal anywhere an interface is expected without extending anything. The 5% is IDs, currency amounts, sanitized vs. raw HTML, and anything else where the &lt;em&gt;shape&lt;/em&gt; is identical but the &lt;em&gt;meaning&lt;/em&gt; isn't allowed to mix.&lt;/p&gt;

&lt;p&gt;Spot where you'd make this mistake yourself? Rename the variables and it's not obvious at all — which is exactly why it ships.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why &lt;code&gt;type UserId = string&lt;/code&gt; doesn't help
&lt;/h2&gt;

&lt;p&gt;The first instinct is a type alias:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;UserId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;OrderId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;issueRefund&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UserId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amountCents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"ord_9f21"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;issueRefund&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// still compiles&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Still compiles. A &lt;code&gt;type&lt;/code&gt; alias doesn't create a new type — it creates a second name for the same type. &lt;code&gt;UserId&lt;/code&gt; and &lt;code&gt;OrderId&lt;/code&gt; both resolve to &lt;code&gt;string&lt;/code&gt;, so as far as the checker is concerned you wrote &lt;code&gt;issueRefund(id: string, ...)&lt;/code&gt; twice. The alias documents intent for a human skimming the code; it does nothing for the compiler.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix: give the type something a plain string doesn't have
&lt;/h2&gt;

&lt;p&gt;Branded types close the gap by attaching a marker property that exists only in the type system — never at runtime:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Brand&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;B&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;__brand&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;B&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;UserId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Brand&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"UserId"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;OrderId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Brand&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"OrderId"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;issueRefund&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UserId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amountCents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toUserId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UserId&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;UserId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toOrderId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderId&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;OrderId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the swap is a compile error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;orderId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;toOrderId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ord_9f21"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;issueRefund&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Argument of type 'OrderId' is not assignable to parameter of type 'UserId'.&lt;/span&gt;
&lt;span class="c1"&gt;// Type 'OrderId' is not assignable to type '{ readonly __brand: "UserId"; }'.&lt;/span&gt;
&lt;span class="c1"&gt;//   Types of property '__brand' are incompatible.&lt;/span&gt;
&lt;span class="c1"&gt;//     Type '"OrderId"' is not assignable to type '"UserId"'.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;UserId&lt;/code&gt; and &lt;code&gt;OrderId&lt;/code&gt; are both still strings underneath — &lt;code&gt;__brand&lt;/code&gt; never appears in a compiled &lt;code&gt;.js&lt;/code&gt; file, it's erased along with every other type annotation. It costs nothing at runtime and nothing in bundle size. All it does is give the structural checker something to disagree about, which is the entire point: you're opting one specific type out of structural compatibility.&lt;/p&gt;

&lt;p&gt;The only place you're allowed to use &lt;code&gt;as UserId&lt;/code&gt; is inside &lt;code&gt;toUserId&lt;/code&gt; itself — a narrow, named boundary function. Every other call site just passes a &lt;code&gt;UserId&lt;/code&gt; it already has, the same way you'd never cast to &lt;code&gt;string&lt;/code&gt; by hand in normal code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The part branding doesn't do for you
&lt;/h2&gt;

&lt;p&gt;This is the honest caveat, and skipping it is how teams get a false sense of safety: &lt;strong&gt;a brand is a label, not a check.&lt;/strong&gt; &lt;code&gt;toUserId("literally anything")&lt;/code&gt; will happily mint a &lt;code&gt;UserId&lt;/code&gt; out of a string that was never looked up anywhere. Branding stops a &lt;em&gt;valid&lt;/em&gt; &lt;code&gt;OrderId&lt;/code&gt; from sneaking into a &lt;code&gt;UserId&lt;/code&gt; slot — it does nothing to stop an &lt;em&gt;invalid&lt;/em&gt; string from becoming either one.&lt;/p&gt;

&lt;p&gt;If you need the value validated too, put a real check inside the boundary function instead of a bare cast:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;toUserId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UserId&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!/^&lt;/span&gt;&lt;span class="n"&gt;usr_&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;z0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;$&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`not a valid user id: ${raw}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;raw&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;UserId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're already on a validation library, you likely don't need &lt;code&gt;Brand&amp;lt;&amp;gt;&lt;/code&gt; yourself — Zod's &lt;code&gt;.brand()&lt;/code&gt; does the same type-level trick and ties it to an actual runtime parse: &lt;code&gt;z.string().brand&amp;lt;"UserId"&amp;gt;()&lt;/code&gt; gives you a &lt;code&gt;UserId&lt;/code&gt; only after the string has actually been validated. Reach for the hand-rolled version above when you're branding something you already trust (a value straight out of your own database row), and a validation library's version when the string is coming from outside — a request body, a query param, a pasted ID in a support ticket.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you get for it
&lt;/h2&gt;

&lt;p&gt;One type error at the call site instead of a wrong refund in production. No new runtime cost — the brand is erased at compile time like every other TypeScript annotation. No new dependency, if you don't want one. And it generalizes past IDs: money as &lt;code&gt;Brand&amp;lt;number, "Cents"&amp;gt;&lt;/code&gt; so you can't add dollars to cents, &lt;code&gt;Brand&amp;lt;string, "SanitizedHtml"&amp;gt;&lt;/code&gt; so raw user input can't reach &lt;code&gt;dangerouslySetInnerHTML&lt;/code&gt; without passing through your sanitizer first, anywhere "same shape, different meaning" is the actual bug waiting to happen.&lt;/p&gt;

&lt;p&gt;The support engineer in the opening scene didn't do anything wrong. The types just never told them &lt;code&gt;userId&lt;/code&gt; and &lt;code&gt;orderId&lt;/code&gt; were different promises. Now they do.&lt;/p&gt;

&lt;h2&gt;
  
  
  🎮 Try it yourself
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://bestpractic.org/blog/typescript-branded-types-nominal-typing/playground" rel="noopener noreferrer"&gt;▶️ Open the interactive playground →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Runs right in your browser — poke at it and watch the concept react live.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Test yourself
&lt;/h2&gt;

&lt;p&gt;Think it clicked? &lt;strong&gt;&lt;a href="https://bestpractic.org/blog/typescript-branded-types-nominal-typing/quiz" rel="noopener noreferrer"&gt;Take the 7-question quiz →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Where else in your codebase are two meanings hiding behind one primitive type? Money amounts in different currencies? Sanitized vs. raw strings? Drop the pair in the comments — I'll bet at least one of them has shipped a swap bug already.&lt;/p&gt;

&lt;h2&gt;
  
  
  📚 Read next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/my-reducers-switch-stopped-being-exhaustive-as-const-fixed-it-4aa4"&gt;My Reducer's Switch Stopped Being Exhaustive. &lt;code&gt;as const&lt;/code&gt; Fixed It.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/stop-formatting-currency-with-regex-intlnumberformat-does-it-220b"&gt;Stop Formatting Currency With Regex. &lt;code&gt;Intl.NumberFormat&lt;/code&gt; Does It.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/inferred-type-predicates-wont-save-filterboolean-heres-what-will-2dok"&gt;Inferred Type Predicates Won't Save filter(Boolean). Here's What Will.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🚀 &lt;strong&gt;Want more like this?&lt;/strong&gt; Every guide, playground, and quiz lives on &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;bestpractic.org&lt;/a&gt;&lt;/strong&gt; — open it and &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;sign up free&lt;/a&gt;&lt;/strong&gt; so the next one finds you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading! Let's stay connected:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⭐ &lt;strong&gt;GitHub&lt;/strong&gt; — follow me and star the projects: &lt;a href="https://github.com/parsajiravand" rel="noopener noreferrer"&gt;github.com/parsajiravand&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;strong&gt;Discord&lt;/strong&gt; — join the frontend best-practices community: &lt;a href="https://discord.gg/d9KRhuAwQ" rel="noopener noreferrer"&gt;discord.gg/d9KRhuAwQ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📸 &lt;strong&gt;Instagram&lt;/strong&gt; — frontend best practices, daily: &lt;a href="https://www.instagram.com/bestpractice___/" rel="noopener noreferrer"&gt;@bestpractice___&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>A Typo Widened My Generic Instead of Erroring. NoInfer&lt;T&gt; Fixes It.</title>
      <dc:creator>Parsa J</dc:creator>
      <pubDate>Fri, 27 Nov 2026 11:45:27 +0000</pubDate>
      <link>https://dev.to/parsajiravand/a-typo-widened-my-generic-instead-of-erroring-noinfer-fixes-it-4565</link>
      <guid>https://dev.to/parsajiravand/a-typo-widened-my-generic-instead-of-erroring-noinfer-fixes-it-4565</guid>
      <description>&lt;p&gt;Someone on the team adds a new feature flag, wires up its default, and opens a PR:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;defineFlags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"darkMode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"newCheckout"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"betaSearch"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"darkmode"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;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 &lt;code&gt;"darkmode"&lt;/code&gt; — only &lt;code&gt;"darkMode"&lt;/code&gt;. The function whose entire job was to make this exact typo impossible let it straight through, silently.&lt;/p&gt;

&lt;p&gt;The fix is one word. Understanding why TypeScript needed it took longer.&lt;/p&gt;

&lt;h2&gt;
  
  
  The function looked airtight
&lt;/h2&gt;

&lt;p&gt;Here's roughly what &lt;code&gt;defineFlags&lt;/code&gt; looked like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;defineFlags&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;defaultFlag&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;defaultFlag&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Spot the bug yet? Try it before you scroll: paste this into a &lt;code&gt;.ts&lt;/code&gt; file and see what the compiler says.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;defineFlags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"darkMode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"newCheckout"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"betaSearch"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"newCheckout"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// fine&lt;/span&gt;
&lt;span class="nf"&gt;defineFlags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"darkMode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"newCheckout"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"betaSearch"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"darkmode"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// ???&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Zero errors. Both lines. The second one — the typo — compiles exactly as cleanly as the first.&lt;/p&gt;

&lt;h2&gt;
  
  
  What TypeScript was actually inferring
&lt;/h2&gt;

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

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;defineFlagsTyped&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;defaultFlag&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;defineFlagsTyped&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"darkMode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"newCheckout"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"betaSearch"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"darkmode"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Assigning the "real" union back reveals what Name actually became:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;check&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"darkMode"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"newCheckout"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"betaSearch"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Type '"darkMode" | "newCheckout" | "betaSearch" | "darkmode"'&lt;/span&gt;
&lt;span class="c1"&gt;// is not assignable to type '"darkMode" | "newCheckout" | "betaSearch"'.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Name&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  🎮 Try it yourself
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://bestpractic.org/blog/typescript-noinfer-generic-widening/playground" rel="noopener noreferrer"&gt;▶️ Open the interactive playground →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Runs right in your browser — poke at it and watch the concept react live.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix: tell TypeScript which position doesn't get a vote
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;defaultFlag&lt;/code&gt; was never supposed to help decide what &lt;code&gt;Name&lt;/code&gt; is. It was supposed to be checked &lt;em&gt;against&lt;/em&gt; a &lt;code&gt;Name&lt;/code&gt; that the array had already settled. That's exactly what &lt;code&gt;NoInfer&amp;lt;T&amp;gt;&lt;/code&gt; — built into TypeScript's standard library since 5.4 — exists to say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;defineFlags&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;defaultFlag&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="nc"&gt;NoInfer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;defaultFlag&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;defineFlags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"darkMode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"newCheckout"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"betaSearch"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"newCheckout"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// fine&lt;/span&gt;
&lt;span class="nf"&gt;defineFlags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"darkMode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"newCheckout"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"betaSearch"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"darkmode"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Argument of type '"darkmode"' is not assignable to parameter of type&lt;/span&gt;
&lt;span class="c1"&gt;// '"darkMode" | "newCheckout" | "betaSearch" | undefined'.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same call, same arguments — now the typo is a compile error, pointing at the exact argument, before the PR even opens.&lt;/p&gt;

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

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where it won't save you
&lt;/h2&gt;

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

&lt;p&gt;It also needs TypeScript 5.4 or newer — it shipped in March 2024 as a real compiler intrinsic (&lt;code&gt;type NoInfer&amp;lt;T&amp;gt; = intrinsic&lt;/code&gt;), not a type you could write yourself. Before 5.4, people approximated it with a conditional-type trick (&lt;code&gt;type NoInfer&amp;lt;T&amp;gt; = [T][T extends any ? 0 : never]&lt;/code&gt;), 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.&lt;/p&gt;

&lt;p&gt;And it won't flag a parameter you &lt;em&gt;forgot&lt;/em&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  The takeaway
&lt;/h2&gt;

&lt;p&gt;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 &lt;code&gt;NoInfer&amp;lt;T&amp;gt;&lt;/code&gt;, and it's safe to add retroactively: it only makes previously-accepted typos start failing, nothing that was correct before stops compiling.&lt;/p&gt;

&lt;p&gt;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?&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Test yourself
&lt;/h2&gt;

&lt;p&gt;Think it clicked? &lt;strong&gt;&lt;a href="https://bestpractic.org/blog/typescript-noinfer-generic-widening/quiz" rel="noopener noreferrer"&gt;Take the 8-question quiz →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  📚 Read next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/my-reducers-switch-stopped-being-exhaustive-as-const-fixed-it-4aa4"&gt;My Reducer's Switch Stopped Being Exhaustive. &lt;code&gt;as const&lt;/code&gt; Fixed It.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/stop-formatting-currency-with-regex-intlnumberformat-does-it-220b"&gt;Stop Formatting Currency With Regex. &lt;code&gt;Intl.NumberFormat&lt;/code&gt; Does It.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/your-typescript-config-objects-are-losing-type-information-satisfies-fixes-it-3hdh"&gt;Your TypeScript config objects are losing type information. &lt;code&gt;satisfies&lt;/code&gt; fixes it.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🚀 &lt;strong&gt;Want more like this?&lt;/strong&gt; Every guide, playground, and quiz lives on &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;bestpractic.org&lt;/a&gt;&lt;/strong&gt; — open it and &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;sign up free&lt;/a&gt;&lt;/strong&gt; so the next one finds you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading! Let's stay connected:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⭐ &lt;strong&gt;GitHub&lt;/strong&gt; — follow me and star the projects: &lt;a href="https://github.com/parsajiravand" rel="noopener noreferrer"&gt;github.com/parsajiravand&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;strong&gt;Discord&lt;/strong&gt; — join the frontend best-practices community: &lt;a href="https://discord.gg/d9KRhuAwQ" rel="noopener noreferrer"&gt;discord.gg/d9KRhuAwQ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📸 &lt;strong&gt;Instagram&lt;/strong&gt; — frontend best practices, daily: &lt;a href="https://www.instagram.com/bestpractice___/" rel="noopener noreferrer"&gt;@bestpractice___&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>NestJS Interceptor Order: Why Stacking Breaks Your Cache</title>
      <dc:creator>Parsa J</dc:creator>
      <pubDate>Fri, 27 Nov 2026 11:44:56 +0000</pubDate>
      <link>https://dev.to/parsajiravand/nestjs-interceptor-order-why-stacking-breaks-your-cache-1mjf</link>
      <guid>https://dev.to/parsajiravand/nestjs-interceptor-order-why-stacking-breaks-your-cache-1mjf</guid>
      <description>&lt;p&gt;User 42 fetches &lt;code&gt;GET /documents/7&lt;/code&gt;, the document they own. The response includes &lt;code&gt;isOwner: true&lt;/code&gt;. A minute later, user 7 — who has read access but doesn't own it — fetches the exact same document. The response &lt;em&gt;also&lt;/em&gt; says &lt;code&gt;isOwner: true&lt;/code&gt;. User 7 now sees an "Edit" button a permission check downstream will eventually reject, but the UI already lied to them, and a client that trusts &lt;code&gt;isOwner&lt;/code&gt; to gate a destructive action has a real authorization bug, not a cosmetic one.&lt;/p&gt;

&lt;p&gt;Nobody touched the authorization logic. Nobody touched the DTO. Both interceptors involved are individually correct, straight from patterns NestJS's own documentation shows. The bug is entirely in the order two &lt;code&gt;@UseInterceptors()&lt;/code&gt; arguments were listed — and until you know what that list actually builds, the order looks arbitrary.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you'll learn
&lt;/h2&gt;

&lt;p&gt;By the end of this article you'll be able to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explain what &lt;code&gt;intercept(context, next)&lt;/code&gt; and &lt;code&gt;CallHandler.handle()&lt;/code&gt; actually do to a request, not just that you're supposed to call them&lt;/li&gt;
&lt;li&gt;Predict the exact execution order when interceptors are stacked at the global, controller, and method level&lt;/li&gt;
&lt;li&gt;Recognize why an interceptor that skips &lt;code&gt;next.handle()&lt;/code&gt; (like a cache) silently skips every interceptor nested inside it too&lt;/li&gt;
&lt;li&gt;Fix a stale-personalization bug caused by interceptor order, instead of chasing it through the DTO and the authorization layer&lt;/li&gt;
&lt;li&gt;Choose between method-level &lt;code&gt;@UseInterceptors()&lt;/code&gt;, &lt;code&gt;app.useGlobalInterceptors()&lt;/code&gt;, and the &lt;code&gt;APP_INTERCEPTOR&lt;/code&gt; provider token&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Who this is for
&lt;/h2&gt;

&lt;p&gt;You've written at least one NestJS controller with &lt;code&gt;@UseGuards()&lt;/code&gt; or &lt;code&gt;@UseInterceptors()&lt;/code&gt; on it, even if you copied the interceptor from the docs without fully tracing what it does. You don't need prior RxJS experience — the handful of operators used here (&lt;code&gt;tap&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;catchError&lt;/code&gt;, &lt;code&gt;of&lt;/code&gt;) are explained as they appear.&lt;/p&gt;

&lt;p&gt;This article is written against &lt;strong&gt;NestJS 12.x&lt;/strong&gt; (&lt;code&gt;@nestjs/core&lt;/code&gt; is at &lt;code&gt;12.6.0&lt;/code&gt; on npm &lt;code&gt;latest&lt;/code&gt;, with &lt;code&gt;11.2.11&lt;/code&gt; the last &lt;code&gt;legacy&lt;/code&gt;-tagged 11.x release). The interceptor API — &lt;code&gt;NestInterceptor&lt;/code&gt;, &lt;code&gt;CallHandler&lt;/code&gt;, &lt;code&gt;ExecutionContext&lt;/code&gt; — and the global → controller → route composition order are core-container behavior, unchanged in shape since well before v12.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The problem: a cache that leaks the wrong user's permissions&lt;/li&gt;
&lt;li&gt;The mental model: interceptors are nested, not listed&lt;/li&gt;
&lt;li&gt;Stage 1: what &lt;code&gt;next.handle()&lt;/code&gt; actually is&lt;/li&gt;
&lt;li&gt;Stage 2: composing two interceptors by hand&lt;/li&gt;
&lt;li&gt;Stage 3: the cache-and-personalization bug, reproduced&lt;/li&gt;
&lt;li&gt;Stage 4: global, controller, and method — the fixed nesting&lt;/li&gt;
&lt;li&gt;Stage 5: binding without breaking DI&lt;/li&gt;
&lt;li&gt;Edge cases and gotchas&lt;/li&gt;
&lt;li&gt;Best practices&lt;/li&gt;
&lt;li&gt;FAQ&lt;/li&gt;
&lt;li&gt;Cheat sheet&lt;/li&gt;
&lt;li&gt;Key takeaways&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The problem: a cache that leaks the wrong user's permissions
&lt;/h2&gt;

&lt;p&gt;Here's the setup that produced the bug above. Two interceptors, each individually correct and each a straightforward extension of a pattern NestJS's own documentation shows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Adds a per-request field: does the CURRENT caller own this document?&lt;/span&gt;
&lt;span class="nd"&gt;@Injectable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OwnershipInterceptor&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;NestInterceptor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;intercept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ExecutionContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CallHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;switchToHttp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;isOwner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ownerId&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Caches the raw document by id — the document itself is the same for&lt;/span&gt;
&lt;span class="c1"&gt;// every viewer, so it's cached once, regardless of who's asking.&lt;/span&gt;
&lt;span class="nd"&gt;@Injectable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CacheInterceptor&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;NestInterceptor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CacheStore&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;intercept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ExecutionContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CallHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;switchToHttp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both are individually reasonable: caching a shared resource by its id is a normal optimization, and stamping a per-caller &lt;code&gt;isOwner&lt;/code&gt; flag onto a response is a normal personalization. Bound on the controller like this, reading top to bottom as "first cache, then personalize":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nd"&gt;@UseInterceptors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CacheInterceptor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;OwnershipInterceptor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;':id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;documents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;User 42 (the owner) requests document 7, a cache miss: &lt;code&gt;CacheInterceptor&lt;/code&gt; calls &lt;code&gt;next.handle()&lt;/code&gt;, which runs &lt;code&gt;OwnershipInterceptor&lt;/code&gt;, which runs the handler, gets back &lt;code&gt;{ id: 7, ownerId: 42 }&lt;/code&gt;, and stamps &lt;code&gt;isOwner: true&lt;/code&gt; because the current request's user is 42. &lt;code&gt;CacheInterceptor&lt;/code&gt;'s &lt;code&gt;tap()&lt;/code&gt; then caches that &lt;em&gt;already-personalized&lt;/em&gt; object — &lt;code&gt;{ id: 7, ownerId: 42, isOwner: true }&lt;/code&gt; — keyed only by the document id.&lt;/p&gt;

&lt;p&gt;User 7 (not the owner) requests the same document 7 next: &lt;code&gt;CacheInterceptor&lt;/code&gt; finds a cached entry for key &lt;code&gt;7&lt;/code&gt; and returns &lt;code&gt;of(cached)&lt;/code&gt; directly — it never calls &lt;code&gt;next.handle()&lt;/code&gt;, so &lt;code&gt;OwnershipInterceptor&lt;/code&gt; never runs for &lt;em&gt;this&lt;/em&gt; request. The response sent to user 7 is the exact object cached from user 42's request: &lt;code&gt;isOwner: true&lt;/code&gt;. User 7 is told they own a document they don't, because the only code that could have corrected the flag for the new caller never ran.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mental model: interceptors are nested, not listed
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The mental model:&lt;/strong&gt; &lt;code&gt;@UseInterceptors(A, B)&lt;/code&gt; does not run &lt;code&gt;A&lt;/code&gt; then &lt;code&gt;B&lt;/code&gt; as two independent steps. It builds &lt;code&gt;A&lt;/code&gt; wrapping &lt;code&gt;B&lt;/code&gt; wrapping the handler — one nested call, not a flat list. The first interceptor you name becomes the &lt;strong&gt;outermost&lt;/strong&gt; layer; the last one you name sits &lt;strong&gt;closest to the handler&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This matters because &lt;code&gt;next.handle()&lt;/code&gt; is not "move to the next item in a list." It is "invoke everything nested inside me, and give me back its result as an &lt;code&gt;Observable&lt;/code&gt;." When &lt;code&gt;A&lt;/code&gt; calls &lt;code&gt;next.handle()&lt;/code&gt;, that call &lt;em&gt;is&lt;/em&gt; &lt;code&gt;B&lt;/code&gt;'s entire &lt;code&gt;intercept()&lt;/code&gt; execution, including whatever &lt;code&gt;B&lt;/code&gt; does before and after its own &lt;code&gt;next.handle()&lt;/code&gt;. If &lt;code&gt;A&lt;/code&gt; never calls &lt;code&gt;next.handle()&lt;/code&gt; — because it short-circuits, exactly like &lt;code&gt;CacheInterceptor&lt;/code&gt; does on a hit — then &lt;code&gt;B&lt;/code&gt; never runs. Not "runs with stale data." Does not run, at all, on that request.&lt;/p&gt;

&lt;p&gt;This is the same onion composition Express and Koa middleware use, and it is also how NestJS composes guards, interceptors, and pipes across scopes (confirmed by the framework's own request-lifecycle documentation): code that runs &lt;em&gt;before&lt;/em&gt; the nested call executes in registration order, outermost first; code that runs &lt;em&gt;after&lt;/em&gt; — the RxJS operators chained onto the &lt;code&gt;Observable&lt;/code&gt; that &lt;code&gt;next.handle()&lt;/code&gt; returns — unwinds in the reverse order, innermost first. Draw it as concentric rings, not a pipeline of boxes, and the cache bug above stops being surprising: &lt;code&gt;CacheInterceptor&lt;/code&gt; is the outer ring, so skipping its &lt;code&gt;next.handle()&lt;/code&gt; cuts out everything inside that ring, &lt;code&gt;OwnershipInterceptor&lt;/code&gt; included.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 1: what &lt;code&gt;next.handle()&lt;/code&gt; actually is
&lt;/h2&gt;

&lt;p&gt;Every &lt;code&gt;NestInterceptor&lt;/code&gt; implements one method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;NestInterceptor&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;R&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;intercept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ExecutionContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CallHandler&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;context: ExecutionContext&lt;/code&gt; is the same object a guard sees — it carries the request, the controller class, and the handler reference. &lt;code&gt;next: CallHandler&lt;/code&gt; has exactly one method, &lt;code&gt;handle()&lt;/code&gt;, which invokes the rest of the chain (the next interceptor, or the route handler if there is none left) and returns an &lt;code&gt;Observable&lt;/code&gt; of its eventual result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key concept:&lt;/strong&gt; code written &lt;em&gt;before&lt;/em&gt; &lt;code&gt;return next.handle()...&lt;/code&gt; runs before the handler. Code chained onto the &lt;code&gt;Observable&lt;/code&gt; that &lt;code&gt;next.handle()&lt;/code&gt; returns — via RxJS operators like &lt;code&gt;tap&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, or &lt;code&gt;catchError&lt;/code&gt; — runs after the handler resolves, and can transform, log, or replace its result. If an interceptor never calls &lt;code&gt;next.handle()&lt;/code&gt; and never returns an equivalent &lt;code&gt;Observable&lt;/code&gt;, the request hangs forever: no response is ever emitted, because nothing inside Nest's response pipeline was asked to produce one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Injectable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoggingInterceptor&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;NestInterceptor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;intercept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ExecutionContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CallHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`${context.getHandler().name} took ${Date.now() - start}ms`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the minimal correct shape: do setup, call &lt;code&gt;next.handle()&lt;/code&gt;, pipe a side effect onto the result, return it unchanged. Nothing here reorders anything else — it's a single ring with nothing nested complicated inside it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 2: composing two interceptors by hand
&lt;/h2&gt;

&lt;p&gt;To see the nesting directly, strip away the decorator and write what &lt;code&gt;@UseInterceptors(A, B)&lt;/code&gt; effectively builds for a single request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;runStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;interceptors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NestInterceptor&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ExecutionContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;terminal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CallHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// Build from the LAST interceptor outward, so the FIRST interceptor ends up outermost.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;chain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;interceptors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reduceRight&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CallHandler&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;interceptor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;interceptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;intercept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;terminal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;reduceRight&lt;/code&gt; walks the array back-to-front, so the &lt;em&gt;last&lt;/em&gt; interceptor in the array is wrapped first (innermost), and each one before it wraps the previous result — leaving the &lt;em&gt;first&lt;/em&gt; interceptor in the array as the outermost &lt;code&gt;CallHandler.handle()&lt;/code&gt; that actually gets invoked. That's a simplified model of the mechanism behind "first named, outermost" — Nest's own implementation builds the same nesting through lazy, forward recursion rather than &lt;code&gt;reduceRight&lt;/code&gt;, but the resulting order is identical: first interceptor outermost, applied once per scope level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 3: the cache-and-personalization bug, reproduced
&lt;/h2&gt;

&lt;p&gt;Swap the order from the problem section, and the leak closes, because now &lt;code&gt;OwnershipInterceptor&lt;/code&gt; is outermost and &lt;code&gt;CacheInterceptor&lt;/code&gt; is innermost:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nd"&gt;@UseInterceptors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OwnershipInterceptor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;CacheInterceptor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;':id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;documents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Walk the same two requests through this order. User 42, cache miss: &lt;code&gt;OwnershipInterceptor&lt;/code&gt; (outer) calls &lt;code&gt;next.handle()&lt;/code&gt;, which reaches &lt;code&gt;CacheInterceptor&lt;/code&gt; (inner), which misses, calls the handler, gets &lt;code&gt;{ id: 7, ownerId: 42 }&lt;/code&gt;, and caches &lt;em&gt;that raw object&lt;/em&gt; — no &lt;code&gt;isOwner&lt;/code&gt; field at all, because personalization hasn't happened yet at this ring. The raw object flows back out to &lt;code&gt;OwnershipInterceptor&lt;/code&gt;, which stamps &lt;code&gt;isOwner: true&lt;/code&gt; for user 42. Response: &lt;code&gt;{ id: 7, ownerId: 42, isOwner: true }&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;User 7, same document: &lt;code&gt;OwnershipInterceptor&lt;/code&gt; (outer) still calls &lt;code&gt;next.handle()&lt;/code&gt; — it has no reason not to, and nothing outside it short-circuited. &lt;code&gt;CacheInterceptor&lt;/code&gt; (inner) finds a hit and returns the cached &lt;em&gt;raw&lt;/em&gt; object without running the handler again. That raw object flows back out to &lt;code&gt;OwnershipInterceptor&lt;/code&gt;, which — because it is never skipped when it's the outer ring — stamps a fresh &lt;code&gt;isOwner&lt;/code&gt; field for &lt;em&gt;this&lt;/em&gt; request's user: &lt;code&gt;7 === 42&lt;/code&gt; is false. Response: &lt;code&gt;{ id: 7, ownerId: 42, isOwner: false }&lt;/code&gt;. Correct, every time, for every caller, regardless of which request happened to populate the cache.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key concept:&lt;/strong&gt; a ring that must re-evaluate something per request — who's asking, what they're allowed to see — has to sit &lt;em&gt;outside&lt;/em&gt; anything that can short-circuit, never inside it. &lt;code&gt;CacheInterceptor&lt;/code&gt;'s short-circuit only ever skips what's nested inside &lt;code&gt;CacheInterceptor&lt;/code&gt;; it can never skip a ring wrapped around it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 4: global, controller, and method — the fixed nesting
&lt;/h2&gt;

&lt;p&gt;The same onion extends across scopes, and you cannot reorder it locally. For a single inbound request, Nest's own request-lifecycle documentation puts interceptors in this order going in, and the exact reverse coming back out:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Global&lt;/strong&gt; interceptors (bound via &lt;code&gt;APP_INTERCEPTOR&lt;/code&gt; or &lt;code&gt;app.useGlobalInterceptors()&lt;/code&gt;) — outermost.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Controller-level&lt;/strong&gt; interceptors (&lt;code&gt;@UseInterceptors()&lt;/code&gt; on the class).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Method-level&lt;/strong&gt; interceptors (&lt;code&gt;@UseInterceptors()&lt;/code&gt; on the route handler) — innermost, closest to the actual handler.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;On the way back out, it's the reverse: method-level interceptors' post-handler logic runs first, then controller-level, then global last. A method-level &lt;code&gt;CacheInterceptor&lt;/code&gt; can never be made to wrap a global &lt;code&gt;LoggingInterceptor&lt;/code&gt;, no matter which order you list decorators in — global is always the outer ring. If you need a cache to be able to skip a cross-cutting concern like the global logger, the logger has to not depend on seeing every response in the first place, or the cache has to move to a scope at or above it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stage 5: binding without breaking DI
&lt;/h2&gt;

&lt;p&gt;Three ways to attach an interceptor, each with a different relationship to Nest's DI container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Method or controller scope — Nest constructs it through DI&lt;/span&gt;
&lt;span class="nd"&gt;@UseInterceptors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CacheInterceptor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Global, but constructed by hand — no constructor injection works here&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useGlobalInterceptors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LoggingInterceptor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Global, constructed through DI — constructor injection, module scope, and&lt;/span&gt;
&lt;span class="c1"&gt;// test overrides via overrideProvider() all keep working&lt;/span&gt;
&lt;span class="n"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;APP_INTERCEPTOR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CacheInterceptor&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;CacheInterceptor&lt;/code&gt; above takes a &lt;code&gt;CacheStore&lt;/code&gt; in its constructor. &lt;code&gt;app.useGlobalInterceptors(new CacheInterceptor(???))&lt;/code&gt; has no way to provide that dependency — you're back to constructing it by hand outside the container, the same trap &lt;code&gt;app.useGlobalGuards()&lt;/code&gt; sets for a guard that needs a service injected. &lt;code&gt;APP_INTERCEPTOR&lt;/code&gt; fixes it the same way &lt;code&gt;APP_GUARD&lt;/code&gt; does: register it as a provider, let Nest instantiate it through DI, and it becomes global while still receiving its dependencies — and while still being swappable in tests with &lt;code&gt;Test.createTestingModule().overrideProvider(CacheStore)&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Edge cases and gotchas
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;An interceptor that never calls &lt;code&gt;next.handle()&lt;/code&gt; still has to return something RxJS can treat as a stream.&lt;/strong&gt; Return a bare value, or nothing at all, and Nest throws immediately — a &lt;code&gt;TypeError&lt;/code&gt; like "You provided 'undefined' where a stream was expected" — which lands in your exception filters. It doesn't hang; there's no silent stall to debug. If you're building a caching or short-circuiting interceptor, always return an &lt;code&gt;Observable&lt;/code&gt; explicitly — &lt;code&gt;of(value)&lt;/code&gt; is the simplest correct shape.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A synchronous throw inside &lt;code&gt;intercept()&lt;/code&gt; doesn't just skip straight past every interceptor wrapped around it.&lt;/strong&gt; Nest invokes each interceptor inside a &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt; and forwards a synchronous throw via the subscriber's &lt;code&gt;error()&lt;/code&gt; callback on the &lt;code&gt;Observable&lt;/code&gt; that invocation built — which is exactly the &lt;code&gt;Observable&lt;/code&gt; an &lt;em&gt;outer&lt;/em&gt; interceptor's &lt;code&gt;next.handle()&lt;/code&gt; returned. So even a throw that happens &lt;em&gt;before&lt;/em&gt; this interceptor's own &lt;code&gt;next.handle()&lt;/code&gt; call surfaces as a stream error there, and that outer interceptor's &lt;code&gt;catchError()&lt;/code&gt; can and does see it. The error only lands directly in Nest's exception filters, the same as a guard or pipe error, when nothing further out is there to catch it (the throw happens in the outermost interceptor, or no outer interceptor pipes a &lt;code&gt;catchError&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple method-level interceptors still nest in the order you list them&lt;/strong&gt;, exactly like the global/controller/method scopes do — &lt;code&gt;@UseInterceptors(A, B, C)&lt;/code&gt; makes &lt;code&gt;A&lt;/code&gt; outermost and &lt;code&gt;C&lt;/code&gt; innermost, all at the same method scope.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;async intercept()&lt;/code&gt; works fine, even though it technically returns a &lt;code&gt;Promise&lt;/code&gt; instead of an &lt;code&gt;Observable&lt;/code&gt;.&lt;/strong&gt; Nest resolves that &lt;code&gt;Promise&lt;/code&gt; before treating the result as a stream, so &lt;code&gt;async intercept(context, next) { await doSomething(); return next.handle().pipe(map(...)); }&lt;/code&gt; behaves correctly — you don't need &lt;code&gt;from()&lt;/code&gt; just to use &lt;code&gt;await&lt;/code&gt; inside an interceptor.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unsubscribing from a timeout doesn't cancel work the handler already started.&lt;/strong&gt; A &lt;code&gt;timeout(5000)&lt;/code&gt; operator makes Nest stop &lt;em&gt;waiting&lt;/em&gt;, but if the handler kicked off a database write before the timeout fired, that write still runs to completion in the background.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Order by scope first, list order second.&lt;/strong&gt; Decide which cross-cutting concerns (logging, auth context, response envelope) must see &lt;em&gt;every&lt;/em&gt; response regardless of what a more specific interceptor does, and put those at the outer scope — global over controller over method — rather than trying to win with list order inside one scope.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Short-circuiting interceptors (cache, dedupe) belong as far inward as the response contract allows&lt;/strong&gt;, nested &lt;em&gt;inside&lt;/em&gt; anything that must re-evaluate per caller — authorization flags, user-specific fields, anything derived from "who is asking."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prefer &lt;code&gt;APP_INTERCEPTOR&lt;/code&gt; over &lt;code&gt;app.useGlobalInterceptors()&lt;/code&gt; the moment an interceptor has a constructor dependency.&lt;/strong&gt; It costs nothing and keeps the interceptor testable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep one interceptor doing one thing.&lt;/strong&gt; A single interceptor that logs, caches, &lt;em&gt;and&lt;/em&gt; transforms is unreadable to reorder safely later — the stacking bug above is much easier to spot in review when each interceptor is a named, single-purpose ring.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When a bug looks like "sometimes the response has stale or wrong per-user data," suspect interceptor ordering before suspecting the data layer.&lt;/strong&gt; A field that's correct for one caller and wrong for the next, correlated with a cache hit, is a strong signal to check which interceptor is nested inside which.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Does interceptor order matter if neither one short-circuits?
&lt;/h3&gt;

&lt;p&gt;Less, but not never. If both interceptors always call &lt;code&gt;next.handle()&lt;/code&gt; and only add side effects or transforms, the &lt;em&gt;pre-handler&lt;/em&gt; logic still runs outer-to-inner and the &lt;em&gt;post-handler&lt;/em&gt; logic still runs inner-to-outer — so a &lt;code&gt;tap()&lt;/code&gt; that logs "before" timing in one interceptor and a &lt;code&gt;map()&lt;/code&gt; that reshapes data in another can still produce a different final object depending on which one is outermost, even though both always run.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I make a method-level interceptor run before a global one?
&lt;/h3&gt;

&lt;p&gt;No. The scope ordering (global, then controller, then method) is fixed by Nest's request pipeline and isn't something &lt;code&gt;@UseInterceptors()&lt;/code&gt; list order can override — list order only controls nesting &lt;em&gt;within&lt;/em&gt; a single scope.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's the difference between an interceptor and a guard for this kind of short-circuiting?
&lt;/h3&gt;

&lt;p&gt;A guard's &lt;code&gt;canActivate()&lt;/code&gt; returns a boolean before any interceptor runs at all — it rejects or allows the whole request, with no concept of wrapping a result. An interceptor wraps the &lt;code&gt;Observable&lt;/code&gt; the handler (or the next interceptor) produces, so it can transform, log, cache, or replace that result, but by the time an interceptor runs, a guard has already allowed the request through.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does &lt;code&gt;ClassSerializerInterceptor&lt;/code&gt; have the same ordering rules?
&lt;/h3&gt;

&lt;p&gt;Yes — it's a regular interceptor, usually bound via &lt;code&gt;APP_INTERCEPTOR&lt;/code&gt;, so it sits at whatever scope you register it at and follows the same nesting as any other interceptor relative to others at different scopes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Will a new NestJS version change this ordering?
&lt;/h3&gt;

&lt;p&gt;The global → controller → method scope order and the onion composition are structural to how Nest builds the pipeline, not a detail that has changed across the 10.x–12.x line. Always verify behavior you depend on against the version you're running if it matters for production correctness.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task&lt;/th&gt;
&lt;th&gt;Code&lt;/th&gt;
&lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Define an interceptor&lt;/td&gt;
&lt;td&gt;&lt;code&gt;intercept(context: ExecutionContext, next: CallHandler): Observable&amp;lt;any&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Must return an &lt;code&gt;Observable&lt;/code&gt;, usually derived from &lt;code&gt;next.handle()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Run the handler&lt;/td&gt;
&lt;td&gt;&lt;code&gt;next.handle()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns an &lt;code&gt;Observable&lt;/code&gt; of the handler's (or next interceptor's) result&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Transform the response&lt;/td&gt;
&lt;td&gt;&lt;code&gt;next.handle().pipe(map(data =&amp;gt; ({ data })))&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Runs after the handler, on the way back out&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Log around the handler&lt;/td&gt;
&lt;td&gt;&lt;code&gt;next.handle().pipe(tap(() =&amp;gt; log()))&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;tap&lt;/code&gt; doesn't change the emitted value&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Short-circuit (skip the handler)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;return of(cachedValue)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Never calls &lt;code&gt;next.handle()&lt;/code&gt; — nothing nested inside this interceptor runs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Map errors&lt;/td&gt;
&lt;td&gt;&lt;code&gt;next.handle().pipe(catchError(() =&amp;gt; throwError(() =&amp;gt; new BadGatewayException())))&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Only catches errors from inside &lt;code&gt;next.handle()&lt;/code&gt;, not thrown before it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Add a timeout&lt;/td&gt;
&lt;td&gt;&lt;code&gt;next.handle().pipe(timeout(5000), catchError(mapTimeoutErr))&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Stops waiting; does not cancel in-flight work the handler started&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bind at method/controller scope&lt;/td&gt;
&lt;td&gt;&lt;code&gt;@UseInterceptors(A, B)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;A&lt;/code&gt; wraps &lt;code&gt;B&lt;/code&gt;: &lt;code&gt;A&lt;/code&gt; is outer, &lt;code&gt;B&lt;/code&gt; is inner&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bind globally, no DI&lt;/td&gt;
&lt;td&gt;&lt;code&gt;app.useGlobalInterceptors(new X())&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Constructor dependencies cannot be injected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bind globally, with DI&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;{ provide: APP_INTERCEPTOR, useClass: X }&lt;/code&gt; in a module's &lt;code&gt;providers&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Outermost of all scopes; still goes through the container&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execution order (inbound)&lt;/td&gt;
&lt;td&gt;global → controller → method → handler&lt;/td&gt;
&lt;td&gt;Fixed; list order only applies within one scope&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execution order (outbound)&lt;/td&gt;
&lt;td&gt;handler → method → controller → global&lt;/td&gt;
&lt;td&gt;Exact reverse of inbound&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@UseInterceptors(A, B)&lt;/code&gt; nests &lt;code&gt;A&lt;/code&gt; around &lt;code&gt;B&lt;/code&gt;, not "A then B" — the first interceptor named is outermost.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;next.handle()&lt;/code&gt; invokes everything nested inside the current interceptor; skip it, and nothing inside that ring runs — including an interceptor that was supposed to apply to every response.&lt;/li&gt;
&lt;li&gt;Global interceptors always wrap controller interceptors, which always wrap method interceptors. Scope order is fixed; list order only matters within a scope.&lt;/li&gt;
&lt;li&gt;A per-caller field (ownership, role, visibility) that's wrong specifically on a cache hit is a strong signal to check interceptor nesting before the data layer.&lt;/li&gt;
&lt;li&gt;Reach for &lt;code&gt;APP_INTERCEPTOR&lt;/code&gt; the moment an interceptor needs a constructor dependency — &lt;code&gt;app.useGlobalInterceptors(new X())&lt;/code&gt; can't provide one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🎮 Try it yourself
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://bestpractic.org/blog/nestjs-weekly-interceptor-order/playground" rel="noopener noreferrer"&gt;▶️ Open the interactive playground →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Runs right in your browser — poke at it and watch the concept react live.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Interceptor nesting is easier to see than to read about. The playground below lets you pick the order of a &lt;code&gt;CacheInterceptor&lt;/code&gt; and an &lt;code&gt;OwnershipInterceptor&lt;/code&gt;, send a simulated request from the document's owner followed by a request from a different user for the same document, and watch exactly which ring the second request never reaches.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Test yourself
&lt;/h2&gt;

&lt;p&gt;Think it clicked? &lt;strong&gt;&lt;a href="https://bestpractic.org/blog/nestjs-weekly-interceptor-order/quiz" rel="noopener noreferrer"&gt;Take the 9-question quiz →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This series has already covered two other pieces of the same request pipeline: &lt;a href="https://dev.to/parsajiravand/nestjs-guards-canactivate-executioncontext-reflector-1onm"&gt;how guards decide who reaches a handler&lt;/a&gt;, and &lt;a href="https://dev.to/parsajiravand/nestjs-request-lifecycle-explained-with-cheat-sheet-227l"&gt;the full middleware → guard → interceptor → pipe → handler order&lt;/a&gt; interceptors are one stop inside. If constructor injection into a provider like &lt;code&gt;CacheStore&lt;/code&gt; isn't fully clear yet, &lt;a href="https://dev.to/parsajiravand/nestjs-dependency-injection-explained-with-cheat-sheet-4bem"&gt;the DI container episode&lt;/a&gt; builds that mental model from scratch.&lt;/p&gt;

&lt;p&gt;So: has an interceptor you stacked ever produced a response that looked right for one caller and wrong for the next, with nothing in the data layer to blame? What gave it away first — the client, a test, or a support ticket?&lt;/p&gt;

&lt;h2&gt;
  
  
  📚 Read next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/nestjs-12-lifecycle-hooks-the-order-you-cant-assume-amn"&gt;NestJS 12 Lifecycle Hooks: The Order You Can't Assume&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/nestjs-12-standard-schema-validation-without-class-validator-161f"&gt;NestJS 12: Standard Schema Validation Without class-validator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/nestjs-testing-module-provider-overrides-with-cheat-sheet-1o79"&gt;NestJS Testing Module: Provider Overrides (with Cheat Sheet)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🚀 &lt;strong&gt;Want more like this?&lt;/strong&gt; Every guide, playground, and quiz lives on &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;bestpractic.org&lt;/a&gt;&lt;/strong&gt; — open it and &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;sign up free&lt;/a&gt;&lt;/strong&gt; so the next one finds you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading! Let's stay connected:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⭐ &lt;strong&gt;GitHub&lt;/strong&gt; — follow me and star the projects: &lt;a href="https://github.com/parsajiravand" rel="noopener noreferrer"&gt;github.com/parsajiravand&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;strong&gt;Discord&lt;/strong&gt; — join the frontend best-practices community: &lt;a href="https://discord.gg/d9KRhuAwQ" rel="noopener noreferrer"&gt;discord.gg/d9KRhuAwQ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📸 &lt;strong&gt;Instagram&lt;/strong&gt; — frontend best practices, daily: &lt;a href="https://www.instagram.com/bestpractice___/" rel="noopener noreferrer"&gt;@bestpractice___&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>nestjs</category>
      <category>node</category>
      <category>typescript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>arr[i] Isn't Always There. noUncheckedIndexedAccess Says So.</title>
      <dc:creator>Parsa J</dc:creator>
      <pubDate>Fri, 27 Nov 2026 11:44:26 +0000</pubDate>
      <link>https://dev.to/parsajiravand/arri-isnt-always-there-nouncheckedindexedaccess-says-so-4dfp</link>
      <guid>https://dev.to/parsajiravand/arri-isnt-always-there-nouncheckedindexedaccess-says-so-4dfp</guid>
      <description>&lt;p&gt;Three lines of TypeScript. Same shape, same compiler, same &lt;code&gt;strict: true&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// User | undefined&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// User | undefined&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;user3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;usersById&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// User&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first two make you prove the user exists before you touch &lt;code&gt;.name&lt;/code&gt;. The third doesn't. It's typed as a plain &lt;code&gt;User&lt;/code&gt; — no &lt;code&gt;| undefined&lt;/code&gt;, no error, no hint that anything could go wrong. Ship it, and the one time &lt;code&gt;id&lt;/code&gt; doesn't match a key, you get &lt;code&gt;Cannot read properties of undefined (reading 'name')&lt;/code&gt; in production, from a line TypeScript signed off on.&lt;/p&gt;

&lt;p&gt;That's not a fluke in one lib's types. It's the default behavior of &lt;code&gt;[]&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The obvious fix that doesn't fix it
&lt;/h2&gt;

&lt;p&gt;Say your team hits this bug, traces it back, and "fixes" the type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;UsersById&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;usersById&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UsersById&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;buildIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;user3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;usersById&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;user3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// still typed `User`, still no warning&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing changes. &lt;code&gt;Record&amp;lt;string, User&amp;gt;&lt;/code&gt; tells TypeScript that &lt;em&gt;every&lt;/em&gt; string key maps to a &lt;code&gt;User&lt;/code&gt; — which is exactly the lie that caused the bug. An index signature isn't a promise about what's actually in the object; it's a promise about what &lt;em&gt;could&lt;/em&gt; be, and TypeScript takes it at face value for every key you ask for, including ones you made up at runtime from user input, a URL param, or a cache miss.&lt;/p&gt;

&lt;p&gt;So the team does what teams do under a ticket deadline: they start writing &lt;code&gt;usersById[id]!.name&lt;/code&gt;. The &lt;code&gt;!&lt;/code&gt; tells the compiler "trust me, it's there." It compiles. It ships. It's the exact same crash, now with the compiler's blessing instead of its silence.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's actually going on
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;arr[i]&lt;/code&gt; and &lt;code&gt;obj[key]&lt;/code&gt; are &lt;strong&gt;indexed access&lt;/strong&gt;, and TypeScript treats indexed access as a guarantee by default: give it a key of the right type, get back a value of the value type, full stop. It doesn't matter that &lt;code&gt;i&lt;/code&gt; could be &lt;code&gt;arr.length&lt;/code&gt; or that &lt;code&gt;key&lt;/code&gt; could be a string nobody ever inserted — the type signature only describes the shape of a successful lookup, never the possibility of a missing one.&lt;/p&gt;

&lt;p&gt;Compare that to &lt;code&gt;Array.prototype.at()&lt;/code&gt;, which TypeScript's own lib types declare as &lt;code&gt;at(index: number): T | undefined&lt;/code&gt; — not because of any special flag, just because that method's type was written honestly from the start. Or &lt;code&gt;Map.get()&lt;/code&gt;, typed &lt;code&gt;V | undefined&lt;/code&gt; for the same reason. Those two already tell you the truth. Bracket indexing is the one place TypeScript picked optimism over accuracy, and it's been that way since index signatures existed — &lt;code&gt;strict: true&lt;/code&gt; never touched it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix that's actually a compiler flag
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json-doc"&gt;&lt;code&gt;&lt;span class="c1"&gt;// tsconfig.json&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"noUncheckedIndexedAccess"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Flip this on (it shipped in TypeScript 4.1, and it is &lt;em&gt;not&lt;/em&gt; bundled into &lt;code&gt;strict&lt;/code&gt;, so turning on strict mode alone will never give you this) and both lines above change shape:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;user3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;usersById&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// User | undefined&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;someArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// T | undefined&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                  &lt;span class="c1"&gt;// still `string` — fixed tuple positions are exempt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice what &lt;em&gt;doesn't&lt;/em&gt; change: a tuple with a literal, in-bounds index stays exact, because TypeScript can actually verify that position exists at compile time. The flag only widens the cases where it can't — variable array indices and index-signature lookups — which is precisely where the runtime risk lives.&lt;/p&gt;

&lt;h2&gt;
  
  
  Turning it on is the easy part
&lt;/h2&gt;

&lt;p&gt;The honest downside: flip this in an existing codebase and you'll likely get a wave of new "possibly undefined" errors, all at once, in code that worked yesterday. That's not the flag being noisy. That's every place your code was already one bad lookup away from the same crash, surfacing at once instead of one ticket at a time.&lt;/p&gt;

&lt;p&gt;Resist the urge to &lt;code&gt;!&lt;/code&gt; your way through the list. Each one wants a real answer, and the right one depends on where you are in the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// You can supply a sane fallback&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s1"&gt;'untagged'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// You're about to use it repeatedly — narrow once&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;user3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;usersById&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;user3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;notFound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;user3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// User, safely&lt;/span&gt;

&lt;span class="c1"&gt;// You actually know more than the type system does&lt;/span&gt;
&lt;span class="c1"&gt;// (a loop invariant, a length check two lines up) — rare, and worth a comment&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// items.length checked above&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;!&lt;/code&gt; isn't banned — it's just supposed to be the least common answer, reserved for the handful of spots where you can point at &lt;em&gt;why&lt;/em&gt; it's safe. If every possibly-undefined error in your list gets the same &lt;code&gt;!&lt;/code&gt;, you haven't fixed anything; you've just moved the crash from "silent" to "asserted."&lt;/p&gt;

&lt;h2&gt;
  
  
  🎮 Try it yourself
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://bestpractic.org/blog/no-unchecked-indexed-access/playground" rel="noopener noreferrer"&gt;▶️ Open the interactive playground →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Runs right in your browser — poke at it and watch the concept react live.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The one-line trust exercise
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;noUncheckedIndexedAccess&lt;/code&gt; doesn't change a single byte of your compiled JavaScript. The runtime behavior of &lt;code&gt;arr[i]&lt;/code&gt; was never in question — it was always capable of returning &lt;code&gt;undefined&lt;/code&gt;. All the flag does is make the type checker stop pretending otherwise, for the one kind of access it had been quietly exempting from its own rules.&lt;/p&gt;

&lt;p&gt;If your &lt;code&gt;tsconfig.json&lt;/code&gt; doesn't have it yet, that's worth fixing before the next &lt;code&gt;usersById[id]&lt;/code&gt; ships. What's the last production error you traced back to a lookup TypeScript swore couldn't fail?&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Test yourself
&lt;/h2&gt;

&lt;p&gt;Think it clicked? &lt;strong&gt;&lt;a href="https://bestpractic.org/blog/no-unchecked-indexed-access/quiz" rel="noopener noreferrer"&gt;Take the 7-question quiz →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  📚 Read next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/stop-formatting-currency-with-regex-intlnumberformat-does-it-220b"&gt;Stop Formatting Currency With Regex. &lt;code&gt;Intl.NumberFormat&lt;/code&gt; Does It.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/inferred-type-predicates-wont-save-filterboolean-heres-what-will-2dok"&gt;Inferred Type Predicates Won't Save filter(Boolean). Here's What Will.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/adddays-mutated-a-date-three-components-away-from-where-i-called-it-3400"&gt;addDays() Mutated a Date Three Components Away From Where I Called It&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🚀 &lt;strong&gt;Want more like this?&lt;/strong&gt; Every guide, playground, and quiz lives on &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;bestpractic.org&lt;/a&gt;&lt;/strong&gt; — open it and &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;sign up free&lt;/a&gt;&lt;/strong&gt; so the next one finds you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading! Let's stay connected:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⭐ &lt;strong&gt;GitHub&lt;/strong&gt; — follow me and star the projects: &lt;a href="https://github.com/parsajiravand" rel="noopener noreferrer"&gt;github.com/parsajiravand&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;strong&gt;Discord&lt;/strong&gt; — join the frontend best-practices community: &lt;a href="https://discord.gg/d9KRhuAwQ" rel="noopener noreferrer"&gt;discord.gg/d9KRhuAwQ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📸 &lt;strong&gt;Instagram&lt;/strong&gt; — frontend best practices, daily: &lt;a href="https://www.instagram.com/bestpractice___/" rel="noopener noreferrer"&gt;@bestpractice___&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Your switch forgets a case. Exhaustiveness checking catches it.</title>
      <dc:creator>Parsa J</dc:creator>
      <pubDate>Fri, 27 Nov 2026 11:43:55 +0000</pubDate>
      <link>https://dev.to/parsajiravand/your-switch-forgets-a-case-exhaustiveness-checking-catches-it-56d5</link>
      <guid>https://dev.to/parsajiravand/your-switch-forgets-a-case-exhaustiveness-checking-catches-it-56d5</guid>
      <description>&lt;p&gt;Someone on your team adds &lt;code&gt;"refunded"&lt;/code&gt; to the &lt;code&gt;OrderStatus&lt;/code&gt; union. Three files use that type. Two of them get updated. The third — the one that renders the status badge on the order page — doesn't.&lt;/p&gt;

&lt;p&gt;It compiles. It ships. A refunded order sits on a customer's dashboard labeled "Processing" for two weeks, because the &lt;code&gt;switch&lt;/code&gt; that renders the badge has no idea &lt;code&gt;"refunded"&lt;/code&gt; exists, falls through to nothing, and renders an empty string.&lt;/p&gt;

&lt;p&gt;Nobody wrote a bug. Someone just didn't update every place that needed updating — which is the normal, forgivable thing that happens on every team, every week. The question is whether your type system notices.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix everyone reaches for first
&lt;/h2&gt;

&lt;p&gt;The instinctive move is a &lt;code&gt;default&lt;/code&gt; case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"pending"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"paid"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"shipped"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"delivered"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;badgeText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"pending"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Processing"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"paid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Payment received"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"shipped"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"On the way"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"delivered"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Delivered"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Processing"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// "it has to be something"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This feels safe. It isn't. Add &lt;code&gt;"refunded"&lt;/code&gt; to the union tomorrow, and this function keeps compiling, keeps running, and keeps quietly mislabeling refunded orders as "Processing" — the exact bug from the top of this post, except now it's load-bearing, because the &lt;code&gt;default&lt;/code&gt; case was &lt;em&gt;designed&lt;/em&gt; to swallow anything unexpected.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;console.warn&lt;/code&gt; in the default branch helps you find out &lt;em&gt;after&lt;/em&gt; it's in production, if anyone's watching logs. What you actually want is to find out before you hit "merge."&lt;/p&gt;

&lt;h2&gt;
  
  
  What you want instead: make the gap a compile error
&lt;/h2&gt;

&lt;p&gt;TypeScript already tracks, branch by branch, which members of a union a &lt;code&gt;switch&lt;/code&gt; has ruled out. Inside the &lt;code&gt;default&lt;/code&gt; case of a fully-handled switch, the type of &lt;code&gt;status&lt;/code&gt; isn't &lt;code&gt;OrderStatus&lt;/code&gt; anymore — it's narrowed down to whatever's left over. If every case was handled, what's left over is nothing at all: the &lt;code&gt;never&lt;/code&gt; type, TypeScript's name for "a value that cannot exist."&lt;/p&gt;

&lt;p&gt;That's the lever. Write a function that only accepts &lt;code&gt;never&lt;/code&gt;, and call it from the &lt;code&gt;default&lt;/code&gt; branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;assertUnreachable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;never&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;never&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`Unhandled case: ${JSON.stringify(x)}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;badgeText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"pending"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Processing"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"paid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Payment received"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"shipped"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"On the way"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s2"&gt;"delivered"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Delivered"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;assertUnreachable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Right now this compiles cleanly, because TypeScript has proven &lt;code&gt;status&lt;/code&gt; really is &lt;code&gt;never&lt;/code&gt; by the time it reaches &lt;code&gt;default&lt;/code&gt; — every other member of the union got its own case above.&lt;/p&gt;

&lt;p&gt;Now add &lt;code&gt;"refunded"&lt;/code&gt; to &lt;code&gt;OrderStatus&lt;/code&gt; and change nothing else:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"pending"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"paid"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"shipped"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"delivered"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s2"&gt;"refunded"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;badgeText&lt;/code&gt; function no longer compiles. TypeScript narrows &lt;code&gt;status&lt;/code&gt; in the &lt;code&gt;default&lt;/code&gt; branch to &lt;code&gt;"refunded"&lt;/code&gt; — one real, leftover case — and &lt;code&gt;"refunded"&lt;/code&gt; is not assignable to the parameter type &lt;code&gt;never&lt;/code&gt;. The error points at the exact line, in the exact file, that forgot to handle the new status. Not a runtime log line three weeks from now. A red squiggle before you push.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why &lt;code&gt;never&lt;/code&gt; is the right tool for this
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;never&lt;/code&gt; is TypeScript's type for "this code path cannot be reached" — functions that always throw, infinite loops, and branches a union's structure has already ruled out. Assigning a value to a &lt;code&gt;never&lt;/code&gt;-typed parameter is only legal if TypeScript can prove no value is left to assign. That's precisely what "the switch handled every case" means. You're not writing a special exhaustiveness feature; you're pointing a general-purpose type at a question it's already equipped to answer.&lt;/p&gt;

&lt;p&gt;It composes the same way with &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else if&lt;/code&gt; chains — narrowing works the same way there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;badgeTextIf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OrderStatus&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"pending"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Processing"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"paid"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Payment received"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"shipped"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"On the way"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"delivered"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Delivered"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;assertUnreachable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same guarantee, same failure mode when you add &lt;code&gt;"refunded"&lt;/code&gt; without touching this function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where it won't save you
&lt;/h2&gt;

&lt;p&gt;This catches &lt;em&gt;missing&lt;/em&gt; cases, not &lt;em&gt;wrong&lt;/em&gt; ones. If you handle &lt;code&gt;"refunded"&lt;/code&gt; by copy-pasting the &lt;code&gt;"paid"&lt;/code&gt; branch, the compiler has nothing to say — that's a logic bug, not an exhaustiveness bug, and no type system catches those for free.&lt;/p&gt;

&lt;p&gt;It also needs an honest &lt;code&gt;default&lt;/code&gt; (or a final &lt;code&gt;else&lt;/code&gt;) with nothing after it that would make the branch unreachable another way — wrap the call in a function typed to return the right thing, as above, rather than leaving it as a bare statement the compiler could route around. And it only protects the functions you've actually wired this way; it doesn't retroactively audit a codebase. The value compounds the way tests do — add it to every switch over a union that models real states (order status, payment status, permission level, wizard step), and the moment someone extends that union, the compiler goes and finds every place that forgot, for you, before a reviewer has to.&lt;/p&gt;

&lt;h2&gt;
  
  
  🎮 Try it yourself
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://bestpractic.org/blog/typescript-exhaustiveness-checking-never/playground" rel="noopener noreferrer"&gt;▶️ Open the interactive playground →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Runs right in your browser — poke at it and watch the concept react live.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The payoff
&lt;/h2&gt;

&lt;p&gt;Go check whatever &lt;code&gt;OrderStatus&lt;/code&gt;-shaped type your codebase already has — a job status, a subscription tier, a form step — and find the switch statements over it. If none of them end in a call to a &lt;code&gt;never&lt;/code&gt;-typed function, the next person to extend that union is one keystroke away from shipping the same quiet gap "refunded" fell into above. One function, reused everywhere: that's the whole fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Test yourself
&lt;/h2&gt;

&lt;p&gt;Think it clicked? &lt;strong&gt;&lt;a href="https://bestpractic.org/blog/typescript-exhaustiveness-checking-never/quiz" rel="noopener noreferrer"&gt;Take the 8-question quiz →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Has a missing &lt;code&gt;switch&lt;/code&gt; case ever gotten past your reviewers and into production? What did it look like when you finally found it?&lt;/p&gt;

&lt;h2&gt;
  
  
  📚 Read next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/my-reducers-switch-stopped-being-exhaustive-as-const-fixed-it-4aa4"&gt;My Reducer's Switch Stopped Being Exhaustive. &lt;code&gt;as const&lt;/code&gt; Fixed It.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/your-typescript-config-objects-are-losing-type-information-satisfies-fixes-it-3hdh"&gt;Your TypeScript config objects are losing type information. &lt;code&gt;satisfies&lt;/code&gt; fixes it.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/parsajiravand/stop-formatting-currency-with-regex-intlnumberformat-does-it-220b"&gt;Stop Formatting Currency With Regex. &lt;code&gt;Intl.NumberFormat&lt;/code&gt; Does It.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;🚀 &lt;strong&gt;Want more like this?&lt;/strong&gt; Every guide, playground, and quiz lives on &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;bestpractic.org&lt;/a&gt;&lt;/strong&gt; — open it and &lt;strong&gt;&lt;a href="https://bestpractic.org/" rel="noopener noreferrer"&gt;sign up free&lt;/a&gt;&lt;/strong&gt; so the next one finds you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading! Let's stay connected:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⭐ &lt;strong&gt;GitHub&lt;/strong&gt; — follow me and star the projects: &lt;a href="https://github.com/parsajiravand" rel="noopener noreferrer"&gt;github.com/parsajiravand&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💬 &lt;strong&gt;Discord&lt;/strong&gt; — join the frontend best-practices community: &lt;a href="https://discord.gg/d9KRhuAwQ" rel="noopener noreferrer"&gt;discord.gg/d9KRhuAwQ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📸 &lt;strong&gt;Instagram&lt;/strong&gt; — frontend best practices, daily: &lt;a href="https://www.instagram.com/bestpractice___/" rel="noopener noreferrer"&gt;@bestpractice___&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
