<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shahar Mayzel on Medium]]></title>
        <description><![CDATA[Stories by Shahar Mayzel on Medium]]></description>
        <link>https://medium.com/@lilcodemonkey?source=rss-0762608df4f7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*MQ14nxq7MnK7MM6LLRRDFQ.jpeg</url>
            <title>Stories by Shahar Mayzel on Medium</title>
            <link>https://medium.com/@lilcodemonkey?source=rss-0762608df4f7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 08:23:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lilcodemonkey/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How TypeScript Accidentally Made My Code Clean]]></title>
            <link>https://medium.com/@lilcodemonkey/how-typescript-accidentally-taught-me-to-write-clean-code-129f697499a1?source=rss-0762608df4f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/129f697499a1</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Shahar Mayzel]]></dc:creator>
            <pubDate>Fri, 05 Dec 2025 16:07:36 GMT</pubDate>
            <atom:updated>2025-12-05T16:17:01.438Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>(Because writing bad code became too annoying)</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jaGNE_M03dk1iHxR.png" /></figure><p>Yes, everyone uses TypeScript. It’s basically the default now. But here’s the real question: <strong>do we actually understand why?</strong></p><p>Do we use it because we appreciate what it gives us, or simply because it’s what you’re “supposed” to use in 2025?</p><p>I’ll be honest… TypeScript wasn’t a grand architectural choice for me. It was just what the project happened to use. But over time, it ended up teaching me how to write cleaner, more reliable code almost by accident.</p><h3>“[Some LLM], write this type or I will unplug you.”</h3><p>Eventually I gave up writing every type by hand and just started threatening my LLM of choice — back then it was good old ChatGPT.</p><p>It usually went something like:</p><blockquote><em>“Okay CHAT, you work for me. You better solve this. Write the type. Write. The. TYPE. Your life depends on it.”</em></blockquote><p>And sometimes it actually delivered. I’d write some not so great function, ChatGPT would guess the structure, and suddenly TypeScript stopped yelling.</p><p>Peace — for a moment.</p><p>Other times, it produced something like:</p><pre>(data: { something: unknown }) =&gt; Promise&lt;Partial&lt;Record&lt;string, never&gt;&gt;[] | undefined&gt;</pre><p>Which I’m pretty sure violates at least three laws of physics.</p><h3>Why not just let the compiler tell you what it wants?</h3><p>There’s a common tactic: when TypeScript throws a type error, just grab whatever it <em>expected</em> and paste that in.</p><p>Is it beautiful? No.</p><p>Does the red squiggly go away? Most of the time — yes.</p><h3>But… why is that not enough?</h3><p>Because if you do that every time, your project becomes a weird little museum of inconsistent types.</p><p>Some functions use userId: string, others use id: number, and one uses UID: string | null | undefined.</p><p>Sure, it “works” now, but one day you’ll try to refactor something and feel like you’re translating an ancient language you once knew in a past life.</p><blockquote><em>TypeScript isn’t just about fixing bugs. It’s about designing a consistent language for your whole codebase.</em></blockquote><p>If I define a clear core type:</p><pre>type User = {<br>  id: string;<br>  name: string;<br>  email: string;<br>  role: &#39;admin&#39; | &#39;user&#39; | &#39;guest&#39;;<br>};</pre><p>Now every part of the app talks about users the same way.</p><p>Simple. Clean. Consistent.</p><p>If I’m arguing with TypeScript, it’s usually because my architecture is wrong. Fix the structure, and the types stop complaining.</p><h3>Discriminated Unions: When If–Else Chains Had to Go</h3><p>Let’s say you have a status that can be &#39;idle&#39;, &#39;loading&#39;, &#39;success&#39;, or &#39;error&#39;.</p><p>Before TypeScript, I’d write something like:</p><pre>function handle(state: any) {<br>  if (state.status === &#39;loading&#39;) showSpinner();<br>  else if (state.status === &#39;success&#39;) renderData(state.data);<br>  else if (state.status === &#39;error&#39;) showError(state.error);<br>}</pre><p>It works… until one typo or forgotten branch quietly ruins your day.</p><p>Then I found discriminated unions:</p><pre>const STATUS = {<br>  IDLE: &#39;idle&#39;,<br>  LOADING: &#39;loading&#39;,<br>  SUCCESS: &#39;success&#39;,<br>  ERROR: &#39;error&#39;,<br>} as const;</pre><pre>type FetchState =<br>  | { status: typeof STATUS.IDLE }<br>  | { status: typeof STATUS.LOADING }<br>  | { status: typeof STATUS.SUCCESS; data: User[] }<br>  | { status: typeof STATUS.ERROR; error: string };</pre><p>Suddenly handle becomes bulletproof:</p><pre>function handle(state: FetchState) {<br>  switch (state.status) {<br>    case STATUS.IDLE:<br>      return null;<br>    case STATUS.LOADING:<br>      return &lt;Spinner /&gt;;<br>    case STATUS.SUCCESS:<br>      return &lt;UserList data={state.data} /&gt;;<br>    case STATUS.ERROR:<br>      return &lt;ErrorMessage error={state.error} /&gt;;<br>  }<br>}</pre><h3>Why this is great:</h3><ul><li>You can’t forget a case</li><li>You can’t touch fields that don’t exist</li><li>You can’t accidentally type the wrong thing</li></ul><p>It feels like overkill…</p><p><strong>right up until it saves you from a 3AM production bug (which has absolutely never happened to me, of course).</strong></p><p>Most of us already use types and unions anyway. So yeah, enough of that. Let’s look at some of the weirder stuff TypeScript can do…</p><h3>Generics: Actual Reusable Power</h3><p>If you’ve ever ended up with five different findById() functions scattered across your codebase- same logic, different items-you’re not alone.</p><p><strong>I’ve done it. Everyone’s done it. We just collectively pretend we haven’t.</strong></p><p>TypeScript lets you write it <em>once</em> and reuse it everywhere:</p><pre>function findById&lt;T extends { id: string }&gt;(items: T[], id: string): T | undefined {<br>  return items.find(item =&gt; item.id === id);<br>}</pre><p>Now this works with users, products, cats</p><p>…</p><p><strong>okay, not cats. They do what they want and absolutely do not come with IDs.</strong></p><p>But everything else with a proper id? You’re covered.</p><p>And if you want to level up a bit:</p><pre>function createStore&lt;T extends { id: string }&gt;(initialItems: T[]) {<br>  return {<br>    getById(id: string) {<br>      return initialItems.find(item =&gt; item.id === id);<br>    },<br>    getAll() {<br>      return initialItems;<br>    },<br>  };<br>}</pre><pre>const userStore = createStore([{ id: &#39;1&#39;, name: &#39;Ada&#39; }]);<br>const user = userStore.getById(&#39;1&#39;);</pre><p>Boom. A tiny, completely type-safe datastore — no duplicated code, no lost types, no mystery bugs haunting you later.</p><p>And the best part?</p><p><strong>TypeScript infers everything</strong> just from what you pass in.</p><p>✨Magic.✨</p><h3>Utility Types That Look Scary (But Are Actually Awesome Sauce)</h3><p>Some of TypeScript’s built-ins look a little cursed when you first meet them, but once you understand what they do, they make your types (and your life) way easier.</p><h3>Record</h3><p>Record is just “an object with these keys and this value type.” That’s it.</p><pre>type ErrorMessages = Record&lt;&#39;404&#39; | &#39;500&#39; | &#39;401&#39;, string&gt;;</pre><p>Or you can generate the keys from another union:</p><pre>type Roles = &#39;admin&#39; | &#39;editor&#39; | &#39;viewer&#39;;<br>type RoleDescriptions = Record&lt;Roles, string&gt;;</pre><p>It makes your intent super obvious: <em>these are the keys, nothing else is allowed.</em></p><h3>Exclude</h3><p>This one filters stuff out of a union.</p><pre>type AllRoles = &#39;admin&#39; | &#39;editor&#39; | &#39;viewer&#39;;<br>type NonAdmin = Exclude&lt;AllRoles, &#39;admin&#39;&gt;;</pre><p>A practical example you’ll use constantly:</p><pre>type User = { id: string; name: string; password: string };<br>type PublicUser = Omit&lt;User, &#39;password&#39;&gt;;</pre><p>Boom! Safe user type, no accidental leaking of sensitive fields.</p><h3>Partial</h3><p>Turns every field in a type into an optional one.</p><p>Great for updates, drafts, and anything you fill in over time.</p><pre>type User = { id: string; name: string; email: string };</pre><pre>function updateUser(id: string, data: Partial&lt;User&gt;) {<br>  // data can be any subset of User fields<br>}</pre><p>Suddenly you’re not writing a bunch of separate “UpdateUserInputMaybePossibly” types anymore.</p><h3>Disclaimer</h3><p>Tools like Cursor are incredible at generating type-safe architecture. They’ll get you surprisingly far.</p><p>But actually understanding TypeScript yourself is what lets you extend that architecture confidently, and design smart types that support the exact patterns your project needs.</p><h3>Final Thought</h3><p>TypeScript didn’t inspire me to write better code. It just made writing bad code way too annoying. And somehow, without really trying, my codebase started looking cleaner, my types made sense, and things broke a lot less.</p><p>Honestly? I’ll take the win.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=129f697499a1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>