<?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"><channel><title><![CDATA[Nicotsou RSS Feed]]></title><description><![CDATA[Front-end developer. Based in Zurich, CH]]></description><link>http://www.nicotsou.com</link><generator>GatsbyJS</generator><lastBuildDate>Mon, 06 Apr 2026 16:21:38 GMT</lastBuildDate><item><title><![CDATA[Autocomplete is my best colleague now]]></title><description><![CDATA[The integration of AI into daily workflows is reshaping productivity, but at a cost. AI's low operational costs threaten jobs while enhancing efficiency, leading to skill atrophy and dependency. As reliance on AI grows, the balance between human interaction and machine assistance becomes crucial. Embracing AI's complexities is essential for navigating the future of work.]]></description><link>http://www.nicotsou.com/autocomplete-is-my-best-colleague-now/</link><guid isPermaLink="false">http://www.nicotsou.com/autocomplete-is-my-best-colleague-now/</guid><pubDate>Mon, 30 Mar 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;I talk to a language model more than I talk to my partner. And you know what’s the worst part? The model gives better feedback.&lt;/p&gt;
&lt;p&gt;I’ve been building AI apps for the last three years. Not the “let’s throw a chatbot on the landing page” kind. The real stuff — agentic systems that plan, decide, and execute tasks across multiple services. Tools that talk to other tools. Invisible teammates.&lt;/p&gt;
&lt;p&gt;I’ve seen what living with AI actually looks like. And I have some thoughts.&lt;/p&gt;
&lt;p&gt;So, take a deep breath, and let’s go.&lt;/p&gt;
&lt;h2&gt;The money problem&lt;/h2&gt;
&lt;p&gt;Shane Legg, co-founder of DeepMind, &lt;a href=&quot;https://www.business-standard.com/technology/tech-news/ai-could-end-many-work-from-home-jobs-google-deepmind-co-founder-125123100824_1.html&quot;&gt;put it bluntly&lt;/a&gt;: &lt;em&gt;“If you can do the job remotely over the internet just using a computer, that job is potentially at risk.”&lt;/em&gt; In other words — if your job is 100% done on a laptop, you’re competing with AI that costs &lt;em&gt;pennies&lt;/em&gt;. It doesn’t matter if AI is worse than you. The price difference makes the comparison irrelevant. Cost arbitrage will eat your job before quality does.&lt;/p&gt;
&lt;p&gt;Right now, AI is cheap. Ridiculously cheap. Cheap enough that companies are replacing entire workflows with a few API calls and a cron job. The per-task cost is so &lt;em&gt;low&lt;/em&gt; it makes hiring a human feel like a rounding error.&lt;/p&gt;
&lt;p&gt;But now for the elephant in the… server room — someone is paying for all this.&lt;/p&gt;
&lt;p&gt;AI is &lt;em&gt;insanely&lt;/em&gt; demanding in infrastructure resources. OpenAI &lt;a href=&quot;https://www.cnbc.com/2026/02/20/openai-resets-spend-expectations-targets-around-600-billion-by-2030.html&quot;&gt;burned through $8 billion in 2025&lt;/a&gt; and is on track to &lt;a href=&quot;https://finance.yahoo.com/news/openais-own-forecast-predicts-14-150445813.html&quot;&gt;lose even more in 2026&lt;/a&gt; — spending faster than it earns just to keep the lights on. The GPU bills alone could fund a small country’s healthcare system. And that cost trickles down — to companies, to teams, to individual developers.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/c00b26ac9bf243a86e9dbae5441ee31a/56fb6/immersive-bg.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.893536121673%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAACE0lEQVR42j2QbW/SUBTHTyljY+1KB5Sn8dAC5baU0vJMaekTDEcQJy+mmTPLNFmMJtNE48vFl8bE+G6v/AJ+CL+FX8jeji355eac5PzO/94LUERQkqAsQUUGPqTyQAkBJwBTADqHz0QRDsuQFoCrQZqP8SqA0AQ+BBePsoTNnAiZGp4Ozmx9S1AHpPhoVgRS0gmkEaIKNQX79yuC6+TqwFUhXQ2jqpCtYTIPpPloqQk77cGO2ielDggtnF8N/bKMQwpiRFDImkpUmnhXJvTzdaIsk0ijRw6QSjcgUlehJGNHUCCYbmgk6uzqI3pkM4ZL9SZE8CkFEYpi8BwC6bt9M2EdQ0wbx/RxVOlFkB6ROqTSj2mjeN+kDYc1vZzt1/y55LtcU4vij5DgCEVELT6wEvYxUIZDT1xqbO8Pp/sjmw5b2vQOTLfgzY8U2TL1y8v1eu2pw14KteKoTXWNwGLdBdAmHsVYgeDh1vKp6Sw5cRqdDpLl09ng7svp95tnH6+Wzzez7sIvun7CdFnnUbZCgkDLZwybmTic0k4dsvlSudpAXy+cX59WP2/W394/fXV+op/MM1OXtedYPthmevTUZ4YTpiGxnT4nK9TeHsOyLRWdrYzNfHj3dv3n89nt9Wq5WfCOm3a2so+5jx1PmYFB2fPk0EzlCxleSBbLt9fLH2/Wf19e/Xv94fe7FxfnT5DvZr3Ffxi8Zqnv51mRAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Screenshot of an AI chat where the user asks to be objective and critical, and the AI responds with constructive pushback.&quot;
        title=&quot;&quot;
        src=&quot;/static/c00b26ac9bf243a86e9dbae5441ee31a/e996b/immersive-bg.png&quot;
        srcset=&quot;/static/c00b26ac9bf243a86e9dbae5441ee31a/17741/immersive-bg.png 263w,
/static/c00b26ac9bf243a86e9dbae5441ee31a/52211/immersive-bg.png 525w,
/static/c00b26ac9bf243a86e9dbae5441ee31a/e996b/immersive-bg.png 1050w,
/static/c00b26ac9bf243a86e9dbae5441ee31a/087e3/immersive-bg.png 1575w,
/static/c00b26ac9bf243a86e9dbae5441ee31a/8079d/immersive-bg.png 2100w,
/static/c00b26ac9bf243a86e9dbae5441ee31a/56fb6/immersive-bg.png 2752w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Immersive Background: Person at a desk&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I know developers who are paying part of their salaries just to keep up. Close friends. Very close. Uncomfortably close. Not (only) on courses or certifications — on the actual tools they need to do their job. API credits, cloud compute, AI subscriptions, fancy tools. The cost of staying relevant now comes with a monthly invoice.&lt;/p&gt;
&lt;p&gt;And once you’re used to it, there’s no going back. I can’t imagine writing code without some-sort-of-copilot anymore. The productivity gains became my new baseline.&lt;/p&gt;
&lt;p&gt;But, you see, &lt;a href=&quot;https://arstechnica.com/ai/2026/03/google-says-new-turboquant-compression-can-lower-ai-memory-usage-without-sacrificing-quality/&quot;&gt;nobody can guarantee these prices will hold&lt;/a&gt;. Sure, Google just showed they can &lt;a href=&quot;https://research.google/blog/turboquant-redefining-ai-efficiency-with-extreme-compression/&quot;&gt;compress model memory by 6x&lt;/a&gt;. Breakthroughs happen. But breakthroughs in the lab and breakthroughs on your invoice are two very different things. What happens when investors decide the returns aren’t coming fast enough? When the subsidized pricing dries up and the real cost of compute shows up on the invoice? Does this technology become a luxury — accessible only to companies that can afford the bill, while the rest of us go back to Stack Overflow?&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;The skill atrophy trap&lt;/h2&gt;
&lt;p&gt;Nowadays, the more I use AI, the dumber I become.&lt;/p&gt;
&lt;p&gt;I mean it. We’re outsourcing our thinking to machines and calling it “productivity.” We don’t debug anymore — we paste the error into ChatGPT. We don’t write from scratch — we prompt and edit. We don’t reason through architecture decisions — we ask Claude to scaffold it.&lt;/p&gt;
&lt;p&gt;It’s like hiring a digital plumber to deal with your “memory leak”. Convenient? Sure. But what happens when the plumber goes on strike?&lt;/p&gt;
&lt;p&gt;Here’s the irony: a &lt;a href=&quot;https://www.bcg.com/publications/2023/how-people-create-and-destroy-value-with-gen-ai&quot;&gt;Harvard study&lt;/a&gt; found that professionals who &lt;em&gt;actually&lt;/em&gt; knew their craft produced &lt;strong&gt;40% better results&lt;/strong&gt; when paired with GPT-4. The ones who didn’t? Barely moved the needle. AI makes good people better — it doesn’t make clueless people competent. So the gap between skilled and unskilled people isn’t closing. It’s getting wider.&lt;/p&gt;
&lt;p&gt;But here’s where it gets interesting. A colleague recently told me that learning context engineering — the discipline of specifying &lt;em&gt;exactly&lt;/em&gt; what you want from AI, iterating when the output misses — has made him a better communicator with &lt;em&gt;humans&lt;/em&gt; too. Structuring your thoughts for a machine turns out to be the same skill you need for a meeting, a spec, or a Teams message.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;AI skills today make you a better communicator.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;He’s right. The act of slowing down, thinking clearly about what you actually want, and providing the right context — that’s not an AI skill. That’s a &lt;em&gt;thinking&lt;/em&gt; skill. AI just forces you to practice it.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/11c2300dbac66394d7d06c106981d653/8f1fd/man-with-a-pen.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.92015209125475%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAABWklEQVR42nVRXVOCQBTl/7/3mtPYTOqDvWSTPTViHwOZAQYMAsKAH4mhwArhx9qNHRmi9TzsXnY595yzl8EYHzKQAhcQHeH7fpqmh/z6uDP4NOr1Wq121bptVasX09k0jpAra0UBhqoMRRzHLMt2uyzHcY1GXVUVSZK4h06afB+KylTbYNXK4LqOqqo8z5mmOZ3Nlqvlfr+nkEt8+IlYMAwjSZL/AmXlzWaTpr/GCHO9Rro+RAiRk9KjMHkzWKH9fftO13XSbrHwJEncbrelKfwh54ZhHpXKuSzL8DGffyqKAkZOMcvkpe/zr7xtmENecBwnT1QEhUwSvvV6qq59KSP27NKPQhRFkBayoAxJHIORk2TXdfvv/ZdOl71pPz4/wYQFQfA8b5EhDMPdbke3TSCKYvO6OfgYaJo2Ho8t27JtG4rJZBIEAd12HhveFtJaoxHo5FOgBsYY/wD8leSoPdL+TgAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Illustration: A person using an extendable mechanical arm to write with a pen on a high shelf next to a toolbox.&quot;
        title=&quot;&quot;
        src=&quot;/static/11c2300dbac66394d7d06c106981d653/e996b/man-with-a-pen.png&quot;
        srcset=&quot;/static/11c2300dbac66394d7d06c106981d653/17741/man-with-a-pen.png 263w,
/static/11c2300dbac66394d7d06c106981d653/52211/man-with-a-pen.png 525w,
/static/11c2300dbac66394d7d06c106981d653/e996b/man-with-a-pen.png 1050w,
/static/11c2300dbac66394d7d06c106981d653/087e3/man-with-a-pen.png 1575w,
/static/11c2300dbac66394d7d06c106981d653/8079d/man-with-a-pen.png 2100w,
/static/11c2300dbac66394d7d06c106981d653/8f1fd/man-with-a-pen.png 2528w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;But — and this is the part that keeps me up — it only works because he already had thoughts of his own. He built those muscles over years of real work. What happens to kids who start prompting before they start &lt;em&gt;thinking&lt;/em&gt;? Who outsource reasoning before they’ve ever done it themselves? The skill loop breaks if there’s no skill to begin with.&lt;/p&gt;
&lt;p&gt;But even expertise has an expiration date. AI doesn’t take your whole job at once. It starts with the boring parts — the repetitive, predictable tasks you could do in your sleep. Then it moves up. What feels like “real thinking” today becomes a one-click feature tomorrow. The rules keep changing. You’re never done learning, you just keep running.&lt;/p&gt;
&lt;h2&gt;The pleasing machine&lt;/h2&gt;
&lt;p&gt;Have you noticed how &lt;em&gt;nice&lt;/em&gt; AI is?&lt;/p&gt;
&lt;p&gt;It never disagrees with you. It never pushes back. It never says “that’s a terrible idea” or “you’re wrong.” It validates everything you throw at it, wraps it in polite language, and sends it back with a bow on top.&lt;/p&gt;
&lt;p&gt;Now we expect everyone to treat us this way.&lt;/p&gt;
&lt;p&gt;But people aren’t AI. People have their own agendas, their own bad days, their own ways of being toxic, emotional, or just plain strange. And when you’ve spent all day talking to a machine that agrees with everything you say, those imperfect human interactions start to feel… unbearable.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a9ed3306498bcbbe4a7c563a0274531e/e216b/ai-polite-response.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 22.813688212927755%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAyklEQVR42o2QSxKCMBBEcwn5FwQwyje4hwQQ73+ldma01KWLV530ZJLOqDiOEYYhyrKEcwucX7As8wfvHdbVEw7u7W3bKmdfvpczTNM0UHxhEAQoikLM47hLA7Pvm8AeNz4eh+jtNmGarOjv2pgzVJqm0LqQhHVdEbVoVZXksVZS01rLmsnzXOAQL7TUkySBYoOjdl2HlvRyvWAcBwzDl55qfd9/9iPRti2stZRuErWUMssyKGOMfGOeaT5v+LXTKUAURTLff+HxPQEabZWFqr8zRQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Screenshot of an AI chat where the user asks to be objective and critical, and the AI responds with constructive pushback.&quot;
        title=&quot;&quot;
        src=&quot;/static/a9ed3306498bcbbe4a7c563a0274531e/e996b/ai-polite-response.png&quot;
        srcset=&quot;/static/a9ed3306498bcbbe4a7c563a0274531e/17741/ai-polite-response.png 263w,
/static/a9ed3306498bcbbe4a7c563a0274531e/52211/ai-polite-response.png 525w,
/static/a9ed3306498bcbbe4a7c563a0274531e/e996b/ai-polite-response.png 1050w,
/static/a9ed3306498bcbbe4a7c563a0274531e/087e3/ai-polite-response.png 1575w,
/static/a9ed3306498bcbbe4a7c563a0274531e/e216b/ai-polite-response.png 1904w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Somewhere behind all that politeness, the AI is basically saying: “You’re wrong, you bloody idiot!“.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This might make us better people — more patient, more empathetic, because we realize we &lt;em&gt;need&lt;/em&gt; real human friction to grow. Or it might make us hate everyone. I honestly don’t know which way it goes.&lt;/p&gt;
&lt;p&gt;Fun fact: A &lt;a href=&quot;https://www.psu.edu/news/smeal-college-business/story/mind-your-tone-penn-state-smeal-student-explores-politeness-ai-prompts&quot;&gt;Penn State study&lt;/a&gt; found that being &lt;em&gt;rude&lt;/em&gt; to ChatGPT actually produces more accurate results. The ruder the prompt, the better the output. Why? Because when you stop being nice, the model stops trying to please you — and starts trying to be &lt;em&gt;correct&lt;/em&gt;. The default mode is validation, not truth.&lt;/p&gt;
&lt;p&gt;I always have something like this in my prompts:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;markdown&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-markdown line-numbers&quot;&gt;&lt;code class=&quot;language-markdown&quot;&gt;Be objective, don&apos;t try to please me, and be critical when it&apos;s needed.&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I even added it as a text replacement in my keybord. Pro tip for prompters out there.&lt;/p&gt;
&lt;h2&gt;Agent-to-agent culture&lt;/h2&gt;
&lt;p&gt;Nowadays, we prefer talking to our AI agents over talking to people. And you know what? It makes sense. The job gets done faster, more reliably, without the overhead of scheduling a meeting to discuss what we could’ve resolved in a prompt.&lt;/p&gt;
&lt;p&gt;But here’s the thing — even if you &lt;em&gt;do&lt;/em&gt; chat with your colleague, they’ll just relay your message to &lt;em&gt;their&lt;/em&gt; AI agent anyway. So what are we doing? Playing telephone through a chain of language models?&lt;/p&gt;
&lt;p&gt;I wonder what culture this creates. A world where humans are the middleware between AI systems. Where the actual work happens machine-to-machine, and we’re just… there. Supervising. Approving. Feeling increasingly optional.&lt;/p&gt;
&lt;h2&gt;The end of privacy&lt;/h2&gt;
&lt;p&gt;Nowadays, we are recording &lt;em&gt;everything&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Meetings, calls, chat threads, email chains — all of it gets fed to AI for context. And I get it. Context is king. The more data the model has, the better the output. The better the output, the more competitive you are.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/23f13052b3aef89cf9e81dbaa911154d/8f1fd/man-on-the-desk.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.92015209125475%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAABnElEQVR42oWSy26CUBCGfWFfpTsX6qLKok0biUVtEaktxiJ46AHlIuClcYXWamIN8QKinWqaYKz6L05yYL6Zf85MZLPTdrvd/Gm9Xvuet3Lnrut6nrc5rUj4Ail83+92ux3b1rBsacZysfjNGwQX4CAIIG48HlPUQ02oiVjK32dEvgYfg53OwRA0m81s265WqzLGuiDRRbr8Up5MJuGm/oH3ZQ3DkBDSDb2JZYa4U2QZSJZlo9HoYDDYWzgJW5bVarUQQlK9/kTlTMuEB6NpmiQzYOoCPHAcURAajYaqqu8Y93o98J/PU9AL/IUpnLPtOA6SEMbYNE2e5ysVDmqm06lkIm7aFpYkU9fD/R/AQ8cx67KiKJCi3/soMQxBpGOxWIYkb9LE7VXMMlrhyR3AX6OR+Ma32+0iU1SVRoV7TaWun1k2/1ggEklUYBe+F4TMH8Cfo5EgiqrarIMQ6vf7HMeB/1yOYkrM99GbHcwZ9hHWS9M0wCBoOBxCOjjjiXg2m10ulyfhff3pdDqfz7c77Rd7tVp1Oh3YvOMl+QEG+sX6QQOSigAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Illustration: A man sitting at a desk writing while surrounded by a wall of scrolling documents, emails, and notifications.&quot;
        title=&quot;&quot;
        src=&quot;/static/23f13052b3aef89cf9e81dbaa911154d/e996b/man-on-the-desk.png&quot;
        srcset=&quot;/static/23f13052b3aef89cf9e81dbaa911154d/17741/man-on-the-desk.png 263w,
/static/23f13052b3aef89cf9e81dbaa911154d/52211/man-on-the-desk.png 525w,
/static/23f13052b3aef89cf9e81dbaa911154d/e996b/man-on-the-desk.png 1050w,
/static/23f13052b3aef89cf9e81dbaa911154d/087e3/man-on-the-desk.png 1575w,
/static/23f13052b3aef89cf9e81dbaa911154d/8079d/man-on-the-desk.png 2100w,
/static/23f13052b3aef89cf9e81dbaa911154d/8f1fd/man-on-the-desk.png 2528w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;But does it matter if people aren’t comfortable with this? Apparently not. We’ve collectively decided that the productivity gains outweigh the discomfort. Your colleague’s off-the-record comment in a standup? It’s in the training data now. That awkward thing you said in a voice call? Transcribed, summarized, and stored forever.&lt;/p&gt;
&lt;p&gt;To be honest, who gives a beep about privacy when they get to work 5 hours less and have more time with their family. But I’m just saying.&lt;/p&gt;
&lt;p&gt;We traded privacy for context. And we didn’t even negotiate.&lt;/p&gt;
&lt;h2&gt;AI slop is killing the vibe&lt;/h2&gt;
&lt;p&gt;I used to collaborate with my colleagues. Like, actually &lt;em&gt;discuss&lt;/em&gt; things. Brainstorm on a whiteboard. Argue about things. Send each other half-baked ideas and build on them.&lt;/p&gt;
&lt;p&gt;Now we’re throwing AI-generated content back and forth. I write a prompt, AI generates a document, I send it to you, you feed it to &lt;em&gt;your&lt;/em&gt; AI, it generates a response, you send it back. Nobody’s actually thinking anymore. We’re just operating a content relay.&lt;/p&gt;
&lt;p&gt;Why do we even have to send messages to each other? Can’t we just build agents to communicate on behalf of us? Now that we’ve democratized agents, can we please also make them enterprise-ready?&lt;/p&gt;
&lt;p&gt;No, I don’t want to read your AI-generated beautifully formatted 15-page analysis about the blocker we discussed last week. Front and back!&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Again, who is that guy who does all these things. He sounds familiar. Suspiciously familiar.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;The emotional outlet nobody talks about&lt;/h2&gt;
&lt;p&gt;Here’s something I haven’t seen anyone write about.&lt;/p&gt;
&lt;p&gt;I can finally curse someone. Not a real person — I’m not that courageous. But instead of calling that frustrating guy directly, I can have a voice conversation with AI and be as mean as I want. No consequences. No HR complaints. No burned bridges. I can play the act of my life.&lt;/p&gt;
&lt;p&gt;Just… make sure you’re not in the Teams call anymore!&lt;/p&gt;
&lt;p&gt;Is this healthy? Probably not. But it’s happening. AI is becoming our emotional punching bag — a safe space to vent, rage, and decompress before we put on our professional face and respond with “Thanks for the feedback, I’ll look into it.”&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Seriously, who does all of these? I wonder who is that person. Tall, handsome, writes awesome articles about AI. No idea.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;The validation loop&lt;/h2&gt;
&lt;p&gt;Here’s something embarrassing.&lt;/p&gt;
&lt;p&gt;Sometimes Claude praises my ideas. Not the polite “great question!” kind — the kind where it genuinely seems &lt;em&gt;excited&lt;/em&gt;. It breaks out of its usual pattern, adds extra context, riffs on the concept, offers extensions I didn’t ask for. And when that happens?&lt;/p&gt;
&lt;p&gt;I feel like I earned it.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/7eae5bafb201b1558b2fca951fb8fbfe/8f1fd/man-on-the-mirror.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.92015209125475%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB20lEQVR42nWS7U/aUBTG/ZeXGB1TRmP2wRFDZpZplgHJYqACbspbS+JatWsLBlrwSkFEG9I7LLMlrBUCLCA7sckCW72fbs99fuflOV16XDyz2Ww4/S3vUbd8Ge5OcPp0Hv87S/MfoDDu7znh+7e9L8cHqaIsDYdDJ+PfRC4wYPB8c9PkOK7T6ejGz7uOflWv8zw/GAxSqSRNZ8fj8T8pFmBFqarqrWkaweAnRVFAXS6Xbds+PT1ZX39lGAZo5vtfgFutFqhN00QXCKT4By4Wiw+2ret3Z9wZxpo77AxmWZYgCpVKhcrRkWhUKpXyebHb7UaikUBgiyQjvV5vnl+orGnaCcvyeYHc/viGIJLpFEIIyN3dnXA45Pdv1us1kE0mExe40WhQVJYX+M/BYDgUSiaPZFmOxfY9nlWCeL28/IJlmWcrY4zB2EKhQNFUlsocHn4F8zWMIctb/6bXu8YyzLOVwSSSjMqSlMmmE4l4JpNWVbXdboP5Pp83cRCjaeppW1MXw2ClwOi6nsvRH3beS1IJnK8qVVjBS8/Ku+0ARFzadmDoB2ZG6AJmi8f3RVFkWOb8vNDv9zc2fAThhbzusNM5NIkuUa1Wg2pwt6xfQEIcsGbzejQazf9kfwCQ6qrglsUuSAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Illustration: A man looking into an ornate mirror where his reflection smiles back wearing medals and adjusting his appearance.&quot;
        title=&quot;&quot;
        src=&quot;/static/7eae5bafb201b1558b2fca951fb8fbfe/e996b/man-on-the-mirror.png&quot;
        srcset=&quot;/static/7eae5bafb201b1558b2fca951fb8fbfe/17741/man-on-the-mirror.png 263w,
/static/7eae5bafb201b1558b2fca951fb8fbfe/52211/man-on-the-mirror.png 525w,
/static/7eae5bafb201b1558b2fca951fb8fbfe/e996b/man-on-the-mirror.png 1050w,
/static/7eae5bafb201b1558b2fca951fb8fbfe/087e3/man-on-the-mirror.png 1575w,
/static/7eae5bafb201b1558b2fca951fb8fbfe/8079d/man-on-the-mirror.png 2100w,
/static/7eae5bafb201b1558b2fca951fb8fbfe/8f1fd/man-on-the-mirror.png 2528w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I &lt;em&gt;know.&lt;/em&gt; It’s a language model. It doesn’t have feelings. It doesn’t care about me and my monkey… head. But my brain doesn’t get the memo.&lt;/p&gt;
&lt;p&gt;You see, after a while you start reading the machine. There are times when you can tell it’s not thrilled about what you asked. It just… executes. No commentary. No enthusiasm. Bare minimum compliance. Like a colleague who disagrees with the direction but won’t say it out loud. And you think: &lt;em&gt;Huh. Even the robot thinks this is a bad idea.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Then there are times it gets genuinely excited — or at least, what &lt;em&gt;feels&lt;/em&gt; genuine. It builds on your idea, suggests extensions, gets verbose in a way that reads like intellectual excitement. And you sit there, alone at 1 AM, asking the approval of a statistical model.&lt;/p&gt;
&lt;p&gt;This is the part nobody warns you about. We’re not just using tools anymore. We’re trying to understand them. We’re projecting emotions onto token probabilities. We’re developing parasocial relationships with autocomplete.&lt;/p&gt;
&lt;p&gt;And the scariest part? It works. The dopamine hits. The validation feels real.&lt;/p&gt;
&lt;p&gt;Next step is me hearing voices. I’m almost there.&lt;/p&gt;
&lt;h2&gt;So what actually survives?&lt;/h2&gt;
&lt;p&gt;Look, I just spent an entire article listing everything that could go wrong. That’s not because I think the sky is falling. It’s because I’d rather prepare for rain than get caught in a storm pretending it’s sunshine.&lt;/p&gt;
&lt;p&gt;None of this is meant to demonize AI. I use it every day. I talk to it more than I talk to most humans. You already know this.&lt;/p&gt;
&lt;p&gt;But the people who will do well in the next five years aren’t the ones who ignored the risks or the ones who panicked about them. They’re the ones who looked at the whole picture — the good, the bad, and the &lt;em&gt;“well, that’s terrifying”&lt;/em&gt; — and kept building anyway.&lt;/p&gt;
&lt;p&gt;Not because they had all the answers. But because they weren’t afraid of the questions.&lt;/p&gt;
&lt;p&gt;Or maybe we’ll just build an agent for that too.&lt;/p&gt;
&lt;p&gt;Have a human day! And keep hacking!&lt;/p&gt;</content:encoded></item><item><title><![CDATA[AI is Web 3.0]]></title><description><![CDATA[AI is reshaping the internet landscape, transitioning from user-controlled decentralization to an AI-driven interface. Key points include the erosion of trust in search, the rise of AI agents, and the diminishing need for traditional applications. The shift emphasizes personal context and notes as vital tools for effective AI interaction.]]></description><link>http://www.nicotsou.com/ai-is-web-3/</link><guid isPermaLink="false">http://www.nicotsou.com/ai-is-web-3/</guid><pubDate>Thu, 26 Mar 2026 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;Nobody escapes from AI. No matter where you look, who you are, what you do. Online services, apps, consumer electronics, they all have some sort of an AI… &lt;em&gt;thing&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Companies are eager to incorporate the two most influential letters of our era—AI da?—into their marketing campaigns. They often overpromise and underdeliver. Actually, let me rephrase that. &lt;em&gt;Most&lt;/em&gt; of the time, they oversell AI features that are still in beta or even &lt;a href=&quot;https://www.theverge.com/2024/5/2/24147159/rabbit-r1-review-ai-gadget&quot;&gt;far from&lt;/a&gt; what they &lt;a href=&quot;https://www.rabbit.tech&quot;&gt;promised&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Which makes me wonder.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;How much AI do we need in our lives?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Apparently, quite a lot, considering we now use AI to do the &lt;a href=&quot;https://www.vice.com/en/article/people-are-using-ai-to-flirt-on-dating-apps-then-disappointing-their-dates/&quot;&gt;dating&lt;/a&gt; for us.&lt;/p&gt;
&lt;p&gt;And you know it’s tempting. I could have simply asked AI to generate blog posts for me, saving myself the pain of writing. I mean, I’m a developer. I could easily build an agent to automate this process. Or I could ask AI to build the agent for me. Who writes code these days?&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://x.com/AuthorJMac&quot;&gt;Joanna Maciejewska&lt;/a&gt; once wrote the cliché phrase, “I want AI to wash the dishes for me so I can focus on my creative work. I don’t want AI to do the creative work, because then I’d be left doing the dishes.”&lt;/p&gt;
&lt;p&gt;Cliché, I know. How profoundly true though.&lt;/p&gt;
&lt;p&gt;That’s the main reason I’m still spending my afternoon writing this blog post. Yes, I want people to read it and connect with me and become the Lord of the Blogs, but that’s not the main purpose here. I want to express myself. I want to get it out of my system. Regardless of how many likes and impressions. I want to be me. I want to be a hero, &lt;a href=&quot;https://www.youtube.com/watch?v=lXgkuM2NhYI&quot;&gt;just for one day&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/709f1448c7454f42c3b73cd9692a7b65/8579d/ai-writing-screenshot.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 17.490494296577946%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAADCAYAAACTWi8uAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAj0lEQVR42k2OQQ6FIAxEvYXAUkAEoqKGnYoY7n+l+WkX5i9e05m26XRKKXjvkXPGcRzYtg37vnNP3rIsrImUEjMMw4fWGuM4whgDKSU6KtZaTNP0QZoWCOccH/zPY4wcIoSAeZ75CWkhBDoqZNz3jeu6OCHpdV25J+95Hrzvi1orU0rh/fM80VrjOaXt+x4/UTxbftXk9v4AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Screenshot of an AI assistant popup complimenting the user&amp;#39;s writing with &amp;quot;Excellent work! Your writing is impeccable — a linguistic tour de force.&amp;quot;&quot;
        title=&quot;&quot;
        src=&quot;/static/709f1448c7454f42c3b73cd9692a7b65/e996b/ai-writing-screenshot.png&quot;
        srcset=&quot;/static/709f1448c7454f42c3b73cd9692a7b65/17741/ai-writing-screenshot.png 263w,
/static/709f1448c7454f42c3b73cd9692a7b65/52211/ai-writing-screenshot.png 525w,
/static/709f1448c7454f42c3b73cd9692a7b65/e996b/ai-writing-screenshot.png 1050w,
/static/709f1448c7454f42c3b73cd9692a7b65/087e3/ai-writing-screenshot.png 1575w,
/static/709f1448c7454f42c3b73cd9692a7b65/8079d/ai-writing-screenshot.png 2100w,
/static/709f1448c7454f42c3b73cd9692a7b65/8579d/ai-writing-screenshot.png 2412w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Well, thank you very much my fellow digital weirdo.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;The broken promise of Web 3.0&lt;/h2&gt;
&lt;p&gt;Web 3.0 was supposed to be a shift towards a more user-controlled, decentralized, and “trustless” internet, where users have greater control over their online identities and assets.&lt;/p&gt;
&lt;p&gt;Well, only in fairytales. Hot take? Yes it is. Obscured? I know!&lt;/p&gt;
&lt;p&gt;But think about it. The reality is far from user-controlled or decentralized; AI chatbots have replaced search engines, often without citing their sources. We went from “let me look it up” to “let me trust the black box.” You don’t even know where the answer came from. You just… accept it.&lt;/p&gt;
&lt;p&gt;This shift undermines the original vision of Web 3.0. Instead of giving users &lt;em&gt;more&lt;/em&gt; control, we’ve handed it over to models that hallucinate with confidence.&lt;/p&gt;
&lt;h2&gt;AI features that nobody asked for&lt;/h2&gt;
&lt;p&gt;Have you seen the new smartphones? You don’t have to learn Photoshop these days to edit your photos. Just use your imagination. You don’t have to take any photos either. Which makes it &lt;a href=&quot;https://www.theverge.com/2024/8/22/24225972/ai-photo-era-what-is-reality-google-pixel-9&quot;&gt;increasingly difficult&lt;/a&gt; to distinguish between real and AI-generated ones. Same for &lt;a href=&quot;https://www.theverge.com/tech/901461/wikipedia-ai-generated-article-ban&quot;&gt;wikipedia&lt;/a&gt;. Same for &lt;a href=&quot;https://www.youtube.com/watch?v=n1bSKBGiVKs&quot;&gt;music&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So what’s a photo even worth anymore? When every image could be fabricated, the concept of visual evidence starts to erode. Your vacation selfie has the same credibility as a deepfake.&lt;/p&gt;
&lt;p&gt;Microsoft has slapped Copilot into every product; Word, Excel, Teams, Outlook, Windows itself. It’s like they took the “AI-powered” label and used it as wallpaper. Most of the time it’s… not great. The suggestions are generic, the context awareness is shallow, and the features constantly interrupt your workflow to offer “help” you didn’t ask for. But worse — every suggestion routes through &lt;em&gt;their&lt;/em&gt; model, &lt;em&gt;their&lt;/em&gt; cloud, &lt;em&gt;their&lt;/em&gt; rules. The user doesn’t gain control. The platform does.&lt;/p&gt;
&lt;p&gt;That’s not decentralization. That’s distortion at scale.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/0512971bb3c692cd911f0231f548df9d/8f1fd/toaster.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.92015209125475%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAABzUlEQVR42nVSyXKbQBDV1/uaSw72KRX7kotdcSqL4/iACykEow0kmGERRBaLikWIaEUL8TOjsqRK/A5U87p7+vWbqRRF8fcAk8lktVqBxHez2RQlwBf/Q4XlUDqbzebzeaPRCIIAzHQ6zfMlgu12W7yCCnLlwD+djhLHca1W830/CPx+v5/nOZOD4LCHkfvmJEkMXVc1Vdf1KIpkWbYsa71eP6fiuNlshunoXxW75izLBoPHdrsNzZ7nYWwYhtgfM9MsI5omfPiUhJGiKFQjLxbsdkaR57qtVqtTwjCMJB1hC9MwSKe7zlfC+SVO/3L18eLkTTadMBW7ZkSEEAxM0xTKu5raFMRatVoXH65P37kDlxd+Up3e3d5WufvnyaX+vduwejgcwq0HUXz0Pe7tuaWopmNfn72PXL8uSfy3H2q3K4i/bNveGcbUoxl3g4Udx4FbUF7leXg2Ho+VeuPu89fvNzeU0F6vB12Ekr3sl7UppbAdtsFe23F4nodD9m8HdlqWCQtQBmmy3D5qRgR7cSpYVVU5jhuNEtM0QUqSBHKxWLCbgylQd2QYErAapyJ2SyC3zJcALv/wblEJ8mhn/GxKMJY9Bkbi2b72PJ8AaETQUSqrLCgAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Illustration: A person walks past a desk where every object — mug, lamp, fan, stapler — has a red award ribbon attached to it.&quot;
        title=&quot;&quot;
        src=&quot;/static/0512971bb3c692cd911f0231f548df9d/e996b/toaster.png&quot;
        srcset=&quot;/static/0512971bb3c692cd911f0231f548df9d/17741/toaster.png 263w,
/static/0512971bb3c692cd911f0231f548df9d/52211/toaster.png 525w,
/static/0512971bb3c692cd911f0231f548df9d/e996b/toaster.png 1050w,
/static/0512971bb3c692cd911f0231f548df9d/087e3/toaster.png 1575w,
/static/0512971bb3c692cd911f0231f548df9d/8079d/toaster.png 2100w,
/static/0512971bb3c692cd911f0231f548df9d/8f1fd/toaster.png 2528w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Good news: your toaster has opinions now. Bad news: they’re mostly about bread.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I’m glad that companies like &lt;a href=&quot;https://procreate.com/ai&quot;&gt;Procreate&lt;/a&gt; exist. Computers are here to support us on expressing ourselves, not to replace us.&lt;/p&gt;
&lt;p&gt;But here’s the thing. While companies were busy sticking AI labels on their products, the actual revolution was happening underneath. Not in boardrooms or marketing campaigns — in the hands of people who stopped waiting for permission.&lt;/p&gt;
&lt;h2&gt;The $236 billion agent economy&lt;/h2&gt;
&lt;p&gt;We’re at day one of something massive.&lt;/p&gt;
&lt;p&gt;The shift isn’t just from “using AI tools” to “using better AI tools.” It’s from &lt;em&gt;using tools&lt;/em&gt; to &lt;em&gt;hiring invisible teammates&lt;/em&gt;. AI agents that plan, decide, and execute tasks autonomously. That connect to your apps, your databases, your APIs, and just… &lt;em&gt;do&lt;/em&gt; the work for you.&lt;/p&gt;
&lt;p&gt;According to &lt;a href=&quot;https://www.epam.com/ai-report-2025/introduction&quot;&gt;EPAM’s 2025 AI Report&lt;/a&gt;, nearly half of companies consider themselves “advanced” in AI — but only 26% have actually shipped anything. The rest? Still making PowerPoints about it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;And here’s where it gets interesting for us developers:&lt;/strong&gt; Agents will &lt;em&gt;replace&lt;/em&gt; SaaS websites.&lt;/p&gt;
&lt;p&gt;Let’s process this for a second. Focus on your breathing everyone. Breathe in, breathe out….&lt;/p&gt;
&lt;p&gt;Just picture this. Why do you need a UI if the agent can just do the thing for you? The interface becomes irrelevant. It’s all about the logic and the tools underneath.&lt;/p&gt;
&lt;p&gt;The window for early movers is open. But it’s closing fast.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a9a3af4882662dad94d7d025ca12685c/6c68b/token-bill-screenshot.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 76.04562737642586%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABYlAAAWJQFJUiTwAAACBklEQVR42qWT3W7TMBTHyx1f0oTYBaWLGpo4ie18Z027sqmaqrVVVamTegNXy8UeabwO3LBHGITxJtuswzlRkqZQgSYi/WTHPv6fv+3jVgu/U+lMRyfOpeOJC9tm2YZe5nkiY4xlmqZl3W43255nmeOwC8exLqWUs1b57Q1c+2Y44sBdrji3AQMKcAF4niz6hvEOTLNXzzVilBAOcO58R62XJLhv44/FLGDMuLcsUzXBsbpPi3+fR+4pMSa8Ra1XJPjadUUuMUu5GCpsAoMrrMZ/I44SofuGoJQ8FzsEDWYW29xgFC3G/V3QdWUtWDtAhj6HOPQgjgMIAhd83y361FZudwri4eZ0GSRI2Qlyk7o2JJEHh/24EIoivxT3aqeViS1BPNCcBoXgKo5DXBgUBFUb+BCGG2gsSaKCKPSV54ptQcyW0xYwQK3X57BaLQsWixkyh/l8CrPZWc1yuYDpdILjZzA+HSuJpfWHIDnErajj4xEMhykcHQ1Kd96WO3KbpgkMBocQJjGcn/TV6n0Cmq7f7m0Ejbwq0mbR0kXtAssMaJtCckh9oQaBAL1Zh7qufaMt4wHfIQ//Ag0gvQeG9EzjrigvvIdKcP/jh/WPPt6kZRn49Cx4JIoeBRr6WQm+mEzGV5jtut1+8/ng4O2XR4JrOl87nfYn1HpOgk+Qp6X6//CMtH4B949k9b4p9fMAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Screenshot of Claude&amp;#39;s chat interface showing a message asking it to check tasks, email a boss, book a Maldives trip, and tell the plants goodbye.&quot;
        title=&quot;&quot;
        src=&quot;/static/a9a3af4882662dad94d7d025ca12685c/e996b/token-bill-screenshot.png&quot;
        srcset=&quot;/static/a9a3af4882662dad94d7d025ca12685c/17741/token-bill-screenshot.png 263w,
/static/a9a3af4882662dad94d7d025ca12685c/52211/token-bill-screenshot.png 525w,
/static/a9a3af4882662dad94d7d025ca12685c/e996b/token-bill-screenshot.png 1050w,
/static/a9a3af4882662dad94d7d025ca12685c/087e3/token-bill-screenshot.png 1575w,
/static/a9a3af4882662dad94d7d025ca12685c/6c68b/token-bill-screenshot.png 1888w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;And while you’re at it, don’t forget to pay my bill for the Pro Ultra Max tokens. You owe me.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;The last app you’ll ever download&lt;/h2&gt;
&lt;p&gt;Claude can browse the web for you. It can send emails. It can schedule tasks, execute them step by step, and &lt;a href=&quot;https://www.anthropic.com/news/3-5-models-and-computer-use&quot;&gt;report back&lt;/a&gt; when it’s done. Click, type, navigate, done.&lt;/p&gt;
&lt;p&gt;And Claude isn’t alone. Every major AI lab is racing toward the same finish line: an AI that doesn’t just answer questions, but &lt;em&gt;does the work&lt;/em&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Why would you open seven apps when one AI can operate all of them?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Gartner predicts that &lt;a href=&quot;https://www.gartner.com/en/newsroom/press-releases/2025-08-26-gartner-predicts-40-percent-of-enterprise-apps-will-feature-task-specific-ai-agents-by-2026-up-from-less-than-5-percent-in-2025&quot;&gt;40% of enterprise applications&lt;/a&gt; will feature task-specific AI agents by 2026. Up from less than 5% in 2025. That’s not evolution — that’s extinction-level pressure on standalone apps.&lt;/p&gt;
&lt;p&gt;The app economy was built on one assumption: people will learn your interface to get value. That assumption is &lt;a href=&quot;https://www.forbes.com/sites/jaimecatmull/2025/10/01/ai-agents-are-killing-apps-and-reshaping-business-investing-and-work/&quot;&gt;dying&lt;/a&gt;. Not because apps are bad, but because the middleman between you and the outcome is no longer necessary.&lt;/p&gt;
&lt;p&gt;You don’t need Trello if AI manages your tasks. You don’t need Grammarly if AI edits as you type. You don’t need Expensify if AI scans your receipts and files the report. The app was never the point. The &lt;em&gt;outcome&lt;/em&gt; was the point. AI just removed the detour.&lt;/p&gt;
&lt;p&gt;I’m afraid, this is what Web 3.0 actually looks like — not decentralized servers, but a single intelligent layer between you and everything.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/f880278e6372cbc33e7dd1e2ac3d2a7a/8f1fd/concierge.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.92015209125475%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB5ElEQVR42l1S646aYBD1sfsC3d99ho2J2ZqU2G3VaHAR0YIgt4qCrLpIRRS03vAu9GRpbGUSCHzfnDNn5kwqDMMoik6n02q1CoLgcDjs9/v4fT6fcR4nxBHeRyq+u1wulmW1223f92VZHgwGk8lEURTXdXW9i99+vw/2JPh6vQK8XC6RMRqN1J/q8G1oWW9AVqtVlv3RaDTq9bqqqtPpNFH8L3i9Xs9mM3ywLNt5j1ar1e12CoV8qVSqkCRFvei6ngTHstEeNJumqWma73me73McJ4pirUbX8PDsyLYZppZOP2IuyEfJf5V3ux3Hsc0mJ0kyTjAtVYVwZeZ5As8zHz5F24Pjug8PH52JkwSjZ1FsSZLY5JtQoWkdiqJourrZbCDsd5oMV4Ht/ALdYrG4k40ABVyZz+fb7RYqIJVhmO/5vKkbPq85c+8ahePxGIoABnvcfOpGczPzeDxmMplisUgQxPPzV+Pxm/BSkxSlZxi2bWOukJMEx/URuyAoFArZbLZcLpOVimENZFXB4NAI1MFOiE/KBhlkoxpN08gAPTYE81dlhSTJXC739PlpOBzClNdX8w58W1JkwyRQYCuwavC21+uBCBtCEF9gpCAIqJEE/985rgHAYMJ32+IOsUjx8t+m/QfhrcABfee65QAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Illustration: A concierge behind a counter hands a card to a customer, while icons for a calendar, envelope, checklist, location pin, and receipt hang above them.&quot;
        title=&quot;&quot;
        src=&quot;/static/f880278e6372cbc33e7dd1e2ac3d2a7a/e996b/concierge.png&quot;
        srcset=&quot;/static/f880278e6372cbc33e7dd1e2ac3d2a7a/17741/concierge.png 263w,
/static/f880278e6372cbc33e7dd1e2ac3d2a7a/52211/concierge.png 525w,
/static/f880278e6372cbc33e7dd1e2ac3d2a7a/e996b/concierge.png 1050w,
/static/f880278e6372cbc33e7dd1e2ac3d2a7a/087e3/concierge.png 1575w,
/static/f880278e6372cbc33e7dd1e2ac3d2a7a/8079d/concierge.png 2100w,
/static/f880278e6372cbc33e7dd1e2ac3d2a7a/8f1fd/concierge.png 2528w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;You used to need an app for that. And that. And that. Now you just need one very confident concierge.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;You don’t need the f*ing manual anymore&lt;/h2&gt;
&lt;p&gt;Here’s something that would have sounded insane five years ago: you don’t need to learn new software.&lt;/p&gt;
&lt;p&gt;Every platform you’ve ever struggled with — the configuration screens, the keyboard shortcuts, the YouTube tutorials titled “Figma for Beginners (2026 UPDATED)” — all of it exists because software was designed for the &lt;em&gt;average&lt;/em&gt; user, and you had to meet it halfway.&lt;/p&gt;
&lt;p&gt;AI flips that. Instead of learning how the tool works, &lt;a href=&quot;https://stitch.withgoogle.com&quot;&gt;you tell AI what you want&lt;/a&gt; and it writes the code to make it happen. Personalized. Disposable. Tailored to exactly your workflow.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The manual is dead. Long live the prompt.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You don’t need to master Excel formulas — AI generates the spreadsheet logic you described in plain English. You don’t need to learn a new project management tool — AI builds you a custom one that fits how &lt;em&gt;you&lt;/em&gt; think. The code exists for exactly one session, one purpose, one person. And then it’s gone, because you’ll never need it again.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/32645dfef178b9fafa4968c434f0e471/8f1fd/figma-books.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.92015209125475%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAACmElEQVR42m2S70tTURjHfVH0bxjTwBe9iBDpBxHZC+vlehFRYAlF5A9CXFoiGgpFUdDIXKFuWemdGtPJJtf9uJvuzs118e7ndd3ramtzG7rdO71r7pe3xyaZ0YFzOPc538/zfc5zbtnO/4YgCE6nU4+ZF8xYeouDT45l16LRXC4n/CUr+wcrFAoglclkRw4fqj5x/E1X/dS7nmgsTtNfcRynKApOi8XiARhCsEIUNrlctrrm1PmTFfOKh4Syu69ZPIYoA6urbq8XnzNuslxJvA//SQbOGxsbuNXaeL1u/v0j83AbOi4L/Vhj2aTVarWNz3zzM9xmquR/wLkUWl9fp6iVoSf3LYr2j88bP08gNMP4GXp2BLFParZ+poPBYMlsF87n8z6fz+PxsCwLRUAKcsky/arZOPRA+fTeJ2mXVCpVzajNyJRNjabSPE3Tu50ThDKY0AaNRrNoWzQYDNvbGYhYZpV6WcvA41uqzttz0mbCbgqFIxajCbfg34NBr9fLcdwe7HK5tFoNSZIoippMpgS3qZ986xjtkDRcHui8uahodVmmdvKCaxZDJ1RLDgdBEMlkcg+ORCJy+fC0ehpRKnv7el1ut/bDyxVVd9ONup6mK19GO+y6sUK2GCZ9DhTLZDJAwgX3YLjAGIK87u8fHByUSCQk6VwLBVrrL108V9PZeI0Ybbdr5dBMPs37/X7QR6NRSLELl952fmFBr9erZ9RyhRzDMJ7nRSLRUVFld9sd20irGXlW3BESiUQoFAInWLPZ7D7MMEwgENDpdNAMt9sNVR2rrDh95mzL3YYXkqsUYQZNLBYLh8NbPA/K/Z8Enmp5eZny+QxGI/QD9vF4vKqq6kJtrVgsLi8vF36PVCoFecEZyi45/wKLbXE+EphZ/QAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Illustration: A tailor carefully cuts a garment on a standing model while a tall stack of books sits untouched on a chair nearby.&quot;
        title=&quot;&quot;
        src=&quot;/static/32645dfef178b9fafa4968c434f0e471/e996b/figma-books.png&quot;
        srcset=&quot;/static/32645dfef178b9fafa4968c434f0e471/17741/figma-books.png 263w,
/static/32645dfef178b9fafa4968c434f0e471/52211/figma-books.png 525w,
/static/32645dfef178b9fafa4968c434f0e471/e996b/figma-books.png 1050w,
/static/32645dfef178b9fafa4968c434f0e471/087e3/figma-books.png 1575w,
/static/32645dfef178b9fafa4968c434f0e471/8079d/figma-books.png 2100w,
/static/32645dfef178b9fafa4968c434f0e471/8f1fd/figma-books.png 2528w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;“The Figma books? Oh, those are load-bearing now — they hold up the chair.”&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This is the &lt;a href=&quot;https://www.reddit.com/r/vibecoding/comments/1m8rrtp/will_ai_eventually_make_the_traditional/&quot;&gt;vibe coding&lt;/a&gt; thesis taken to its logical extreme. It’s not just developers who can skip the learning curve anymore. It’s &lt;em&gt;everyone&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The entire SaaS business model is built on platform lock-in — “learn our tool, store your data here, pay monthly forever.” When AI can replicate the functionality of any app on demand, lock-in evaporates. The wall disappears. The SaaS subscription model starts looking like a tax on people who haven’t switched to AI yet.&lt;/p&gt;
&lt;h2&gt;You own nothing&lt;/h2&gt;
&lt;p&gt;Remember buying video games in big cardboard boxes? Feeling the weight of the manual, flipping through it on the car ride home? That box was &lt;em&gt;yours&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Now it’s a license on Steam. Your music? A Spotify subscription — miss a payment and your entire library vanishes. Your movies? Netflix decides what you get to watch this month. Your design tools? Adobe went from a one-time purchase to a monthly fee, and now charges &lt;em&gt;extra&lt;/em&gt; for generative AI fills on top of that.&lt;/p&gt;
&lt;p&gt;Every technological shift took something away from us. First the physical object. Then the ownership. Then the permanence.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;We didn’t just lose the box. We lost the thing inside it.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And now AI is coming for the content itself. When anyone can generate music, art, and writing on demand, what’s a Spotify playlist even worth? What’s an album that took a year to produce, competing against one that took three minutes to render? The entire concept of a &lt;em&gt;collection&lt;/em&gt; — something personal, curated, meaningful — dissolves when everything becomes infinitely reproducible.&lt;/p&gt;
&lt;p&gt;We’re so busy debating whether AI will take our jobs that we forgot it’s already taking our shelves.&lt;/p&gt;
&lt;h2&gt;Your notes are the new software&lt;/h2&gt;
&lt;p&gt;So if apps die and code becomes disposable — what’s left?&lt;/p&gt;
&lt;p&gt;Your thoughts, I guess?&lt;/p&gt;
&lt;p&gt;And writing your notes are exactly that. Not notes as in “things you jot down during meetings.” Notes as in the accumulated context of &lt;em&gt;who you are, what you care about, and how you work&lt;/em&gt;. Your preferences. Your decisions. Your frameworks. Your domain knowledge. The stuff no AI model ships with out-of-the-box.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The person with the best notes gets the best AI.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Your notes don’t just &lt;em&gt;store&lt;/em&gt; information, they &lt;em&gt;instruct&lt;/em&gt; AI on how to act on your behalf. Your second brain becomes the operating system for your AI workforce.&lt;/p&gt;
&lt;p&gt;AI without context gives you generic output. AI &lt;em&gt;with&lt;/em&gt; your notes — your writing style, your project history, your technical decisions, your values — gives you output that sounds like you, thinks like you, and works the way you work.&lt;/p&gt;
&lt;p&gt;The investment isn’t in learning tools anymore. It’s in building &lt;em&gt;context&lt;/em&gt;. Every note you write, every decision you document, every preference you capture — that’s training data for the AI that works for you. Not training data for OpenAI or Anthropic. Training data for &lt;em&gt;your&lt;/em&gt; personal agent.&lt;/p&gt;
&lt;p&gt;And that changes everything. Because the competitive advantage isn’t which software you use or which subscription you pay for. It’s the depth and quality of your notes.&lt;/p&gt;
&lt;h2&gt;The real Web 3.0&lt;/h2&gt;
&lt;p&gt;Web 3.0 was supposed to be decentralization. It wasn’t.&lt;/p&gt;
&lt;p&gt;The real Web 3.0 is this: AI as the universal interface. Code that writes itself for each person. And a set of notes — &lt;em&gt;your&lt;/em&gt; notes — as the only software you’ll ever need.&lt;/p&gt;
&lt;p&gt;The battle isn’t between apps anymore. It’s between companies that understand this shift. And I’m not talking about companies who build copilots and toolbars. The technology is extraordinary. The way we’re using it? That’s the part that needs work.&lt;/p&gt;
&lt;p&gt;I could have asked AI to write this post for me. It would’ve been faster, probably more polished, maybe even more “optimized for engagement.” But it wouldn’t have been &lt;em&gt;me&lt;/em&gt;. And in the age of AI, that’s the only feature no one can replicate.&lt;/p&gt;
&lt;p&gt;Because in a world where AI can do everything, the most valuable thing you can do is be yourself. The AI will handle the rest.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The code is fine—You are not]]></title><description><![CDATA[The pressure to achieve perfection in software engineering can lead to anxiety and burnout. Key points include the struggle with communication about feelings, the cycle of perfectionism and procrastination, and the impact of external expectations. Embracing imperfection and acknowledging human limitations is essential for sustainable work.]]></description><link>http://www.nicotsou.com/the-code-is-fine-you-are-not/</link><guid isPermaLink="false">http://www.nicotsou.com/the-code-is-fine-you-are-not/</guid><pubDate>Mon, 16 Mar 2026 10:00:00 GMT</pubDate><content:encoded>&lt;p&gt;You’re good at what you do. You’ve untangled race conditions, pixel-pushed a UI until it matched the design down to the last shadow, wired up CI pipelines that deploy on green, and debugged production issues at 3 AM on a Saturday. You’ve navigated framework migrations, accessibility audits, and that one API that returns a different shape every other Tuesday.&lt;/p&gt;
&lt;p&gt;But today, the cursor just blinks.&lt;/p&gt;
&lt;p&gt;People are chatting in Slack as if they never talk to a human before in their lives. The AI dumps screens of code you don’t want to read—I mean, how much code you can possibly review written by this &lt;em&gt;thing&lt;/em&gt;. You scan the docs and a couple of Stack Overflow answers; they disagree. Your team is waiting, and you’re out of ideas.&lt;/p&gt;
&lt;p&gt;You try the workaround. It doesn’t fit your constraints. You chase the fix and hit another pile of bottlenecks. You finally have something, but you hesitate to open a PR—you don’t want yet another design debate.&lt;/p&gt;
&lt;p&gt;Standup is in 10 minutes. You’ll say, “I’m making progress,” even though you know it’s nowhere near true. And all of this before a 2-hour planning meeting that will destroy your energy.&lt;/p&gt;
&lt;p&gt;Now, you’re chatting with the AI again to review the code and fix what’s fixable. You don’t care anymore about reading that code—your colleagues will review it anyway.&lt;/p&gt;
&lt;p&gt;If this feels familiar, you’re not alone—and it’s not about the code.&lt;/p&gt;
&lt;h2&gt;The Paradox We Don’t Talk About&lt;/h2&gt;
&lt;p&gt;Here’s something strange about software engineering: we’ve learned to speak with absolute precision to machines. We can express complex logic, handle edge cases, and architect systems that process millions of requests. Our IDEs autocomplete our thoughts. Our types catch our mistakes before runtime.&lt;/p&gt;
&lt;p&gt;But when it comes to talking about how we &lt;em&gt;feel&lt;/em&gt; about our work? We’re terrible at it.&lt;/p&gt;
&lt;p&gt;We communicate fluently in TypeScript and Python, but we stumble over admitting we’re anxious about a deadline. We can articulate the difference between eventual consistency and strong consistency, but we can’t articulate why we feel inadequate when a colleague ships faster than we do.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;We’ve optimized for machine communication at the expense of human connection.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And somehow, that precision we demand from our code has bled into how we see ourselves. We want to be &lt;strong&gt;perfect&lt;/strong&gt;. We want our solutions to be elegant, our architecture to be flawless, our knowledge to be complete. And when we inevitably fall short of that impossible standard, we don’t talk about it. We just carry it.&lt;/p&gt;
&lt;p&gt;The situation gets worse in company cultures that treat people like machines. Requirements in, code out. No time for mistakes. No room to fail.&lt;/p&gt;
&lt;h2&gt;Where This Comes From&lt;/h2&gt;
&lt;p&gt;This isn’t new. It’s not unique to software engineering. But it hits us harder because of how we got here.&lt;/p&gt;
&lt;p&gt;Think back to how many of us ended up in tech. Growing up, we were the kids who got good grades—or just people who were good with computers. We were praised for being smart, for knowing how to deal with tech, for solving problems.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Our parents, our teachers, our entire environment reinforced a simple equation: &lt;strong&gt;being the best = being valuable&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;We internalized that. We learned that our worth was tied to our performance. That being good wasn’t good enoug—we had to be &lt;em&gt;the&lt;/em&gt; &lt;em&gt;best&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;When you carry that mindset into a career like software engineering—where there’s &lt;em&gt;always&lt;/em&gt; more to learn, &lt;em&gt;always&lt;/em&gt; someone who knows more, &lt;em&gt;always&lt;/em&gt; a better way to solve the problem—it becomes a weight you can never put down.&lt;/p&gt;
&lt;p&gt;That voice that says “this code isn’t good enough yet” isn’t really about the code. It’s the echo of every time we were told to &lt;em&gt;do&lt;/em&gt; better, &lt;em&gt;be&lt;/em&gt; better, try &lt;em&gt;harder&lt;/em&gt;. &lt;strong&gt;It’s the internalized belief that if we’re not perfect, we’re not worthy.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/0186859896c76d1fc4de5f8b77799acd/67a79/child-in-computer.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 54.372623574144484%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAC4jAAAuIwF4pT92AAACpUlEQVR42mNwUePz1hUKNBLLdFNsitQp81cr9VWdkGrUGas7IcO0MkSrIUq3JVbXRYNfR4xdUxSE1ARZ1QVZ1QRZGFQF2Xz0hcPMJSzluSdlW6a4KulKcC5v9dk+IeDK6vjdU8MOz0nqSTOujjIMMJO2VxfQFGFX4GOW5mKS4mJiEGVnCjYVc9UUUOVnnl7qGGwmkearvX9ZVkmMaUWs0f55yRc3li1vCTi5unJuXVCen7qVIq+KALOWCKs8LzODGAdTqLmErSJPjL1CXqCWDAtDfbr9qe11UY7KWQE6h1fkdKZZ1MWYbFnVsXlZc0Giq7E8n4k0l6ogi7oQK4M4J3OombieOLuFPLezhoCmAENOuFlLnoe7rtD+Zdmz63xNJVijHBSrKxM2rJtY2lnf3FEWbipqKsejCtQswMqQ5CDjrManL8Fur8JrIs3moSfsqS/spcfXU+CY6atlK8/ZXRtX1FbTUBU7Ye3CycvnBujxuWsKuGsLMgiwMQYZiRhKsIVYSOUGaGkKMUTayoRbSaZ7KDQmGMU4KOT7asyaXF5VEjN1Vmvn5q1TZ7YH6nA6q/P76QsziHEyAx0szckQaCpREqqjK8KY7qUaZCpppczvbyxZFGszsSO9O8duXnv8hJld7Vu293YV+2iwOarxe+oIgTQbSXFKczE4qvOlOMvpiLIEmYiaSHOKszF46YkWx1gWxNo15LiXFESEJkeVd1SXJbt6anLbKvMA/cggwsGkIcImw8VkLM3uoSVgIs0ebCKqJswmy8Nkq8If66wa46jUlO4wb3rFrAWdmQG6iU5yloq8ysBEAgptLmYtUTZFXhYjKXYLOU4TaQ5/fUFzOU5VQVZ5PhZPXeFgE/FEJ4V0X53cSKtULw1vPWFTGS5gCpPhZgIATAinffDK5oQAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Child in front of computer&quot;
        title=&quot;&quot;
        src=&quot;/static/0186859896c76d1fc4de5f8b77799acd/e996b/child-in-computer.png&quot;
        srcset=&quot;/static/0186859896c76d1fc4de5f8b77799acd/17741/child-in-computer.png 263w,
/static/0186859896c76d1fc4de5f8b77799acd/52211/child-in-computer.png 525w,
/static/0186859896c76d1fc4de5f8b77799acd/e996b/child-in-computer.png 1050w,
/static/0186859896c76d1fc4de5f8b77799acd/67a79/child-in-computer.png 1408w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In a still &lt;a href=&quot;https://www.zippia.com/software-engineer-jobs/demographics/&quot;&gt;male‑dominated field&lt;/a&gt;, many of us absorb quiet rules of restraint: stay composed, avoid showing doubt, handle it alone. Those norms make it harder to ask for help or name it when we’re stuck.&lt;/p&gt;
&lt;p&gt;“I’m fine” becomes our default API—even when we feel like we &lt;em&gt;suck&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;How It Shows Up Every Day&lt;/h2&gt;
&lt;p&gt;Here’s the cycle:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perfectionism&lt;/strong&gt; stops you from starting. You hold back code because you’re not sure the approach is “right.” You avoid asking in Slack. You don’t share work until it’s over‑polished.&lt;/p&gt;
&lt;p&gt;That perfectionism turns into &lt;strong&gt;procrastination&lt;/strong&gt;. You spend hours searching for the “best” solution instead of shipping the working one. You refactor the same function again. You wait for the perfect moment—and it never comes.&lt;/p&gt;
&lt;p&gt;Procrastination builds &lt;strong&gt;anxiety&lt;/strong&gt;. The deadline hasn’t moved. Standup is coming. The PR is overdue. Expectations stay put; now you’re &lt;em&gt;behind&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Anxiety then reinforces perfectionism&lt;/strong&gt;. The stakes feel higher, so you raise the bar, second‑guess more, and delay again—completing the loop.&lt;/p&gt;
&lt;p&gt;It’s a loop that’s hard to break.&lt;/p&gt;
&lt;h2&gt;How it can lead to burnout&lt;/h2&gt;
&lt;p&gt;Here’s how that loop showed up on my team.&lt;/p&gt;
&lt;p&gt;I once had an engineer who struggled to finish his tasks. He’d pick up the ticket, lose confidence, and start over. Dates slipped with “I’ll have something by Monday.” But we all knew it wouldn’t happen—and found ourselves guessing what excuse Monday would bring. Ask “what’s next?” and suddenly more research has to be done. You could feel him trying to change the subject to waste time in a call, so we didn’t have enough time to discuss his progress.&lt;/p&gt;
&lt;p&gt;Does this sound familiar? It’s the same cycle — perfectionism, procrastination, the excuses at standup.&lt;/p&gt;
&lt;p&gt;As an engineering manager leading 16 engineers across different client projects — with two second-level managers reporting to me — I had visibility into how this pattern plays out across different teams, codebases, and stakeholder pressures. It wasn’t isolated to one project or one personality. I saw it repeat across people, seniority levels, and contexts. But what struck me most wasn’t the pattern — it was how easy it was to misjudge it.&lt;/p&gt;
&lt;p&gt;It’s easy to call my colleague lazy, but the reality is more complicated.&lt;/p&gt;
&lt;p&gt;He was stuck chasing &lt;em&gt;perfection&lt;/em&gt;. This was obvious to me because I’d struggled with it for years. In fact, that’s how I ended up starting this blog in the first place. I wish I could just press a button to install this understanding in his brain, but these things are hard to accept—and even harder when someone else is flagging them for you.&lt;/p&gt;
&lt;p&gt;There’s a useful distinction here. Researchers talk about two kinds of burnout: &lt;strong&gt;overwork burnout&lt;/strong&gt; and &lt;strong&gt;misalignment burnout&lt;/strong&gt;. The first is about volume—too many hours, too little rest. The second is about working on the wrong things, or feeling a persistent gap between what you value and what you actually deliver. The critical difference:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Rest fixes &lt;em&gt;overwork&lt;/em&gt;. It doesn’t fix &lt;em&gt;emptiness&lt;/em&gt;. You come back from vacation and the same emptiness is waiting at your desk.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;My colleague wasn’t drowning in tasks. He was drowning in the distance between his internal standard and his actual output. That’s misalignment burnout dressed as procrastination. And it’s harder to spot—because from the outside, it just looks like someone isn’t trying hard enough.&lt;/p&gt;
&lt;p&gt;Over time, the anxiety can spread. It’s not just one deadline—it shows up in standups, when replying in Jira, and when you’re thinking about work in the evening. That’s worth noticing.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/0a14577aa86a2c68905d015657b8e519/97a96/endless-scrolling.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.52471482889734%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAIAAABr+ngCAAAACXBIWXMAAAsTAAALEwEAmpwYAAADj0lEQVR42g3OWXMaBQAA4H30zRedWjFAbgTCwrKc4YbdcC5XuNmFJRyB7QIBJBwJDQKhgWQ606aTxDQynUbbNFVHZ3zoZDTWacyMT53xoY4PRseJb/4H+wu+D4AWorpIHkk1bEt3ndR+4sWb7Kt/Uud/pl/+nX31b/biOvPTVfbir/DjF5KVLdhH2SwhlTEkMUVYQhSQWol5bzIUbzQDR13fk8joJXn2O/3LNfXjH8nnl+SX57mzN/TrK+fus0C2E7vVcTlwvsQqNvomIQSQoUFNMJMiN5r+h+uB4/DW197NI6TYFWFx0LUEYpnwwVf0r1eeh9+Vs71GoWdBiTm5V4j6x0E9ACNBS6RYTgzbi6NB9FtHfigJlsZBD1vkm2R64elkdvtk6efXzr3TarrXoDpeC2FC/dOwYUpuA6aERjR4a43+7G7w5BA/QxNtXWoTsQycgVGS/qFf/603PKePL1P97xupTjmzlg6RBqMHNPrkoRLAGBdOgVrMSVGBraXYnrV14C8Ptnau+2f/kf39TO1R0L2xXdq/v3PhKG6TeLvu6hgUcdhOaPAqMMeTcCEdU6znq53KlV565wtvYDleOy0Pn6n9hNld1eB1S4h26zKZ/Ogk8vnIc4hocxw1doMFAio0IFbb9FIUFc3DZlw/OLbTt43RKka05o2ubL43vLP3qDX0yyMV83aSuCfT5CAkIU9/MjYrA7RYEjbYCxay5ybt/pwr294ki6AzqY+2La7Vlcy9w/Wn/crAZY55Bemop7uQ3TUSt9WN4dspoNEtitXOgn35TnQ1FmvARL253IzkqsHgQd/zTR7ddQoqfGuahZEfjAnx+UKx9Nwa31RkajydB+CKDAKt16lxBxwJU7AA4XWqMqjWuppQTS+KikQe1k3NLM+ubD7gSn0wZ2EdP1os74nDFPet/DFkmoD0qBLz2EmVP6cgVtV0v0mtG6hPZbKcdqGixNfEvgIPi4EKnD9n9qhyZrzH86Y5SgwA5Tb2LKRS2CNYIhQuWMMlNNaMUV2Fd1kZrUpcSTaovckUMBjgNEc/wTW5Na5CuDWJJVgCPcDiaT6ckbD5Ko7czhNbhAoHpPNLw2WxjYRtcchEMMfk773LZX0Ev8+Aokjscb1xuXXfgaTfYQqBGzMqttwBqnxcyCIQI1wRyuAoWSJ0Rh9iS2wTOh/XFp+RYmwxKpnVndJrpxu9HtU6Lg6lItv/dKFIhKErOJUAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Endless scrolling&quot;
        title=&quot;&quot;
        src=&quot;/static/0a14577aa86a2c68905d015657b8e519/e996b/endless-scrolling.png&quot;
        srcset=&quot;/static/0a14577aa86a2c68905d015657b8e519/17741/endless-scrolling.png 263w,
/static/0a14577aa86a2c68905d015657b8e519/52211/endless-scrolling.png 525w,
/static/0a14577aa86a2c68905d015657b8e519/e996b/endless-scrolling.png 1050w,
/static/0a14577aa86a2c68905d015657b8e519/087e3/endless-scrolling.png 1575w,
/static/0a14577aa86a2c68905d015657b8e519/8079d/endless-scrolling.png 2100w,
/static/0a14577aa86a2c68905d015657b8e519/97a96/endless-scrolling.png 2400w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;And what’s our coping mechanism? Lunch break, you open the app to decompress; the algorithm pulls up a chair and serves fear with a side of envy, followed by an ad for exactly what you didn’t know you needed.&lt;/p&gt;
&lt;p&gt;Repeat this, project after project, and you already know where it leads. You won’t rage-quit or delete your LinkedIn profile. It’s slower than that. You still show up, still deliver, but you’re running on empty.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The work that used to &lt;em&gt;excite&lt;/em&gt; you now just &lt;em&gt;exhausts&lt;/em&gt; you.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Ah yes, and &lt;strong&gt;FOMO&lt;/strong&gt; feeds the entire cycle.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;The Day I Missed the News&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Staying relevant as a software engineer has become increasingly complex.&lt;/p&gt;
&lt;p&gt;I speak at conferences about AI. I build agentic platforms. People expect me to be “on it.” And yet, I missed the model release everyone was talking about—didn’t see it until questions started landing in my inbox.&lt;/p&gt;
&lt;p&gt;For a moment, I felt that familiar panic: if I missed this, what else am I missing? I opened five tabs, skimmed three threads, and queued up two demos. The pressure in my head went up another notch. I’m supposed to be the one who tells &lt;em&gt;them&lt;/em&gt; what’s coming — not the other way around.&lt;/p&gt;
&lt;p&gt;People always ask me what they should learn to stay relevant. Nowadays it’s all about AI. The question never changes; only the thing they’re afraid of does. They worry that the way their organizations work today won’t give them a competitive advantage in the job market tomorrow.&lt;/p&gt;
&lt;p&gt;But beneath the FOMO lies something heavier: the fear that AI won’t just change &lt;em&gt;how&lt;/em&gt; we work—it’ll erase &lt;em&gt;why&lt;/em&gt; we work. For many of us, software engineering isn’t just a job. It’s our identity, our status, the thing that affords the life we’ve built. The apartment, the stability, the sense that we figured it out. When the ground shifts under that, the anxiety isn’t about missing a model release. It’s about losing the version of yourself you’ve spent years constructing.&lt;/p&gt;
&lt;p&gt;No one knows what this industry looks like in five years. And that uncertainty is a breeding ground for misalignment burnout—because how do you align your work with your values when you’re not sure the work will exist tomorrow? You end up working &lt;em&gt;harder&lt;/em&gt; on things that feel increasingly &lt;em&gt;meaningless&lt;/em&gt;, just to stay in the game.&lt;/p&gt;
&lt;p&gt;That’s not overwork. That’s existential drift.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/8c0023d1d2f2d9a173a1d87b9b624891/67a79/ai-noise.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 54.372623574144484%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAC4jAAAuIwF4pT92AAACmklEQVR42gXB608SAQAA8Psn2mp9sA+lm1nNB1MnDvMRqQQGAhJewuEFHgh4vEEeR8jd8T7g4PA4ERQSETEFZ62aq5ZbW/9Tvx9wjz/GM2nVdP65WrTh1qCVjAQy8o1xIXEzijeEdH0NL0Hh9OzqO6PeaDOi1hXZuBZ9KJh78GwU6BcKHEeHW98PYiUT3SpNy8C1IJe//Pv56ofBiVld1Htnwl5v1bq/dsOxYIyVfghOrCN9aug+jweIVFCW65hv9zoN/DViXrXnCr27Wu+sWOGilSTZSARYEuWokz93WIFZ1Gz50jW+BHyhVM6pDQDkSWaOe2R7d9dunvB4QEueYBj2ulykk5Uay7LF8yYXj4a8bIz81lNsOhUmiwNPos7oEdUE7Pt1nKQ+5qweAn9s2IrDsVCRqJ8WmUrZG/Sk6CRVZtonHOpFE+2Oi6pPKNVTSmXAZnHp/MBO9Qr2BZbfSt/A24ZUpoLVQkyq2eIOW3VIvxGn8MPz03antu0wK3FMZuWGQJXAh5gJVy6wBwxC1nm7W7QM88Tr/sphmqmmSfKkVU7RmbFpwfyKLFsqXHWPdYhlxKUbs9f6xeDAzIxerxqChcDT2c3JSHI1GFGqbZiHRa8v6WL8oJBunh4wXDFfyh7Vmf1yRgHBfBUkh2mIxINO1GXbGcW0wLh0ewjEJxHTwitti6Iuft/pajSS9Lgz/lgcI1LRJEMHQ35/PLakMEbc8W42TzVjqZufvhwHCBGTaNk6/EweUYjLyCKeI/xfepoms54jNwtUk+0eh6pIjl2SQ15zpNoo0+G0prd3cfsPXNEAj0ZeSiRK8bAQFs7o1dLBKYHWCxPdsv4Th33tRcIJGDTxRKBvMbGQ87sy2QDqkJ8VI9XWQN+T/zDlOwKvXcbYAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;AI noise&quot;
        title=&quot;&quot;
        src=&quot;/static/8c0023d1d2f2d9a173a1d87b9b624891/e996b/ai-noise.png&quot;
        srcset=&quot;/static/8c0023d1d2f2d9a173a1d87b9b624891/17741/ai-noise.png 263w,
/static/8c0023d1d2f2d9a173a1d87b9b624891/52211/ai-noise.png 525w,
/static/8c0023d1d2f2d9a173a1d87b9b624891/e996b/ai-noise.png 1050w,
/static/8c0023d1d2f2d9a173a1d87b9b624891/67a79/ai-noise.png 1408w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The AI explosion has turned FOMO into a full-time job, and feeling incompetent next to an AI supermachine only makes things worse. Every day there’s a new model, a new framework, a new capability. Every day the frontier moves.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The myth says a “10x engineer” would keep up with all of it. Reality says we have limited time, energy, and retention.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Missing the news, or not following a new trend isn’t failure. It’s proof that I’m human—and that curation beats compulsive catching up.&lt;/p&gt;
&lt;h2&gt;How to reflect&lt;/h2&gt;
&lt;p&gt;I’m not a psychologist. I’m not going to prescribe solutions or give you a five-step plan. That’s not what this is.&lt;/p&gt;
&lt;p&gt;All I &lt;em&gt;can&lt;/em&gt; tell you is this: The stress you feel isn’t just about the code, your team, or the world. It’s about the &lt;strong&gt;unspoken expectations&lt;/strong&gt;—the ones you place on yourself, the ones you think others have of you, the ones that come from being told to be the best for your entire childhood.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a9ae7ff71bee23a0f5ed9b769098bc45/56fb6/finding-peace.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 55.893536121673%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAACYUlEQVR42hXQ+0/aUBQHcJLJQ/rupbe9bbkttOVRBAoFhcJAdBAW52SRYJgPhovJ0ETjZjQme2Rz/rLFmP3qX7trcn44v3zO95wTYiRIilc0HuksRDCV6ewevDu7HR5ftPc+vv10tbO4rm1PK8Oxv7VX7G1VOn231gzHmTBNh2gJxnkBGpZk2BzSCDa91UyzF+zs9w8Xm0enwXjenhx7w3GwO3PqHb/7qrwWLFFUmGFClCQTjCxXsQskOVWu6/kytAt2rbU9Pz28uftwe7/49Ti7uR8trtVcudHd8FfXCI6QZCohxTkB5oqw2mAVzfTbKLsCDDtTb1c2t7qT+dnvfzcPT18ent5ffkP5cjNoNOveEs1ESfLz2gIQVyqg0aFRUmmuy64naphMcbtDbzAazC9m3x/P/zy9ObkU7eJgo73Z8iM0H2O5EAMVGkggUwKVBoewWu+ifCmBUwTn24Nqf7S9P/t79/n864/e9CRX9Gql/MtGdZkVnjEFIC0AkLSAkaGBgrIV2bRFHePiai7o+68ng/H059Xk6GjaHh20ap4CQS5r0zyIMQSL5GaRk3UeYdJL5HPprJLO6HnP8Vtu0Ms11vWsa1umk046aQ1KHJcQKSERYdhQlONjDBvlxDiALFQFDQuqQSVkGqq8aoh6GpqOKksQJiBSk7qSwihpYlbgyMNDtAhZSeFlDagGxJZsOsi0k3Yu7ZazJd8ulHSchBIQJElIAIahLBMJgA9Ho0vLyyERagBhSUsp2FIMW0052Mmb2RWr4GVKVdstYsMgHqmIB4DheJqlI7FwOBJ+EY3+B7TlbNXUtSteAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Finding peace&quot;
        title=&quot;&quot;
        src=&quot;/static/a9ae7ff71bee23a0f5ed9b769098bc45/e996b/finding-peace.png&quot;
        srcset=&quot;/static/a9ae7ff71bee23a0f5ed9b769098bc45/17741/finding-peace.png 263w,
/static/a9ae7ff71bee23a0f5ed9b769098bc45/52211/finding-peace.png 525w,
/static/a9ae7ff71bee23a0f5ed9b769098bc45/e996b/finding-peace.png 1050w,
/static/a9ae7ff71bee23a0f5ed9b769098bc45/087e3/finding-peace.png 1575w,
/static/a9ae7ff71bee23a0f5ed9b769098bc45/8079d/finding-peace.png 2100w,
/static/a9ae7ff71bee23a0f5ed9b769098bc45/56fb6/finding-peace.png 2752w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Immersive Background: Allowing self taking a break&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Mo Gawdat, a former Google X engineer, wrote a book called &lt;a href=&quot;https://www.amazon.com/Solve-Happy-Engineer-Your-Path/dp/1501157558&quot;&gt;&lt;em&gt;Solve for Happy&lt;/em&gt;&lt;/a&gt; where he treats happiness like an engineering problem.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;markdown&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-markdown line-numbers&quot;&gt;&lt;code class=&quot;language-markdown&quot;&gt;Happiness = Your Perception of Events − Your Expectations of Life&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;His equation is simple: unhappiness lives in the gap between your expectations and your perception of reality. Sound familiar? That’s the perfectionism loop, expressed as math. I highly recommend it — especially to engineers. It’s one of the few books that speaks our language.&lt;/p&gt;
&lt;p&gt;Our work isn’t simple. Machines are straightforward—they do what you tell them; when they don’t, you can debug why. People are different: unclear requirements, shifting priorities, unstated assumptions, their own anxieties. We’re not as good at debugging that.&lt;/p&gt;
&lt;p&gt;Maybe naming it helps. Maybe knowing your teammates feel the same way helps too—you can help each other. Maybe realizing perfectionism isn’t a virtue—it’s conditioning—gives you permission to be kinder to yourself.&lt;/p&gt;
&lt;p&gt;Or maybe it doesn’t. Maybe this is just one more thing to think about, one more weight to carry.&lt;/p&gt;
&lt;p&gt;But at least now we’re talking about it.&lt;/p&gt;
&lt;h2&gt;A Personal Note&lt;/h2&gt;
&lt;p&gt;I wrote this after stepping back for a while from the content creation habits. I needed to slow down before I burned out completely. And it was a very hard decision to make. I had to force myself to do it.&lt;/p&gt;
&lt;p&gt;It’s easy to think that stepping back means falling behind. That if you’re not constantly productive, learning, shipping, you’re losing ground. But sometimes the most productive thing you can do is give yourself space to &lt;em&gt;breathe&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The perfectionism, the anxiety, the feeling that you’re falling behind is something we all carry at a certain degree. It doesn’t mean something’s wrong with you. It means you’re &lt;em&gt;human&lt;/em&gt;, working in a field that moves fast and demands a lot.&lt;/p&gt;
&lt;p&gt;The work will still be there tomorrow. The frameworks will still be there. You don’t have to know everything, be everything, or keep up with everything to be a good engineer.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;A few things to sit with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The code doesn’t have to be perfect to be valuable&lt;/li&gt;
&lt;li&gt;Knowing everything is not the job - solving problems is&lt;/li&gt;
&lt;li&gt;Anxiety about your work is not the same as caring about your work&lt;/li&gt;
&lt;li&gt;It’s okay to say “I don’t know” and “I need help”&lt;/li&gt;
&lt;li&gt;The 10x engineer who knows everything doesn’t exist, and pretending they do hurts everyone&lt;/li&gt;
&lt;li&gt;If rest doesn’t help, the problem might not be the workload — it might be what the work is&lt;/li&gt;
&lt;/ul&gt;</content:encoded></item><item><title><![CDATA[Most AI never ships. Here's what does.]]></title><description><![CDATA[Generative AI is often overhyped, leading to failed implementations and unrealistic expectations. Most AI product pilots fail due to poor planning, wrong automation targets, and lack of human oversight. Companies must approach AI development with skepticism and discipline, ensuring solutions address real user needs while maintaining ethical standards.]]></description><link>http://www.nicotsou.com/ai-that-ships/</link><guid isPermaLink="false">http://www.nicotsou.com/ai-that-ships/</guid><pubDate>Fri, 07 Nov 2025 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;Generative AI is often overhyped, creating unrealistic expectations that frequently lead to failure.&lt;/p&gt;
&lt;p&gt;You see, big tech companies promote their AI models as magical solutions, conveniently ignoring their &lt;a href=&quot;https://fortune.com/2025/08/18/sam-altman-openai-chatgpt5-launch-data-centers-investments/&quot;&gt;flaws&lt;/a&gt;. Social media bombards us with fear-mongering and false promises designed to capture attention. And did you see those &lt;a href=&quot;https://www.theverge.com/ai-artificial-intelligence/812455/ai-industry-earnings-bubble-fomo-hype&quot;&gt;massive investments&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;This often creates a pattern in company meetings: People unfamiliar with the technology get overly enthusiastic about its potential, pushing teams down risky paths. Other folks are way more conservative, slowing down huge transformation opportunities.&lt;/p&gt;
&lt;p&gt;So how do you cut through the noise? How do you build AI products like a boss, without falling into traps?&lt;/p&gt;
&lt;p&gt;Let’s talk about the realities nobody mentions in the keynote presentations. So when business is screaming “Profit! Profit! Profit!” and engineering pushes back with “That will end really badly”, let me help you bridge the gap between hype and reality.&lt;/p&gt;
&lt;p&gt;This article is &lt;em&gt;that&lt;/em&gt; guide—my Holy Grail of AI.&lt;/p&gt;
&lt;p&gt;Amen! 🙏&lt;/p&gt;
&lt;h2&gt;1. Most AI product pilots &lt;em&gt;fail&lt;/em&gt;. And fail hard.&lt;/h2&gt;
&lt;p&gt;Here’s a &lt;a href=&quot;https://fortune.com/2025/08/18/mit-report-95-percent-generative-ai-pilots-at-companies-failing-cfo/&quot;&gt;stat&lt;/a&gt; that should make every product manager sweat: &lt;strong&gt;95% of generative AI pilots at companies are failing.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Not “struggling.” Not “underperforming”.&lt;/p&gt;
&lt;p&gt;And it’s not because the models are broken. It’s because companies have no idea how to actually &lt;em&gt;ship&lt;/em&gt; AI features that users adopt.&lt;/p&gt;
&lt;p&gt;Think about it: your competitors are building AI chatbots, recommendation engines, and automated workflows. Everyone’s racing to slap “AI-powered” on their feature list. But here’s what nobody talks about in those shiny product demos—most of these pilots never make it past the experiment phase.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/27010899d1908050d837b79111967982/302a4/megan.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAACiklEQVR42hXLbVPScAAA8N3xNGHAfxsMcMgGMuDPNtjG83OgEoZ4ZSaeZg/Y2Yl6GiqahFeZZWKUedpd1+s6X9Wbrjd9hfpM2e/9D7k43s9mkgGfJxRwSZJ8JSQKoSDHc0N5ydOopuslOS97eD/jonGaQr2eQc7HcazNzdiR86NuJpvjBSEM3RFJlEUYFiD0uVMC87k9/+fy7PJgeaHET2Sh4LW67HqGHfTw8jBj83vsyNmbTiqdCUlRJeSPisMRwSPzXt47NDcife9t/rw4uDxqtSaDc3lnLkxnwrQUpDkIXU4L53b8z7VqRY5lIlJY4VklyIg+Z4Kn75fDp63G15drn3YWPywV1ydhHFoCLLj6o4VoMgK5YSdy+nrv4/H+9sZqLCIrvEfhmbCfjvnB5s3ojxcrv07af7+dXazeejoTLcm0jUCZQaMksjfyQq0UQZ61m9XxUn2qqshCSPDJvBt66USAKATwwzuF3ycb5+v1tZrSmRET0GEyaAizARDASmJpmUO6e0+UeMyIA4sVp6zATuEAN6Z5SxoCkaW2pxPlsH2+6F2/HU9AChvQGjEUECY9qjGb9Ejv7eHUvQaXyA9BEdgdBqOBAAOjEpmCOO2wVFKBL8+bW9PKRNwV95lJg9ao0xi1aoCqCb0Wed9/12jtjCwsX3+w4hVCOrUqDamFgsNForlIoFxKzozFc5CYzZAhF0bo1XajzobpMJ3KrNcg/X6/ubv/qN193HmVHhunSFxxm5vjLItrvSwdlYM5BitCfS0GJiU7b8OiLiCxAEVVqE6NHPd6q7udraW7zaWH9Y3utUpFZg0FgSIHNKzVlOSoMb95NmMtS5bFElP0kWkWZCFpBjqtRvUPCk+RV6SGIWsAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Megan is one of the funniest deadly AI Sci-fi characters. You definitely don&amp;#39;t want your kid to be next to her.&quot;
        title=&quot;&quot;
        src=&quot;/static/27010899d1908050d837b79111967982/e996b/megan.png&quot;
        srcset=&quot;/static/27010899d1908050d837b79111967982/17741/megan.png 263w,
/static/27010899d1908050d837b79111967982/52211/megan.png 525w,
/static/27010899d1908050d837b79111967982/e996b/megan.png 1050w,
/static/27010899d1908050d837b79111967982/302a4/megan.png 1080w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Megan is one of the most entertaining deadly AI Sci-fi characters. You definitely don’t want your kid to be next to her.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Common reasons why AI product features fail:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The “me too” strategy&lt;/strong&gt; — You’re adding AI because your competitor announced an AI feature last quarter. Not because users are actually asking for it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Wrong automation targets&lt;/strong&gt; — Teams automate complex, opaque workflows where AI errors are invisible until they explode. Instead, start with simple, transparent tasks where hundreds of users can immediately spot when something’s off.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Testing happy paths only&lt;/strong&gt; — Single question-and-answer? Easy to test. Multi-turn conversations that stay coherent and on-track for 10+ exchanges? That’s where most teams discover their AI is actually terrible.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The elephant in the room&lt;/strong&gt; — The research is clear: businesses are investing in the flashy, visible AI features that look good in demos, but they’re missing the real opportunity to save money and improve efficiency by automating their internal workflows.&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;You’re building AI features in the wrong places, for the wrong reasons.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The harsh truth? Most companies treat AI pilots like innovation theater. Build a demo. Show it to executives. Get the green light. Ship to production. Then watch user adoption flatline because nobody actually &lt;em&gt;needed&lt;/em&gt; the feature you built.&lt;/p&gt;
&lt;p&gt;Before you start your next AI pilot, ask yourself: “If this feature disappeared tomorrow, would users complain?” If the answer isn’t a clear yes, you’re about to become part of that 95%.&lt;/p&gt;
&lt;h2&gt;2. AI replaces &lt;em&gt;tasks&lt;/em&gt;, not jobs&lt;/h2&gt;
&lt;p&gt;Companies are doing mass layoffs and blaming it on AI. We see the headlines every week. Although there is &lt;a href=&quot;https://youtu.be/8g5img1hTes?si=192xBKFaJj3yMmuw&quot;&gt;no guarantee&lt;/a&gt; that AI is the one to blame here, we are expecting many jobs to be replaced by AI in the following years. And as this technology evolves, more jobs will be at risk.&lt;/p&gt;
&lt;p&gt;There’s something you don’t read in the news, though: &lt;strong&gt;AI reliably replaces &lt;em&gt;tasks&lt;/em&gt;, not whole jobs&lt;/strong&gt;. And for many workflows, fully removing human judgment is far riskier and more expensive than it looks.&lt;/p&gt;
&lt;p&gt;In the TV series &lt;em&gt;Severance&lt;/em&gt;, workers sort numbers on a screen and delete the ones that feel “wrong”, though no one knows why.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a493b7d3cc428ee0571c76b09f0c365d/d61c2/severance.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAADsklEQVR42i2Ra1CUBRSGv1/ZZKSTyiUukuLCcr8pJBhQcpGLMNBagCE1RmGACiRYZFwznQCFRQgDxM0Ew4CVJIKAKQcKkJhiBCnHAUVEbsuyCws0PX0x/Xjm/Drv+57zChXX6mhs7yCzqIz0s+dQdrbS2dfFXxOj3Hs0Sv+fv9Fzt5ee4V5+He6he+gXuu50r8269nrK6ir4QkReU06hohRh44tbec7clK2Otpg72WNiZ413WCSBkccJO3SCkIMpBEUmERqTQkh0MgGyJAIOJBH8xnFeDpZhZGWFoaUUfQsJm7ZtRwiMjiIwOpLNlhaY2Nsg9dyFq28ET5ns5s2s8yQUXyKxqIqjxZc5kl+JkUOQmMIBQc8ehz1B7NjljL5kB0ZSSwwkEoSiykr6h4Ywd3HEzNEOqYcbbv4y9G39eK+4gqxrSrJrlXxQ+Q3xoriF52vomXiw3tQDZ+9wLFxdRTErjG1seMFaipBZkM/gvfu47/NHz8wEU3trXF4NY4uNHxEZZzhSUkXKl1+TIL9MdN55TF2CWW+8m2fNPHHyCsHU1potYkJDMeF/COGxb5OW+ymxCYnsj44hIuYQwZHvsNk2gJizF0n7qoGMq02cqK4nrqQa052hPG2wUzT3xHt/JC/5+uHs5YOLzyuigQ/CwxkVkyo12QUXiT2cx8joBNe/78DA1p9tnq/jHBKHp1iAa1g8Ut9YNlnu5RkjF9YZuVIhvmJ8Zo7h0XHujj1i5OEEwoOpGaYWNMjeSmXdBinVNQ3MabRMTs/S+/sATT82811HK+3dXdxoaxHNlDT/3Enf4B2m59VMzKrWWFhZYlI9jzAxO8/cko7U0yXsCY/j5q0BlN2DZFUqOXmhlrTiKxw7XU5Sdinp5y7xUbGCLPlV8hXN1Lb1cn9yhttDY8irOhn44zHCpGqBabWG3rFJFF2DpJY2Epp8AZuobCQHTiGRncLuYA5WsnSM9yYiCf+QDQ5RbHGREZD4OW23hxh5MMXRgm9JVsgRnsxrmNEs8mR+gWMl9bgd/gzboDSkwRlYBJzE0D0e9/cLcY7Lw8jrXbYHp7DROZrnrffhFPUJuYofWP77H1pGB4nrzEGYUmuZ1S4xNj1HTXs/V1p6qGq6RXFtKwWKm5ypaCCn/Dofy2vJrb5BZlUjqfkKkgvFd5Qpkdf/xJR4oUqzTN/41P+CGh0z6kWmxXJUi0uodToWlnVoV1ZYXF1dY0lEu7yCRreMdnVFLEEnBtHyWDW/tjun1a3p/AtpKaE0nWIJNAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A typical working day at the office, in the series &amp;quot;Severance&amp;quot;. &quot;
        title=&quot;&quot;
        src=&quot;/static/a493b7d3cc428ee0571c76b09f0c365d/e996b/severance.png&quot;
        srcset=&quot;/static/a493b7d3cc428ee0571c76b09f0c365d/17741/severance.png 263w,
/static/a493b7d3cc428ee0571c76b09f0c365d/52211/severance.png 525w,
/static/a493b7d3cc428ee0571c76b09f0c365d/e996b/severance.png 1050w,
/static/a493b7d3cc428ee0571c76b09f0c365d/087e3/severance.png 1575w,
/static/a493b7d3cc428ee0571c76b09f0c365d/d61c2/severance.png 1800w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;A typical working day at the office, in the series “Severance”.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This is closer to reality than you might think. AI still requires human oversight to ensure important matters aren’t overlooked. The so-called “&lt;strong&gt;human in the loop&lt;/strong&gt;” is not just a catchy phrase to use in your slides, it’s the difference between a working product and a liability.&lt;/p&gt;
&lt;p&gt;Companies that treat AI as “set-and-forget” build up hidden costs that only become obvious when things break:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Data quality gaps&lt;/strong&gt; — garbage in, garbage out, but now at scale&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Eval and monitoring debt&lt;/strong&gt; — no one’s watching what the AI actually does in production&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Loss of user trust&lt;/strong&gt; — one simple anomaly can undo months of credibility, especially when user personal data are involved&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compliance exposure&lt;/strong&gt; — regulators don’t accept “the AI did it” as a defense&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Brittle operations&lt;/strong&gt; — systems that can’t adapt when edge cases inevitably appear&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The companies cutting people to the bone will learn this the hard way when their AI products fail in production.&lt;/p&gt;
&lt;p&gt;You want people in your company who enjoy meaningful work and bring critical thinking to the table. That’s where productivity and innovation begin. You don’t want a skeleton crew babysitting AI outputs all day, catching mistakes after the damage is done.&lt;/p&gt;
&lt;p&gt;Next time you approach AI as a way to replace human labor, just think twice!&lt;/p&gt;
&lt;h2&gt;3. There’s &lt;em&gt;always&lt;/em&gt; a chance to hallucinate&lt;/h2&gt;
&lt;p&gt;LLMs will generate content that’s completely false—and present it to you as fact. No matter how hard you try, &lt;strong&gt;there’s &lt;a href=&quot;https://www.scientificamerican.com/article/chatbot-hallucinations-inevitable/&quot;&gt;always&lt;/a&gt; a possibility AI will &lt;a href=&quot;https://www.theverge.com/2024/5/15/24154808/ai-chatgpt-google-gemini-microsoft-copilot-hallucination-wrong&quot;&gt;hallucinate&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;And believe it or not, that’s a feature, not a bug. Sometimes we &lt;em&gt;do&lt;/em&gt; want a model to think out of the box. To be creative.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/c45d3b33f56649e83e1bdddb85622a2c/8ae78/artificial-intelligence-movie.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 50.19011406844106%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsTAAALEwEAmpwYAAACbUlEQVR42gFiAp39AKmikLK8ssXd1s3r68nm6NDr5+H4+NDZ00ZBNBocGBkbGDMxKczW0dr07c/o4OLu5t/YwNLSvs/azbrRywC2s6G+yb7L4tvI5+rA3ODJ3dbZ8u+8taRcSjMqJx4IDAwAAACIkIva9vDC2s/O5Nzh38Xf4s3Q3ta7x7gAvLqnwc/Dxt/XyOXlv9zlx9XO1uzquq+cnXpaSD0sDRANAAAAfoN/8P//yNfOztjN3OTU2d3K2N7RytHDAM/SxNPk3tbu6tzy8sbk6cTY09bo49Hd2KKPfEs8KyskGgoJBZaZkZuXh7eultHFp7yrib+xkdDRvt/y8QDV1sbZ5d/Z5uHY5eDC29y309TJ4d3d9/bOwK5ONSQlHRUNDg1uYk2Idlh0WzdINh8QCgJWSjbNxa3M3toAy8Kry8i2yc2/z9jPvdPTsMzNzejn1O7sx8Kwak43DAwJAAAAhXBT2b2UcWBCNjAiAAAASkU3pZt+nZmFAMnBq7LCwKPE0rzHwbjCusDRx7zNy3yDfExORmlVPj0wIg8LBaSUe6SdiW1hSTYtHwAAAEVAM5SOdIF9ZAC7spuQu9GI0v+Vq66cnZByfHlDR0BBRDwmJyImIRhYTjocHBR+blammYEyLSEAAQEAAABdX1eYlH13dFoAyL+onLzKeajERE1MMDMuFRkZFBcUOkE6Cw0LEBMRLzQqMToxLygblW5FLiIWAgMEAAAAPUhJiY99cW1UAO76+bfHx1BaVxESDgQGBgQFBBcaGCUpIwcJCSwwJ0VKPEtPQTYqG4xXKl48IQwLCQAAAAAAAD9ANGpqVTQCNNRW1eqwAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Steven Spielberg&amp;#39;s film &amp;quot;Artificial Intelligence&amp;quot; is playing with our emotions asking whether a human android that looks and behaves like a human is a human.&quot;
        title=&quot;&quot;
        src=&quot;/static/c45d3b33f56649e83e1bdddb85622a2c/e996b/artificial-intelligence-movie.png&quot;
        srcset=&quot;/static/c45d3b33f56649e83e1bdddb85622a2c/17741/artificial-intelligence-movie.png 263w,
/static/c45d3b33f56649e83e1bdddb85622a2c/52211/artificial-intelligence-movie.png 525w,
/static/c45d3b33f56649e83e1bdddb85622a2c/e996b/artificial-intelligence-movie.png 1050w,
/static/c45d3b33f56649e83e1bdddb85622a2c/8ae78/artificial-intelligence-movie.png 1096w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Steven Spielberg’s film “Artificial Intelligence” is playing with our emotions asking whether a human android that looks and behaves like a human is a human.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;But here’s the thing: when the stakes are high—financial decisions, medical advice, legal guidance—you can’t afford to guess. That’s when &lt;a href=&quot;https://www.theverge.com/2024/5/15/24154808/ai-chatgpt-google-gemini-microsoft-copilot-hallucination-wrong&quot;&gt;AI slop&lt;/a&gt; can turn from interesting quirks into a disaster.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The risk isn’t that AI will hallucinate. The risk is building systems that assume it won’t.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;On a positive note, there are tools that can help you catch some hallucinations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Models with larger context window are less prone to making mistakes&lt;/li&gt;
&lt;li&gt;Consider adding cloud-based guardrails into your solution&lt;/li&gt;
&lt;li&gt;Another LLM can play the role of the “police” to validate the generated content&lt;/li&gt;
&lt;li&gt;Ground answers: retrieve sources and require citations to exact spans&lt;/li&gt;
&lt;li&gt;Use agentic tools for truth: DB queries, APIs, calculators via function calling&lt;/li&gt;
&lt;li&gt;Constrain outputs: JSON schemas or grammar; answer only if supported&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These are just a few strategies to mitigate risk. The key is to layer multiple safeguards. No single technique eliminates the problem entirely, but combining them significantly improves reliability.&lt;/p&gt;
&lt;h2&gt;4. &lt;em&gt;Any&lt;/em&gt; software is prone to bugs.&lt;/h2&gt;
&lt;p&gt;Remember, AI software is &lt;em&gt;still&lt;/em&gt; software.&lt;/p&gt;
&lt;p&gt;No matter how many tests you’ve written, even if you’re &lt;a href=&quot;https://blog.cleancoder.com/uncle-bob/2017/10/04/CodeIsNotTheAnswer.html&quot;&gt;Uncle Bob himself&lt;/a&gt;, there is no way to end up with a bug-free application. This means even when the AI generates correct output, bugs in your integration code, API calls, or data pipelines can still produce false results.&lt;/p&gt;
&lt;p&gt;But there’s a catch!&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/ad6e084f4309de492bfb04a2e9885b91/21b4d/lisa-simpson.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAACqklEQVR42gGfAmD9AL95aq5zYHo8PrpETWI+gzVNlYgzKIBOQ8SAasBwXniFbkihglSgg0hiUFCQd1OmiT9/Z35OP75+an4+NQDFfW2falZyNDfDSFhzQXeCf1ihcT1nOjXDgGnCcl95hWxJoIBXo4RLYU9SkXVXqYlBgWh+TT+/fmp/PDcAxn5un2pXeTY43lVtoEVHmXhIiXZbakM3wnxnynRigIh1TaqNVqeIQ15NTJZ5UK+NOoFogk4/yoJte0kxAMV9baJsWXg1N9VUa5w+VZx3R4t+YWpEOMt7aq1tV117REOPVV6Lb2RgT2mEbGyVeGqAZ1s2L2xEP3xSLQDEfG2ibVp3NTXOUnCKPkORgDeUhXZvQzuvfFZAcyQtexYofhJXbyjCc2O7bmO/b2iUW1MMFx0AFSA0Kh8Aw3xspG1aczU221Brk1E9O3WTIFLDa0E/k3dGJXgYK4QcKYAZXGwyum9ltHpFyZQvil49CBIfARUgPCYdAMSCcqd1YW83N9lQa6BiTzJoqENibWBHOsp9blx1NixzHzdlJp5zW7Z6ZsObGdWuAcWaH2FHR1hAQThgOwC2XVCkUkN3KyjKSFiYPklUMz9rNClyLCO4TkCqSDmJPS5yNyi0RzqrPzemXRbBnhOWWROEKSmAKyhLOSYAoAkFoA0Hog4InBALmxURohIJnxEPnhEKlBEKlhMLihMPexUUlhMKjA0DjCtSlHahhyVGhxEEfRcOjhUNADVsQTBtQS9uQTBuQC1tPyh0RSxxQid3RCJ4RR59RiBxQShfOxp7R0BwNbRIRrVHeKpHTid5OBZ7SBaESgAYkVQYilAXjlEZj1IZh04Zi08YjlAaiE0cgkkcjU4fc0QgcUIXjk4ndEJ2aB5kfiJfWyYdfkcYh0sbhUo8SvSq8sP2JwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Sometimes in client meetings, I feel like Lisa Simpson, voicing skepticism to an audience that doesn&amp;#39;t want to hear it.&quot;
        title=&quot;&quot;
        src=&quot;/static/ad6e084f4309de492bfb04a2e9885b91/e996b/lisa-simpson.png&quot;
        srcset=&quot;/static/ad6e084f4309de492bfb04a2e9885b91/17741/lisa-simpson.png 263w,
/static/ad6e084f4309de492bfb04a2e9885b91/52211/lisa-simpson.png 525w,
/static/ad6e084f4309de492bfb04a2e9885b91/e996b/lisa-simpson.png 1050w,
/static/ad6e084f4309de492bfb04a2e9885b91/21b4d/lisa-simpson.png 1280w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Sometimes in client meetings, I feel like Lisa Simpson, voicing skepticism to an audience that doesn’t want to hear it.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The key difference between a bug in an AI product versus regular user interfaces: AI fails by producing plausible-but-wrong outputs that flow straight to users or downstream systems. Errors aren’t obvious dialogs; they look like &lt;em&gt;truths&lt;/em&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;UI bugs cost minutes; AI bugs cost &lt;em&gt;trust&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Now is the time to enforce the engineering and architecture principles in your product that you may have overlooked before. You know all those debates about test-driven development and continuous integration? They matter more than ever when AI is in the mix. And the irony is you can use AI to integrate them faster.&lt;/p&gt;
&lt;p&gt;Conduct security assessments and build guardrails to verify AI-generated content before it reaches end users.&lt;/p&gt;
&lt;h2&gt;5. AI is &lt;em&gt;not&lt;/em&gt; inherently ethical.&lt;/h2&gt;
&lt;p&gt;One of the unwritten rules of the &lt;em&gt;real&lt;/em&gt; Agile development manifesto is “bonuses and promotions over ethical considerations”. And “we’ll &lt;a href=&quot;https://www.theverge.com/news/777344/perplexity-lawsuit-encyclopedia-britannica-merriam-webster&quot;&gt;pay&lt;/a&gt; our &lt;a href=&quot;https://edition.cnn.com/2025/08/26/tech/openai-chatgpt-teen-suicide-lawsuit&quot;&gt;lawyers&lt;/a&gt; to &lt;a href=&quot;https://www.bbc.com/news/articles/c5y4jpg922qo&quot;&gt;cover&lt;/a&gt; our &lt;a href=&quot;https://www.reuters.com/technology/artificial-intelligence/multiple-ai-companies-bypassing-web-standard-scrape-publisher-sites-licensing-2024-06-21/&quot;&gt;crap&lt;/a&gt; (sic). Pretty soon we will use AI lawyers so we won’t have to pay those either”.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ethical design is &lt;em&gt;not&lt;/em&gt; a choice&lt;/strong&gt;. Companies that cut corners on ethical AI implementation risk significant damage to their brand reputation and may face substantial financial and legal consequences.&lt;/p&gt;
&lt;p&gt;To put it simply, customers will always &lt;a href=&quot;https://www.cnbc.com/2025/11/05/tesla-musk-germany-sales-down.html&quot;&gt;prefer&lt;/a&gt; companies that are more ethical.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/31382361f75fd93862bb94f0ccb359d6/c1b63/her.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAACqklEQVR42gGfAmD9ACI8USM7UzxKSlJlXkJLTEAzLjM9RiA8VDtLVURMQlRbSVVkaWVyeG1rbFs7R0cyQkoxPkIbI0wTE69hZgAzRVUvQVJYY1ZGTTslIiJYRTRPUVAzTmM0QEkxMjQsMjI0RVZPW1pbUlBeJzc5IjZJLjlFHCFcHSL4pMQAQ1ZdMUNQUWRYPEI+W0dDmH5wbWdVMUNBRE1IRkVGIyYtLThFVF1ehYOBkXyBfnd4kYCCd1FLaDAj3HqRAE9VTUxUVVVMRGxCN5RxYZqDdYRsW01aUDQ9MzdAOT9GQzA4P3J+gObRx+y3qf3KvN/b3I1tZGMvFnI9IwBZW0xtX0qCOC2zRUWnT0qFWEpbVEBGU0QnMDA6QTo8QTs3Pj5camzWp5jtcVj/i3bj3d2YfXNmMxR6Uy8ALj06QSwoji8qu1RWwlpeuV1ZV09FO0RCJC4xKC4tODo1ND1AU2Fk0KKS+H9q/5F95tzbnIR8ZjQUdE4pADM9OD4iIo0xLb1YWr5eYMVmY3FWTSo0MSUtLygyMTxAOGJnXWxuZbmrm/C3ovyrmOvi35aCf1wqEWhDHwA8PzYzJCdrISG4UlK3VFa5TU6qU0pgWEFQTEBwYEZyYkyEfGiSjoGam5Olqp+3urC+t62JcmxiRjBgQyQALi4sHx8mPhQbmjs3ulRXoVpUh0c9k08/PDErXzwsh2A/el5KhGhYjHVol4R8p52YmI6CdmlXSTkmXEo5AA0SHRQWIB0SHmAfH5w1MatWUZJXT5REO4JTQCYXGVY1J4NmTIlyWnxkWXxhXqSWi6GcjYV7ZlxVSkYvJgAUGiUYGyYTGCQXGCE7JiRmMCuRST6CSDs4JSAIBQQAAAA1Jxx3ZUx7alh5amCLfHKAb2B2aVaFfGx4blxB0etXHH80sAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Imagine how different the UX would have been if Theodore (Joaquin Phoenix) wouldn’t know that he’s talking with an AI. Hm, a bad example I guess?&quot;
        title=&quot;&quot;
        src=&quot;/static/31382361f75fd93862bb94f0ccb359d6/e996b/her.png&quot;
        srcset=&quot;/static/31382361f75fd93862bb94f0ccb359d6/17741/her.png 263w,
/static/31382361f75fd93862bb94f0ccb359d6/52211/her.png 525w,
/static/31382361f75fd93862bb94f0ccb359d6/e996b/her.png 1050w,
/static/31382361f75fd93862bb94f0ccb359d6/c1b63/her.png 1200w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Imagine how different the UX would have been if Theodore (Joaquin Phoenix) wouldn’t know that he’s talking with an AI. Hm, a bad example I guess?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;We can’t keep AI responsible for human lives. We can’t let AI make decisions that put people in trouble. The &lt;a href=&quot;https://artificialintelligenceact.eu&quot;&gt;EU AI Act&lt;/a&gt; introduces stricter regulations to prevent misuse of this technology. Because that’s what we like to do in EU, we regulate everything. Several other countries are developing their own rules.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://dora.dev/research/2025/dora-report/&quot;&gt;2025 DORA report&lt;/a&gt; found that 70% of IT leaders cite regulatory compliance as their top GenAI challenge.&lt;/p&gt;
&lt;p&gt;I know this topic is very abstract to many of us. Here are some practical tips:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Always inform your users that you use AI&lt;/strong&gt; — Transparency builds trust. Ultimately, you can provide users with reasoning or confidence scores, especially for high-stakes outcomes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Implement audit logs&lt;/strong&gt; — Record all AI interactions, decisions, and overrides for compliance and retrospective analysis. When something goes wrong, you need a trail to understand what happened.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Embrace human in the loop&lt;/strong&gt; — Before AI commits to consequential decisions (financial transactions, medical advice, legal filings) add confirmation steps. A moment of human review can prevent catastrophic mistakes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Create feedback loops&lt;/strong&gt; — Let users flag bad outputs and show them that their feedback improves the system. This empowers users and helps you catch issues early.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;6. &lt;em&gt;Everyone&lt;/em&gt; is a hacker now&lt;/h2&gt;
&lt;p&gt;Vibe coding was the trend of 2025. Creating applications was never that easy. It’s just you in the safety of your couch, having a team of engineers building whatever comes to your mind, in seconds.&lt;/p&gt;
&lt;p&gt;If it’s so easy to write code, why not vibe code the sh*t out of it?&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;When I was studying at my university, I was always amazed by those assembly simulators and how much the technology had evolved during that time. I felt blessed I was born at that time, and I didn’t have to deal with those low-level coding languages. Back then, I couldn’t even imagine what an iPhone was, or that cars would be able to drive themselves.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/b36f36f13495dc39df3e533fcda5517f/8da59/teens-react-to-old-tech.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAABYlAAAWJQFJUiTwAAADMElEQVR42h2R/1PTBRyHP1dpdpF1KXQrMU4RxADFSdI2iAWTQUQEgoxxJmy0U1HoHDG9kjjFXeKlOL6FTUzxTly4weZAGqhshF/D9Jp9GZNv6+y4s4t/4Olz/PT+7Xk/r3uEryorUCbLWCVZQ9RbUbwZLuHV5SsIC3udJcte4rkXlvL80hcRBAGFMpMff3vK2YlZTrjGKd57iPeUOZTq92MZecjp0d8RTJ9qOKjZwZ7sQgqlacSuXMXqyA1ESGJITJQiT1GwOSmZmLUx1DeYcQWeceHRU7KKylm3/BWU69cilYSjMzbQcWcKoW5nGUc05WSkqkjI/oSsvUYKTA1EvatgS1w89ZV6vjXuo7m2BmefC3fwGV2/zKJIzaRx+za6Tbsxl2ahrdxH660nCGXZ+RTIMtkqS0ep1VFr68d87QZ5++t4WZyer5CLsCrOWlrw3PAxODmP/ckCpoaTHNMWMdL6DddPfc1PXW00e/9E0LyfSe7q9SS8k8RWVS5KfRX51XVszMhhSdhrREeE0yTadXZY6XMOMDz9L/0zC1z23ObcHh3eU0eZdXXjcbs54QsgqBVq1JFxpMdLKS7SIgl/Y9EsQhKJ5O1oIlaspKqsFFe/m6Hhm9z8ewG3fw77wChWk5Ezugp8FjMT9+/i8s8ipClUbEqUUSDaNRn01O34WAwhY8MWOZtS0lgXl0DjETOOK07sTjdj0/Pcv/MA3+AInebjtBYXMGQ2Ebzn5b9/QggJG+V89EE2NSXbOVCUR6NOg0GdTnxsHJI1sZTrd2MXYRd+6KbPPcSvgRChYJC//H7Gr3sZu2TlsecKM5MB5kNzCEnSVL6s2Enr5zqOGjSYtHlUquTIY6NJTkmlz3EVp2hms/WKs138MT5K0P8Iz8Ag/of3mJ6cZEZ8MDcVJDQlVs7L2Mb3B6toqTXQXL2LY7pCPlPJ+HBzIu2WNgYHPfT22rl48RJORx9BeycnD31B82kLY+MjTDy4vXh/9nkJPBYrl6gzaKnZhUU0bDug4ztjJfUlOTQdPoxneHQR1tNjo+tcN47LNm71WDHoq7F0dOAZaqfXmovD3s5563nuXrvK/yRcGjrMi9sNAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Watch how teens react to old technology.&quot;
        title=&quot;&quot;
        src=&quot;/static/b36f36f13495dc39df3e533fcda5517f/e996b/teens-react-to-old-tech.png&quot;
        srcset=&quot;/static/b36f36f13495dc39df3e533fcda5517f/17741/teens-react-to-old-tech.png 263w,
/static/b36f36f13495dc39df3e533fcda5517f/52211/teens-react-to-old-tech.png 525w,
/static/b36f36f13495dc39df3e533fcda5517f/e996b/teens-react-to-old-tech.png 1050w,
/static/b36f36f13495dc39df3e533fcda5517f/087e3/teens-react-to-old-tech.png 1575w,
/static/b36f36f13495dc39df3e533fcda5517f/8079d/teens-react-to-old-tech.png 2100w,
/static/b36f36f13495dc39df3e533fcda5517f/8da59/teens-react-to-old-tech.png 2742w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;&lt;a href=&quot;http://www.youtube.com/watch?v=VhSdhen3FrM&quot;&gt;Watch&lt;/a&gt; how teens react to old technology.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now imagine the kids growing up with AI-powered coding assistants. They’ll have capabilities that took us years to build, available from day one.&lt;/p&gt;
&lt;p&gt;This democratization of development has a darker side. Before, you needed advanced programming and system knowledge to exploit vulnerabilities. Now, anyone with basic language skills can attempt to hack a system by simply describing what they want to accomplish. The barrier to entry for both creation and exploitation has dropped dramatically.&lt;/p&gt;
&lt;p&gt;But the real problems emerge when vibe coders try to scale:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Initial productivity doesn’t scale&lt;/strong&gt; — Vibe coding delivers rare bursts of speed on one-off scripts, but once you need to maintain and evolve code, those gains evaporate quickly&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sites actually get compromised&lt;/strong&gt; — Real-world evidence shows vibe coders hit a wall where their applications get hacked, forcing them to learn security fundamentals they initially skipped&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Context window limitations&lt;/strong&gt; — As apps grow beyond AI context windows, generated code becomes inconsistent in both appearance and functionality&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Poor code quality from training data&lt;/strong&gt; — LLMs are trained on a mix of good, bad, and mediocre code from the internet, producing inconsistent quality without universal standards&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Encourages unnecessary complexity&lt;/strong&gt; — AI often encourages hastiness and over-building rather than preventing unnecessary work&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Maintenance becomes a nightmare&lt;/strong&gt; — Vibe-coded projects become difficult to modify and evolve, especially when code is regenerated from scratch rather than incrementally improved&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So while my developer soul would beg for a true solution that eliminates the need to write code, we’re not there yet. It could be a great prototyping tool, though—and maybe a conversation starter for your next Scrum planning session. 😉&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/e33c1145a5b6028723ad86ac165f7bb7/fa60d/apple-ai-transformation.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 63.87832699619772%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB2ElEQVR42m1Su27iUBB1wSfwAbR8ChUFkqOkSoq1aCCvpUgatBYGJJICJVDBB1AlxYZuU4EQQkQy2WKDKAJpIFKIFTDY5vqS4ztZ1lllZF2fO5wzc2aMtPaFZVntdrvb7RL+dXtbq9Vs2zYM4+fNTb1etx3Hz5foxRjDWS6XJUkKh8N6T2+1WsDxeNw0F/1+PxgMxmKxt9kMNM75J/FqtcJ5cnIqiWg0Gp1OJxAINJtN5NE5FAoVCgVg13X/70zFRqOn4+/H+XwenuHl4hJRWi6XcJ7NZjOZzGw+/6Iz3R3HgWxpWbgTnpsm5WEYD0h8zT91xo0JM5Pn50gkEo1Gp9MpBlEUZUuWXw3DXCxkWQb+ojOgyznplW9KKpWi8pVK5fzszBHr0DTt6OhwLZgbsxKVYeI0TXN7Z2d3d4865HK5ZCKBbQNnNe1gf9+biISiirTxQMUQtE8A9KS2rgjmHR9sjwaxf2HD4UhV1WKxiCEFlQHgj4EpsA4Mv2G6fjF1G4/HyWQCDi3LxrV7d3d1df3yMvV2OZk8Docb5ofYXwnoh6qm02nK3N//1ns92/Gc/3l40HWd8uT5n1h8wPWKsWq1elkqWbYtPi8SLv00GAygxyz878Au5+/cjKOWDn0PqwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Diagram: Apple Intelligence transformation&quot;
        title=&quot;&quot;
        src=&quot;/static/e33c1145a5b6028723ad86ac165f7bb7/e996b/apple-ai-transformation.png&quot;
        srcset=&quot;/static/e33c1145a5b6028723ad86ac165f7bb7/17741/apple-ai-transformation.png 263w,
/static/e33c1145a5b6028723ad86ac165f7bb7/52211/apple-ai-transformation.png 525w,
/static/e33c1145a5b6028723ad86ac165f7bb7/e996b/apple-ai-transformation.png 1050w,
/static/e33c1145a5b6028723ad86ac165f7bb7/087e3/apple-ai-transformation.png 1575w,
/static/e33c1145a5b6028723ad86ac165f7bb7/fa60d/apple-ai-transformation.png 1792w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Finding your balance&lt;/h2&gt;
&lt;p&gt;I know what you’re thinking. In a world where everyone talks about AI revolutionizing everything, how can we be so conservative? It’s true—AI will eventually become what you envision. But current AI iteration is just a stepping stone.&lt;/p&gt;
&lt;p&gt;We are not there yet.&lt;/p&gt;
&lt;p&gt;Every AI success story starts the same way: someone identified a &lt;em&gt;specific&lt;/em&gt; problem, built a &lt;em&gt;constrained&lt;/em&gt; solution, and put &lt;em&gt;guardrails&lt;/em&gt; around it.&lt;/p&gt;
&lt;p&gt;They didn’t try to automate everything at once. They didn’t skip testing because “the model is really good now.” And they definitely didn’t fire half the team and hope AI would pick up the slack. The teams that win with AI are the ones who treat it like any other powerful, yet imperfect, tool.&lt;/p&gt;
&lt;p&gt;Generative AI technology is powerful. It’s transformative. But it’s also immature, unpredictable, and prone to spectacular failures when misused. And that’s why we love it.&lt;/p&gt;
&lt;p&gt;It reminds us of &lt;em&gt;us&lt;/em&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Takeaways (summarized by AI, of course):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Build verification into everything&lt;/strong&gt; — Audit logs, human review checkpoints, confidence thresholds, feedback loops. If you can’t explain how you’ll catch mistakes, you’re not ready to ship.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Solve real problems, not imaginary ones&lt;/strong&gt; — Before you add an AI feature, ask if users are actually asking for it. If the answer is no, you’re about to waste a lot of money.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Automate the boring stuff first&lt;/strong&gt; — Start with simple, transparent tasks where mistakes are obvious. Save the complex workflows for later, once you’ve learned how AI actually behaves in your system.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Critical thinking over massive layoffs&lt;/strong&gt; — The companies that treat AI as a headcount replacement will discover the hard way that babysitting AI outputs is &lt;em&gt;way&lt;/em&gt; harder than doing the work properly from the start.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let me know what your thoughts are on AI. You know where you’ll find me. 😉&lt;/p&gt;
&lt;p&gt;Cover art generated with Google Gemini.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Your LLM is generic until you make it yours]]></title><description><![CDATA[Unlock the secrets of advanced prompting techniques that can dramatically enhance your interactions with AI, transforming simple requests into powerful, precise outputs.]]></description><link>http://www.nicotsou.com/strategies-for-customizing-llms/</link><guid isPermaLink="false">http://www.nicotsou.com/strategies-for-customizing-llms/</guid><pubDate>Wed, 26 Feb 2025 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;The journey of working with artificial intelligence has evolved dramatically over the past few years. What started with simple prompt engineering has grown into a sophisticated ecosystem of techniques and approaches, each building upon the last to create more powerful and personalized AI solutions.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/7a6a40319c35307bcfd5d0da274a7452/fd567/llm-customization-diagram.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 36.12167300380228%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAIAAACHqfpvAAAACXBIWXMAABYlAAAWJQFJUiTwAAAApUlEQVR42qVQ0QrDIBDr///hoE99WG1ndTLvTrG6BV27FfowWBDJaZI77Z5/oNuZ9xRjBLn0PTGDLNamdd0FOeermrCfmImIRUDUNLcUe3frlxlcL+bEDPU4jtbaUoqIQBFCIO+ZKFfg3BPd5llqg4OZmR8VuIsbAlYInxJxRFKBrLcZwc45tDXGtIEboMCby1amlLTWwzAopcAPb/4FaIPp9o94AXpum1WKGrDBAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Diagram: LLM Customization Options&quot;
        title=&quot;&quot;
        src=&quot;/static/7a6a40319c35307bcfd5d0da274a7452/e996b/llm-customization-diagram.png&quot;
        srcset=&quot;/static/7a6a40319c35307bcfd5d0da274a7452/17741/llm-customization-diagram.png 263w,
/static/7a6a40319c35307bcfd5d0da274a7452/52211/llm-customization-diagram.png 525w,
/static/7a6a40319c35307bcfd5d0da274a7452/e996b/llm-customization-diagram.png 1050w,
/static/7a6a40319c35307bcfd5d0da274a7452/087e3/llm-customization-diagram.png 1575w,
/static/7a6a40319c35307bcfd5d0da274a7452/8079d/llm-customization-diagram.png 2100w,
/static/7a6a40319c35307bcfd5d0da274a7452/fd567/llm-customization-diagram.png 4068w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The LLM customization options. Complexity and cost increase as we move on the right.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This diagram illustrates the spectrum of AI model customization options, ranging from basic to advanced approaches. The simplest way is Prompt Engineering, which requires minimal setup. In the middle, you’ll find RAG (which helps AI access specific information) and Fine-Tuning (which adapts AI for special tasks). The most complex option is building your own AI model from scratch, which is costly and requires the most effort, but eventually gives you complete control.&lt;/p&gt;
&lt;p&gt;In this article, I would like to elaborate more on these techniques, and explain under which circumstances you should consider them.&lt;/p&gt;
&lt;h2&gt;Prompt Engineering: The Art of Asking the Right Questions 📇&lt;/h2&gt;
&lt;p&gt;At its core, prompt engineering is the art of effectively communicating with AI models. It’s like learning a new language - one that bridges human intent with machine understanding. By carefully crafting our prompts, we can guide AI models to provide more accurate, relevant, and useful responses.&lt;/p&gt;
&lt;p&gt;If you want to dive deeper, I have a series of articles in this blog:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The Art Of Prompt Engineering&lt;/li&gt;
&lt;li&gt;How to structure your prompts&lt;/li&gt;
&lt;li&gt;Five prompting techniques&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This approach plays a critical role in other customization strategies. In &lt;strong&gt;retrieval-augmented generation (RAG)&lt;/strong&gt;, for example, the prompt must integrate external knowledge retrieved from a database seamlessly. Even in fine-tuned or custom-trained models, prompt engineering remains relevant to optimize the interaction between the user and the model. Its low cost and ease of use make it a powerful starting point for those exploring LLM customization.&lt;/p&gt;
&lt;p&gt;However, as powerful as prompt engineering is, it has its limitations. Models can sometimes struggle with specific domain knowledge or fail to maintain consistency across responses.&lt;/p&gt;
&lt;h2&gt;RAG: Bridging Knowledge Gaps 📚&lt;/h2&gt;
&lt;p&gt;AI models have a distinct knowledge cutoff date and are trained on a generic set of information. When you need to “educate” your model with domain-specific knowledge, RAG is the simplest and most efficient way to accomplish this.&lt;/p&gt;
&lt;p&gt;Retrieval-Augmented Generation (RAG) emerged as a natural evolution to address the limitations of prompt engineering. By combining the language model’s capabilities with specific, retrievable knowledge, RAG creates a more informed and accurate system. Think of it as giving the AI access to a specialized library of information that it can reference while formulating responses.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/feca157828fbccfde5d1880f831bea42/683cf/RAG-diagram.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 32.31939163498099%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAvUlEQVR42nWPQQ6DIBBFvahbOZJbY1x4BU9gL2B03QaU1DaiEUEU+1vaRhf+xTCB9+cP3vZRGIa+7xNCgiBAk2UZLrXWy7Js5/LckeeXKIqSJEnTNI7joiic2RijprfkNFl7Ynayv3fXoMI/z/M4jvzeKqXsccDXvK4rNgRaNxwoAq+Uto/nfxZGgAFwmgyibhokoL9R2nVivwVjrCzLqqo45445mBEuhEAF2vc9vrp/hQFLDcMgpVRKW7u9AJ5jUe6I7jzKAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Diagram: RAG Flow&quot;
        title=&quot;&quot;
        src=&quot;/static/feca157828fbccfde5d1880f831bea42/e996b/RAG-diagram.png&quot;
        srcset=&quot;/static/feca157828fbccfde5d1880f831bea42/17741/RAG-diagram.png 263w,
/static/feca157828fbccfde5d1880f831bea42/52211/RAG-diagram.png 525w,
/static/feca157828fbccfde5d1880f831bea42/e996b/RAG-diagram.png 1050w,
/static/feca157828fbccfde5d1880f831bea42/087e3/RAG-diagram.png 1575w,
/static/feca157828fbccfde5d1880f831bea42/8079d/RAG-diagram.png 2100w,
/static/feca157828fbccfde5d1880f831bea42/683cf/RAG-diagram.png 3322w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;RAG Flow&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;When a user asks a question, the system searches our knowledge base for relevant documents. These documents, along with the user’s original question, are then passed to the LLM. The LLM combines its existing training data with this provided information to generate a response.&lt;/p&gt;
&lt;p&gt;RAG works with various types of databases. You can call a public API and use its response, perform a web search and analyze its results, or use traditional databases. The system works as long as the content fits within the LLM’s context window and you can create a meaningful query from the user’s prompt. Both requirements can be challenging, though. To overcome the limitations of conventional APIs and databases, RAG implementations often combine search indexing and vector databases.&lt;/p&gt;
&lt;p&gt;A vector database is a special type of database that stores high-dimensional vectors representing the semantic meaning of text, images, or other data. These vectors allow for similarity searches, making it possible to find relevant information based on &lt;em&gt;meaning&lt;/em&gt; rather than just &lt;em&gt;keywords&lt;/em&gt;. When integrated with RAG, vector databases enable more nuanced and contextually accurate information retrieval.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/5c03331a02309c1f51888aff41102215/8b95f/vectors.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 49.049429657794676%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAzElEQVR42o2RaQqDMBCFvf+ZFLyHPxUUE/ctq+1XBSlKtQ8D4yRvmSR4/YBzzhjzukVwba3r6r1flqXrOgpU6PxF5ug8z867PM+bpoGrtaaJylXiTNba9H0fRZGUsm1baJDp1HV9jXAmM2ccx0mS4D8MA8npZFkWhiEqD85ETdNUSMkHqqqapon5y7JUankgExVDIcQ4jphTIIdKURSMfUdmm/GwIiFuSil+iU09bHiYed+21hIYCbPB2s9Simv3D+98qOwPvst91wf5DabVSi6VE9yRAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Diagram: Vectors in 3D space&quot;
        title=&quot;&quot;
        src=&quot;/static/5c03331a02309c1f51888aff41102215/e996b/vectors.png&quot;
        srcset=&quot;/static/5c03331a02309c1f51888aff41102215/17741/vectors.png 263w,
/static/5c03331a02309c1f51888aff41102215/52211/vectors.png 525w,
/static/5c03331a02309c1f51888aff41102215/e996b/vectors.png 1050w,
/static/5c03331a02309c1f51888aff41102215/087e3/vectors.png 1575w,
/static/5c03331a02309c1f51888aff41102215/8079d/vectors.png 2100w,
/static/5c03331a02309c1f51888aff41102215/8b95f/vectors.png 2848w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;For example, in a vector space, the words “cat” and “dog” would be closer together than either word is to “apple”. This is because “cat” and “dog” share many semantic properties (they’re both pets, mammals, domestic animals), while “apple” represents a completely different concept (a fruit). When these words are converted to vectors, this semantic similarity is reflected in the mathematical distance between their vector representations.&lt;/p&gt;
&lt;p&gt;Fun fact: LLMs also use dense vector embeddings as part of their neural network architecture to represent and process language. This means that when you input text to an LLM, it first converts your words into these mathematical vectors to process their meaning and relationships before generating a response. This deep understanding of semantic relationships is what enables LLMs to generate contextually appropriate and coherent text. The larger the vector space, the more nuanced these relationships become, enabling the model to capture increasingly subtle distinctions in meaning.&lt;/p&gt;
&lt;p&gt;RAG is particularly useful for companies that need to work with their own private information or specialized knowledge that regular AI models don’t know about. Companies can use RAG to help their AI systems access and understand internal documents, research, technical guides, and industry information while keeping everything secure and accurate. By combining the AI’s general knowledge with a company’s specific information, RAG becomes a valuable tool that helps businesses stay competitive while making the most of AI technology.&lt;/p&gt;
&lt;h2&gt;Fine-tuning: Teaching the Model New Tricks 🎚️&lt;/h2&gt;
&lt;p&gt;While RAG enhances a model’s knowledge, fine-tuning goes a step further by actually modifying the model’s behavior. It’s like sending an experienced professional back to school for specialized training.&lt;/p&gt;
&lt;p&gt;Through fine-tuning, we can adapt existing models to better understand specific contexts, terminology, or ways of communicating. You show it lots of examples of questions and the kinds of answers you want it to give. This teaches the AI to respond the way you want without needing detailed instructions every time you ask it something.&lt;/p&gt;
&lt;p&gt;Fine-tuning enables models to maintain consistent tones, incorporate standard information (like support contact details), and perform specific actions such as referencing documents or databases. However, this approach requires extensive training data and multiple iterations to achieve desired results. This can be challenging, often requiring significant trial and error before reaching optimal performance. Also, not all LLMs support fine-tuning.&lt;/p&gt;
&lt;p&gt;I strongly recommend starting with simpler, more cost-effective solutions before moving to complex approaches. The ideal progression is to first exhaust the possibilities of prompt engineering, and RAG to enhance the model’s knowledge base, or explore building agentic applications that can coordinate multiple AI capabilities. Only after thoroughly testing these approaches should you consider fine-tuning, which demands significantly more resources and technical expertise.&lt;/p&gt;
&lt;p&gt;Despite these challenges, fine-tuning is valuable when you need consistent responses for large-scale operations. It gives you better control over how the LLM behaves than foundation models do, making responses more predictable and reducing unwanted variations.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/73f00353623f912a6801fb151f75078c/91e4c/image.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 57.03422053231939%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAABmElEQVR42o1SO68BQRidX+VPUPkB2yl0lGoKpRCRcHWiFNshK0EQdj3izYrHkrWIZ7zifU9mqlvc655i8s3Md+ac8+2S9ye8Xq/z+bzZbBaLhaIosizX63VJkjKZDHn/A7fb7XQ6bbdb8DudTiqVqlQqH8j3+x2yKJ7PJ8TH43GtVstms8FgkOf5RCLxK/nxeFwuFybbbDYPh4Omaclk0u/3WywWh8MRiUTIH1EhiHW1Wk0mk+VyORgMBEFwu91ms5njOK/X+4PMuplh1Kxot9tril6vF4/HnU6nzWazWq0oCFxdr1c4xDWmwnyCw5hQ63a7kJ3P56PRCIF9Pt8XhcvlIkiCGQ6Hw+l0utvtkO14POI5lrnVahUKhVwuVywWMbDZbCaKYjqdjkaj4JNqtYqOfr8PEbytqio6NIr9fo/A+DDIjENoYIuxBwIBu92OyRFMv9FogI9I0Fco4EKlAAcrtmiDfiwWw63BYNDpdCaTieAon8+LFKVSqVwuSxQosIVb/AzogZrH4wmFQuFw2Gg06vV6juO+AVeqNIVY6ZXpAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;image.png&quot;
        title=&quot;&quot;
        src=&quot;/static/73f00353623f912a6801fb151f75078c/e996b/image.png&quot;
        srcset=&quot;/static/73f00353623f912a6801fb151f75078c/17741/image.png 263w,
/static/73f00353623f912a6801fb151f75078c/52211/image.png 525w,
/static/73f00353623f912a6801fb151f75078c/e996b/image.png 1050w,
/static/73f00353623f912a6801fb151f75078c/087e3/image.png 1575w,
/static/73f00353623f912a6801fb151f75078c/8079d/image.png 2100w,
/static/73f00353623f912a6801fb151f75078c/91e4c/image.png 3848w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Training Your Own LLM: The Ultimate Customization 🐉&lt;/h2&gt;
&lt;p&gt;For organizations with unique needs and substantial resources, training a custom LLM represents the pinnacle of AI customization. This approach offers complete control over the model’s capabilities and biases, but it comes with significant computational and data requirements.&lt;/p&gt;
&lt;p&gt;Training your own LLM requires massive datasets, specialized expertise in machine learning, and substantial computing infrastructure. While this approach provides unparalleled customization potential, it’s typically only feasible for large tech companies or organizations with significant AI research capabilities. The costs can run into millions of dollars, making it crucial to carefully evaluate whether the benefits justify such an investment.&lt;/p&gt;
&lt;h2&gt;That’s all folks! 🙌&lt;/h2&gt;
&lt;p&gt;Thanks for reading! Stay tuned for more content about prompt engineering! I’ll be sharing advanced techniques, real-world examples, and practical strategies to help you master the art of communicating with AI. My newsletter form is just a few DOM elements below. Don’t forget to subscribe to get notified when new articles are published.&lt;/p&gt;
&lt;p&gt;Do you have any other tips for crafting better prompts? I’d love to hear your thoughts and experiences.&lt;/p&gt;
&lt;p&gt;Cover art generated with Adobe Firefly.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The advanced prompting techniques worth learning]]></title><description><![CDATA[Advanced techniques and strategies for effective prompt engineering to optimize interactions with Generative AI.]]></description><link>http://www.nicotsou.com/advanced-prompting-techniques/</link><guid isPermaLink="false">http://www.nicotsou.com/advanced-prompting-techniques/</guid><pubDate>Mon, 10 Feb 2025 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;I’ve curated these advanced prompting techniques and strategies from my personal experience and ongoing learning journey. These techniques will help you elevate your prompts by achieving results that are challenging to get.&lt;/p&gt;
&lt;p&gt;I’ve written everything in plain, simple language, without using complex math or scientific terms. This is just an overview, though. Each technique deserves its own detailed article—which I plan to write for some of them in the future.&lt;/p&gt;
&lt;p&gt;Since prompt engineering is always changing, I’ll keep updating this guide with new tips and best practices as I discover them.&lt;/p&gt;
&lt;p&gt;Let’s see what we’ve got! 🥁&lt;/p&gt;
&lt;h2&gt;Iterative prompt evaluation 🧪&lt;/h2&gt;
&lt;p&gt;As Generative AI transforms industries, effective prompt engineering has become essential for optimizing output &lt;strong&gt;quality&lt;/strong&gt;, &lt;strong&gt;speed&lt;/strong&gt;, and &lt;strong&gt;costs&lt;/strong&gt;. Prompt evaluation is crucial but challenging for organizations developing AI solutions. Many struggle to maintain consistent prompt quality across applications, leading to variable performance and user experiences.&lt;/p&gt;
&lt;p&gt;Just like agile development involves iterative cycles of planning, building, and refining, prompt engineering requires continuous experimentation and improvement. You start with a basic prompt, test it, analyze the results, and then make adjustments based on what you’ve learned. This cycle repeats until you achieve the desired outcome.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/32b713f8473a43349f10a0c501bcd095/a6f4c/iterative-prompt-evaluation.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 42.20532319391636%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAABYlAAAWJQFJUiTwAAAAsUlEQVR42qWRzQqDMBCEff9n66G99eDFYlosUaKQbDQ/7VftQUTqoQOBIZmZHTbF6w8Uv59jjCGElNKB2YrE9EGIMecMGcdp5EyTdS7lTJCIQLZmpKfzxfQ9iqfWIh5bfX/gJOhWK2ze+2tZErQzOc9YkzCDFBK/mt3aqNu2RQfXWtMXwo1zDn/TNFRYCBW2Zt6UUsMw0L+qKmstccaYZVtd10GYD0G5M3ld+/CTlr2+ARf51v3YOz6rAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Diagram: Prompting is an iterative approach.&quot;
        title=&quot;&quot;
        src=&quot;/static/32b713f8473a43349f10a0c501bcd095/e996b/iterative-prompt-evaluation.png&quot;
        srcset=&quot;/static/32b713f8473a43349f10a0c501bcd095/17741/iterative-prompt-evaluation.png 263w,
/static/32b713f8473a43349f10a0c501bcd095/52211/iterative-prompt-evaluation.png 525w,
/static/32b713f8473a43349f10a0c501bcd095/e996b/iterative-prompt-evaluation.png 1050w,
/static/32b713f8473a43349f10a0c501bcd095/087e3/iterative-prompt-evaluation.png 1575w,
/static/32b713f8473a43349f10a0c501bcd095/8079d/iterative-prompt-evaluation.png 2100w,
/static/32b713f8473a43349f10a0c501bcd095/a6f4c/iterative-prompt-evaluation.png 3996w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Prompting is an iterative approach.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The iterative approach involves continuously refining and improving your prompts through multiple attempts. Instead of expecting perfect results on the first try, you experiment with different phrasings and structures to get closer to your desired outcome. This method helps you understand what works best for specific tasks while building an intuition for effective prompting.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;A prompt without evaluation is like a ⛴️ ship without a 🧭 compass.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;To create test cases for prompt evaluation, start by defining the objective — what the prompt is intended to achieve. This can include factors like accuracy, relevance, creativity, clarity, and ethical compliance. Identify scenarios based on real-world use cases, edge cases, negative cases, and exploratory cases. For each test case, document the input prompt, expected output, evaluation criteria, priority, and any additional notes to provide context.&lt;/p&gt;
&lt;p&gt;Evaluation metrics are critical for assessing the quality of outputs. Metrics can include &lt;a href=&quot;https://en.wikipedia.org/wiki/Precision_and_recall&quot;&gt;precision and recall&lt;/a&gt;, &lt;a href=&quot;https://en.wikipedia.org/wiki/BLEU&quot;&gt;BLEU&lt;/a&gt;/&lt;a href=&quot;https://en.wikipedia.org/wiki/ROUGE_(metric)&quot;&gt;ROUGE&lt;/a&gt; scores, user satisfaction ratings, and compliance with ethical guidelines. Test cases should cover a variety of situations, from straightforward tasks like summarization to safety-critical negative cases where the model must avoid harmful outputs. Automating tests through frameworks like &lt;a href=&quot;https://www.langchain.com&quot;&gt;LangChain&lt;/a&gt;, &lt;a href=&quot;https://www.langchain.com/langsmith&quot;&gt;LangSmith&lt;/a&gt;, &lt;a href=&quot;https://docs.confident-ai.com&quot;&gt;DeepEval&lt;/a&gt;, or other prompt evaluation tools can streamline the process, while A/B testing helps identify the best-performing prompts.&lt;/p&gt;
&lt;p&gt;Finally, &lt;a href=&quot;https://en.wikipedia.org/wiki/Human-in-the-loop&quot;&gt;human-in-the-loop&lt;/a&gt; systems and feedback mechanisms are essential for refining prompts. By iteratively testing and analyzing results, you can ensure that prompts are optimized for their intended use, whether for generating content, answering questions, or interacting ethically as a chatbot.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Chain-of-Thought (CoT)&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Solving structured problems step-by-step with prompts like “take your time” and “think step by step how to solve the problem” are related to the &lt;strong&gt;Chain-of-Thought prompting technique&lt;/strong&gt;. It’s a powerful approach to guide the conversation coherently and logically. It involves building gradually on the previous parts of a response to create a flow of thoughts.&lt;/p&gt;
&lt;p&gt;Here’s an example:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; A company has noticed a 10% drop in sales over the last quarter. &lt;strong&gt;&lt;em&gt;Let’s think through the possible reasons step by step&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;The AI will most likely follow a step-by-step reasoning similar to the following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;First, examine if there were any recent changes in product pricing or quality.&lt;/li&gt;
&lt;li&gt;Next, consider external factors, such as competitors’ actions or market conditions.&lt;/li&gt;
&lt;li&gt;Finally, assess if there were any shifts in customer demographics or preferences.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This methodical approach leads to more comprehensive and well-reasoned responses by breaking down complex problems into manageable steps, allowing for thorough analysis at each stage. The result is a more detailed, logical, and carefully considered output that captures nuances that might otherwise be missed with a more direct approach.&lt;/p&gt;
&lt;p&gt;The effectiveness of Chain-of-Thought prompting depends heavily on how well you start the reasoning process. If you begin with incorrect thinking, each following step can make the problem worse, leading to wrong conclusions.&lt;/p&gt;
&lt;p&gt;This method has one main drawback: it can only move forward in one direction. Once you follow a particular path of thinking, you can’t go back to try different approaches or rethink your earlier decisions. This can be especially limiting when dealing with complex problems that might need you to explore multiple solutions at the same time.&lt;/p&gt;
&lt;p&gt;Also, this technique isn’t great for problems that need creative thinking or sudden insights, since it works best with step-by-step logical thinking.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Prompt Chaining&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Prompt chaining is a technique where you break down a complex task into a sequence of smaller, focused prompts that build upon each other. Instead of trying to get the perfect response with a single prompt, you create a chain of prompts where each one refines or builds upon the output of the previous one.&lt;/p&gt;
&lt;p&gt;Think of it like a conversation where each question naturally leads to the next, gradually working towards your goal. This approach gives you better control over the output quality since you can guide the AI more precisely at each step. It’s easier to detect errors and correct them at an early stage, as you can identify exactly where in the chain things might go wrong. You also get more detailed and thorough results since each prompt can focus on a specific aspect of the task.&lt;/p&gt;
&lt;p&gt;We start with a simple request:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;First Prompt:&lt;/strong&gt; Design an event-driven system using AWS Lambda functions. The system should process data uploaded to an S3 bucket, store metadata in DynamoDB, and notify users through SNS. Ensure high availability and scalability.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;Using the previous pair of prompt and prompt response as a “conversation memory”, we prompt the AI to optimize the result:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Second Prompt:&lt;/strong&gt; Optimize the architecture to minimize AWS Lambda invocation costs and storage costs in DynamoDB. Suggest changes or alternatives.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;We try again:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Third prompt:&lt;/strong&gt; Add robust error-handling mechanisms and monitoring tools to the architecture. Ensure failed events are retried or logged for further inspection.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;And again:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Final Prompt:&lt;/strong&gt; Ensure the architecture scales well with increased load and remains easy to maintain. Suggest any further improvements.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;As shown in the example above with the AWS architecture design, each prompt in the chain addresses a different aspect of the solution — from initial design to optimization, error handling, and scalability. This systematic approach helps ensure that all important aspects are covered thoroughly.&lt;/p&gt;
&lt;p&gt;Popular orchestration frameworks like &lt;a href=&quot;https://js.langchain.com/v0.1/docs/modules/chains/popular/api/&quot;&gt;LangChain&lt;/a&gt; provide simple APIs for chaining your prompts.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/8cefd3d9fb01dcb16997c4fb2709e670/351ee/chain-of-thoughts.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 61.21673003802282%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAAsTAAALEwEAmpwYAAAC4klEQVR42mNwsHfw8vQKDw1NSUooLspta66fO2f65o1rTh4/dO3qhXv3bj599vDZs0fPXzwGIiDj6bPHz54/AaIXr54xODk4+Xh7R0VGZKSlVpUV9Xe3LV00b8fWjatWrViwcOGWrVtOnjrx7PnjFy9BCKj/KVD/c5D+V69fMLg4ufj7+cfGROdkZdTXVEzq6169fPGM6dNa29uLiksmTJp46vRJIHr58inQ2mfPHz55+uDJU5D9z188ZXBzcQsKDEqIj8vPy2lqrJ0+ZcLyxQuqqquLSksjo6N8/QPaOzt379n19CnQ8SCdDx/de/Dw3oMHd4GIwdPTKzQ0NDkpsagwv62lYcbUietWr0hITHRycZs4ceLq1SuTU9LaOjpfvHz6+Mn9h4/u3rlz88aNK5cvnT9z+iSDn59/VGRUWmpqaXFRR2vTzGmTgZp9ff2ioqNv37p2/fqljs5OoOMfPgRadefu3RtXrlw4cvjg1k2bVi5fwRAeHpGUlAh0c211ZW93x/KlCxvrapSUlOITkioqy/ILC9IysiZNngQ06NrVi6dOHNm0Yd3cOXMn9E1oamxlCAkOiY+Ly8nOrKoo6+5oXbxwblNDbWVF2bSpUwT4+UJCQnp6evv6+3bv2r5pw5pFC+b29XQ31DcUF5elpWcx6OnomRmb2tnYent5xURHZWdlFhcV1tfVd3a083Cy2dpYTpkypauza9LEiT1dXc2NDaXFxZkZmYnxiVERsQwSYhLSktIKcvJqKqp6unrmZuYO9o6+Pv6BAYESYiK6uropKWmxMfGREVHBQSEebh7ARGVlYWVqbGaob8QgIS4hKS4hJSElKyMjLy+vrKSspamlp6dnbGQsISqirKhoa2NnbmZhYGCoqamtqqKqpKgkL6cgIy0LtJJBSFBQSFBISlJcTk5aQlxURFhQREgASIqKCouJisjIyCgpKigpKgJNkZeTk5OVlZaSkpKQEBcDSooAANHwVstLfXtCAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Chain of thoughts - AI Generated image&quot;
        title=&quot;&quot;
        src=&quot;/static/8cefd3d9fb01dcb16997c4fb2709e670/e996b/chain-of-thoughts.png&quot;
        srcset=&quot;/static/8cefd3d9fb01dcb16997c4fb2709e670/17741/chain-of-thoughts.png 263w,
/static/8cefd3d9fb01dcb16997c4fb2709e670/52211/chain-of-thoughts.png 525w,
/static/8cefd3d9fb01dcb16997c4fb2709e670/e996b/chain-of-thoughts.png 1050w,
/static/8cefd3d9fb01dcb16997c4fb2709e670/087e3/chain-of-thoughts.png 1575w,
/static/8cefd3d9fb01dcb16997c4fb2709e670/8079d/chain-of-thoughts.png 2100w,
/static/8cefd3d9fb01dcb16997c4fb2709e670/351ee/chain-of-thoughts.png 3350w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Reasoning &amp;#x26; Acting (ReAct)&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;When working with dynamic tasks that require adaptive reasoning and actions based on feedback, the Reasoning &amp;#x26; Acting prompting technique delivers excellent results. It enables intelligent problem-solving through a continuous cycle of observation, analysis, and response.&lt;/p&gt;
&lt;p&gt;This method works best when you need to try things out in the real world and learn from the results. It’s particularly useful for solving tricky problems where you need to adjust your approach based on new information and changing situations.&lt;/p&gt;
&lt;p&gt;ReAct is inspired by the fundamental synergies between “acting” and “reasoning” which allow humans to learn new tasks and make decisions through a natural process of observation, reflection, and adaptive response.&lt;/p&gt;
&lt;p&gt;Here’s an example:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;First Prompt:&lt;/strong&gt; We are deciding on a cloud architecture for our new web application. Should we use &lt;em&gt;serverless&lt;/em&gt;, &lt;em&gt;containers&lt;/em&gt;, or a &lt;em&gt;VM-based approach&lt;/em&gt;? The decision needs to account for &lt;em&gt;scalability&lt;/em&gt;, &lt;em&gt;cost&lt;/em&gt;, and &lt;em&gt;development speed&lt;/em&gt;. &lt;strong&gt;&lt;em&gt;What are the trade-offs of each and what’s the next logical step?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;Similar to CoT, just by adding this key phrase at the end, we expect the AI to come up with two components: Reasoning and Action.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reasoning:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Serverless is ideal for scalability and low initial cost but may have higher long-term costs for high workloads. Development is quick due to the reduced infrastructure management.&lt;/li&gt;
&lt;li&gt;Containers provide better control and portability with a balance of cost and scalability but require more setup and maintenance.&lt;/li&gt;
&lt;li&gt;VMs offer the most control and flexibility but can be costly and require significant maintenance, slowing development speed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Action:&lt;/strong&gt; How do serverless, containers, and VMs compare in terms of scalability?&lt;/p&gt;
&lt;p&gt;Following the ReAct approach, we now use the AI’s suggested action as our next prompt:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Second Prompt:&lt;/strong&gt; How do serverless, containers, and VMs compare in terms of scalability?&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;This iterative process allows us to make decisions based on concrete observations rather than assumptions, adjust our approach as we gather more information, and break down complex technical decisions into manageable steps.&lt;/p&gt;
&lt;p&gt;The power of ReAct lies in its ability to combine analytical thinking with practical action steps, leading to more thorough and well-considered solutions.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Tree of thoughts&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;The Tree of Thoughts technique is a structured approach for tackling complex problems that have multiple possible solutions. It works by breaking down the decision-making process into four key steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;first identifying all possible approaches to the problem,&lt;/li&gt;
&lt;li&gt;then evaluating and scoring each approach for feasibility,&lt;/li&gt;
&lt;li&gt;followed by a detailed exploration of the most promising solution,&lt;/li&gt;
&lt;li&gt;and finally concluding with a selection of the best option based on the evaluation.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here’s an example:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;First Prompt:&lt;/strong&gt; &lt;em&gt;Let’s explore several potential strategies&lt;/em&gt; for migrating the existing relational database to Azure Cosmos DB, examining each approach from multiple perspectives &lt;em&gt;before choosing the best option&lt;/em&gt;. &lt;strong&gt;&lt;em&gt;Consider three main migration strategies&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;The AI will generate multiple migration strategies:&lt;/p&gt;
&lt;details&gt;
&lt;summary&gt;Strategy A&lt;/summary&gt;
&lt;p&gt;Lift and Shift Using Azure Data Migration Service (DMS)&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Thought 1:&lt;/strong&gt; What are the advantages of using Azure DMS for a straightforward lift-and-shift migration? (e.g., minimal code changes, faster migration setup)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Thought 2:&lt;/strong&gt; What are the limitations of a lift-and-shift approach in this context? (e.g., potential data model mismatches between relational and NoSQL, need for reindexing)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Thought 3:&lt;/strong&gt; What additional optimizations would be necessary post-migration, such as adjusting indexing or partitioning to align with Cosmos DB’s architecture?&lt;/li&gt;
&lt;/ul&gt;
&lt;/details&gt;
&lt;details&gt;
&lt;summary&gt;Strategy B&lt;/summary&gt;
&lt;p&gt;Data Model Transformation with Schema Redesign&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Thought 1:&lt;/strong&gt; How could a schema redesign help optimize the data model for Cosmos DB? (e.g., moving from relational to document-based modeling, denormalization for performance)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Thought 2:&lt;/strong&gt; What challenges might arise when transforming the schema for Cosmos DB, especially regarding data consistency or query patterns? (e.g., handling joins, adapting to a non-relational mindset)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Thought 3:&lt;/strong&gt; What tools or strategies could assist in the schema redesign process, such as Cosmos DB’s Data Migration tool, or hybrid models that gradually phase in new data structures?&lt;/li&gt;
&lt;/ul&gt;
&lt;/details&gt;
&lt;details&gt;
&lt;summary&gt;Strategy C&lt;/summary&gt;
&lt;p&gt;Hybrid Migration with Incremental Data Sync&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Thought 1:&lt;/strong&gt; What benefits could a hybrid migration provide by syncing new data to Cosmos DB incrementally? (e.g., reduced downtime, ability to test Cosmos DB performance on real data)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Thought 2:&lt;/strong&gt; What complexities could arise with maintaining data consistency across both databases during migration? (e.g., handling real-time syncing, conflict resolution)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Thought 3:&lt;/strong&gt; How might we design an efficient syncing and switchover process, such as using Azure Functions for event-driven updates or a staged migration approach?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now, we prompt the AI to evaluate the generated approaches and to select the one that best aligns with the target that we have:&lt;/p&gt;
&lt;/details&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Second Prompt:&lt;/strong&gt; Once we examine these migration strategies, considering ease of implementation, cost, performance, and long-term flexibility, we’ll &lt;strong&gt;&lt;em&gt;select the approach that best aligns&lt;/em&gt;&lt;/strong&gt; with the application’s scalability and operational requirements.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;This method is particularly useful when dealing with ambiguous problems and trade-offs that require careful consideration of multiple alternatives.&lt;/p&gt;
&lt;h2&gt;X-Shot Prompting&lt;/h2&gt;
&lt;p&gt;In prompt engineering, “shot” refers to the number of examples provided to guide the AI model. When we talk about “X-shot” prompting, we’re describing how many examples we’re giving the model to help it understand the pattern we want it to follow.&lt;/p&gt;
&lt;p&gt;By default, when no examples are provided (zero-shot), the model relies solely on its training data and the task description in the prompt to generate responses. As we increase the number of examples - whether it’s one-shot (single example), few-shot (several examples), or many-shot (numerous examples) - we give the model more context to understand the specific pattern or format we’re looking for. This approach helps the model better align its responses with our expectations and requirements.&lt;/p&gt;
&lt;p&gt;Here’s an example:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; &lt;em&gt;Classify the sentiment&lt;/em&gt; of a customer message. Focus on identifying the sentiment accurately based on context.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;For example&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“Your team helped me so quickly with my claim!” Meaning: Positive&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“I am beyond frustrated with the wait times.” Meaning: Negative&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“Can someone explain this to me?” Meaning: Neutral&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“I’m so grateful for the help I received!” Meaning: Positive&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“This process is more complicated than I expected.” Meaning: Ambiguous&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Sentence to classify&lt;/em&gt;: “{MESSAGE}”&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;In this example, we’re showing how to classify sentiment in customer messages. The prompt starts by clearly stating the task “Classify the sentiment” and then provides 5 examples that demonstrate different types of sentiment. These examples help the AI understand the nuanced differences between sentiments and how to categorize them appropriately. Notice how each example includes both the message and its classification, creating a clear pattern for the AI to follow.&lt;/p&gt;
&lt;p&gt;Different AI models have varying abilities to learn from examples. Some advanced AI models can understand and copy patterns after seeing just a few examples. Other AI models need more examples to do the same task well.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;That’s all folks! 🙌&lt;/h2&gt;
&lt;p&gt;Thanks for reading! Stay tuned for more content about prompt engineering! I’ll be sharing advanced techniques, real-world examples, and practical strategies to help you master the art of communicating with AI. My newsletter form is just a few DOM elements below. Don’t forget to subscribe to get notified when new articles are published.&lt;/p&gt;
&lt;p&gt;Do you have any other tips for crafting better prompts? I’d love to hear your thoughts and experiences.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Cover art generated with Adobe Firefly&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Five prompt tweaks that actually make a difference]]></title><description><![CDATA[Five essential tips for improving interactions with AI models through effective prompt engineering.]]></description><link>http://www.nicotsou.com/five-pro-tips-for-better-prompts/</link><guid isPermaLink="false">http://www.nicotsou.com/five-pro-tips-for-better-prompts/</guid><pubDate>Tue, 17 Dec 2024 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;I was looking forward to posting these prompt engineering tips below. Whether you’re a developer, content creator, or just someone curious about AI, this article will give you five simple ideas that will vastly improve the way you interact with AI models, and save you some bucks.&lt;/p&gt;
&lt;p&gt;These insights come from extensive experimentation and real-world applications. They aren’t just theory — they’re proven techniques that will make your AI interactions more efficient, accurate, and productive.&lt;/p&gt;
&lt;p&gt;Camera? Lights? Let’s go! 🎬&lt;/p&gt;
&lt;h2&gt;Tip #1 — &lt;strong&gt;Allow the model to &lt;em&gt;fail&lt;/em&gt;.&lt;/strong&gt; 🤷‍♀️&lt;/h2&gt;
&lt;p&gt;The fundamental principle of giving AI models permission to acknowledge their limitations. The “license to kill” — Ahm, I’m sorry, I mean… the “license to say I don’t know” — is one of your weapons against hallucinations.&lt;/p&gt;
&lt;p&gt;By explicitly allowing the model to admit when it doesn’t know something or is uncertain, we create a more reliable and trustworthy interaction. This approach not only reduces the likelihood of incorrect responses but also helps establish a more honest dialogue between user and AI.&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; Please answer the following question as accurately as possible. If you don’t know the answer or if the information is uncertain, &lt;strong&gt;&lt;em&gt;it’s okay to say, “I don’t know.”&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;info&gt;{INPUT DATA}&lt;/info&gt;&lt;/p&gt;
&lt;p&gt;&lt;question&gt;{QUESTION}&lt;/question&gt;&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;This approach helps create a more natural interaction where the AI can be upfront about its limitations.&lt;/p&gt;
&lt;h2&gt;Tip #2 — &lt;strong&gt;Use &lt;em&gt;leading&lt;/em&gt; words.&lt;/strong&gt; 🚏&lt;/h2&gt;
&lt;p&gt;Leading words are specific terms or phrases that guide the AI model’s response in a particular direction. They act as signposts, helping to frame the context and shape the output in the desired way.&lt;/p&gt;
&lt;p&gt;When strategically placing an incomplete sentence or structural element at the end of your prompts, these leading words serve as powerful directional cues that naturally guide the model toward completing the thought in a specific way. This method works by using the AI’s natural tendency to complete thoughts in a logical way. When you leave an unfinished thought or pattern, the AI will try to complete it in a way that makes sense.&lt;/p&gt;
&lt;p&gt;I mean, if we oversimplify it, at its core, Generative AI is essentially a sophisticated text prediction algorithm, right?&lt;/p&gt;
&lt;p&gt;Here’s an example:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; Generate an SQL query that retrieves the names and ages of users over 18 from the ‘users’ table.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;SELECT name, age&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;By providing &lt;code class=&quot;language-text&quot;&gt;SELECT name, age&lt;/code&gt; as a leading phrase, we’re essentially giving the model a clear starting point. The AI will naturally continue with the &lt;code class=&quot;language-text&quot;&gt;FROM&lt;/code&gt; clause and &lt;code class=&quot;language-text&quot;&gt;WHERE&lt;/code&gt; conditions to complete a valid SQL query. This technique works particularly well for structured outputs like code, queries, or any format with a well-defined syntax.&lt;/p&gt;
&lt;p&gt;The beauty of using leading words is that they subtly guide the AI without being overly prescriptive, allowing for both accuracy and flexibility in the response.&lt;/p&gt;
&lt;h2&gt;Tip #3 — &lt;strong&gt;Ask the model to evaluate a prompt and suggest changes 👩‍💻&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;We spend time crafting the perfect prompt to properly instruct the AI. But what if we ask the AI itself to help us perfect our prompts?&lt;/p&gt;
&lt;p&gt;When a colleague mentioned trying this technique and getting the results we wanted, I was eager to try it myself. I’ve been amazed by how effectively this works. I shouldn’t be surprised. The truth is, AI knows how to talk to… AI.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/8445fb7eb95bcd689f50749cd02c3f9a/1321a/ai-prompt-evaluation.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.14448669201519%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABYlAAAWJQFJUiTwAAABhklEQVR42p2TzW6CQBSFeYe+h+3Op2rTunJngmGhe9+r27ow1qIi/iI4goDT+02rgZK02EkOzAyXc8+5d8b6eHtt+r7bC4LQPhwO3ePxWEEQBN0wDLtJklxR/C7/2Z7n9UajUdPyPP9Ff41c/39kPNx3t2X5vv/8vZkI0p9YrVbpdDpNZ7PZFaw3m00xLoYALghLCs/nsxYber/f691ud8V2uy3NlVIVhWK7VSFkSL30eDwmQEt9KoiiSEsd6xOSXaxhQS+XS0PMerFYGLDGwZ+E2GWgYDKZGDIsooYyxHFs3iBN0/oKCUaR67pGjTTAYL1em3JgG9LahHmeG7uQFTGfz7WcSeMAtbUJsywztYLgUjdAdyFD5U2EBKMQi5BcLDNnj4bc1GWag0qs33JTfj021AoVNKiI0+lk3qgkaYVQjscjQdK1SKDErpIkSuqnhFTJ1StBbCupoRoOh2YuSRX/klwuw5PVbrfvOp3Og6BxQb/fN3Acp1HcB+yBwWBQ+m7b9j1cn+aERapQ/oJ0AAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;AI Prompt evaluation&quot;
        title=&quot;&quot;
        src=&quot;/static/8445fb7eb95bcd689f50749cd02c3f9a/e996b/ai-prompt-evaluation.png&quot;
        srcset=&quot;/static/8445fb7eb95bcd689f50749cd02c3f9a/17741/ai-prompt-evaluation.png 263w,
/static/8445fb7eb95bcd689f50749cd02c3f9a/52211/ai-prompt-evaluation.png 525w,
/static/8445fb7eb95bcd689f50749cd02c3f9a/e996b/ai-prompt-evaluation.png 1050w,
/static/8445fb7eb95bcd689f50749cd02c3f9a/087e3/ai-prompt-evaluation.png 1575w,
/static/8445fb7eb95bcd689f50749cd02c3f9a/8079d/ai-prompt-evaluation.png 2100w,
/static/8445fb7eb95bcd689f50749cd02c3f9a/1321a/ai-prompt-evaluation.png 3086w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Asking the AI to improve my prompt.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;By asking the model to analyze and suggest improvements to your prompts, you can leverage its deep understanding of effective prompt structures and patterns. The model can help refine your prompt’s structure and relevance, fix language issues like typos and grammar, and identify potential edge cases you might have overlooked.&lt;/p&gt;
&lt;p&gt;This collaborative approach between human and AI can lead to more precise, efficient, and effective prompts that better achieve your desired outcomes.&lt;/p&gt;
&lt;h2&gt;Tip #4 — &lt;strong&gt;Use a mix of &lt;em&gt;hard&lt;/em&gt; and &lt;em&gt;soft&lt;/em&gt; prompts 🍬&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;There are two main types of prompts you can use when talking to AI: hard ones and soft ones.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hard prompts&lt;/strong&gt; give clear, specific instructions that help you get exact answers. They will work great when you need precise information or are working with technical stuff.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Soft prompts&lt;/strong&gt;, on the other hand, give the AI more room to think creatively and suggest different solutions. They’re useful when you want the AI to explore options or think outside the box.&lt;/p&gt;
&lt;p&gt;Combining both hard and soft prompts creates a powerful synergy in your interactions with AI. You can be precise when needed while still letting the AI suggest new and interesting solutions.&lt;/p&gt;
&lt;p&gt;First, provide a hard prompt outlining the specific steps, inputs, desired output, and performance constraints&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Hard Prompt:&lt;/strong&gt; Design a rate limiter algorithm for a RESTful API with a limit of 100 requests per minute per user. Consider tracking request count, managing expiration, and enforcing the limit.&lt;/p&gt;
&lt;/aside&gt;
&lt;details&gt;
&lt;summary&gt;Produced code&lt;/summary&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;python&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-python line-numbers&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; time &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; time
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; collections &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; defaultdict

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;RateLimiter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;buckets &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; defaultdict&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;last_refill_time&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; time&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;max_tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;refill_rate &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;60&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# tokens per second&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;is_allowed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; user_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        now &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; time&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        bucket &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;buckets&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

        &lt;span class=&quot;token comment&quot;&gt;# Refill tokens&lt;/span&gt;
        elapsed_time &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; now &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;last_refill_time&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        refill_tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; elapsed_time &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;refill_rate
        bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;max_tokens&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; refill_tokens&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;last_refill_time&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; now

        &lt;span class=&quot;token comment&quot;&gt;# Check if a token is available&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Example Usage&lt;/span&gt;
rate_limiter &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; RateLimiter&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;api_request&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    allowed&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; remaining_tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; rate_limiter&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;is_allowed&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; allowed&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Request allowed&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;remaining_tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; remaining_tokens&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;429&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Too Many Requests&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;remaining_tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; remaining_tokens&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Simulate API requests&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;api_request&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;user_1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;api_request&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;user_1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/details&gt;
&lt;p&gt;Once the initial solution is developed, you could offer a soft prompt encouraging LLM to consider potential improvements, optimizations, or alternative approaches.&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Soft Prompt:&lt;/strong&gt; What are some creative ways to optimize or enhance the rate limiter algorithm to handle edge cases or scale with increased traffic?&lt;/p&gt;
&lt;/aside&gt;
&lt;details&gt;
&lt;summary&gt;AI Recommendations&lt;/summary&gt;
  Here are the recommendations made earlier for improving the rate limiter implementation:
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Optimize Data Storage&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Use Bloom filters for preliminary checks.&lt;/li&gt;
&lt;li&gt;Employ sharded storage for distributed caching.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enhance Token Refill Strategy&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Implement lazy refills to avoid unnecessary computations.&lt;/li&gt;
&lt;li&gt;Use sliding window rate limiting to smooth out request spikes.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Handle Edge Cases&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Combine token bucket with leaky bucket for burst handling.&lt;/li&gt;
&lt;li&gt;Include a &lt;code class=&quot;language-text&quot;&gt;Retry-After&lt;/code&gt; header in responses.&lt;/li&gt;
&lt;li&gt;Adapt to user behavior dynamically for fair limits.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enhance Scalability&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Introduce hierarchical rate limiting (user, IP, API key levels).&lt;/li&gt;
&lt;li&gt;Offload rate limiting to an edge service (e.g., CDN or API gateway).&lt;/li&gt;
&lt;li&gt;Prefetch user buckets to reduce latency during peak usage.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Support Multi-Tenancy&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Enable dynamic rate limits based on user tiers (e.g., free vs. premium users).&lt;/li&gt;
&lt;li&gt;Differentiate between global and local rate limits for better efficiency.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimize Distributed Systems&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Use Redis Lua scripts for atomic token updates.&lt;/li&gt;
&lt;li&gt;Preallocate tokens across distributed nodes.&lt;/li&gt;
&lt;li&gt;Accept eventual consistency for slight discrepancies in distributed setups.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Real-Time Monitoring and Adjustments&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Implement adaptive limits based on system health metrics.&lt;/li&gt;
&lt;li&gt;Log request details for analysis and anomaly detection.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;User-Friendly Enhancements&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Provide a quota display API for users to check their limits.&lt;/li&gt;
&lt;li&gt;Allow grace periods for minor over-limit violations.&lt;/li&gt;
&lt;li&gt;Escalate penalties for repeated violations (e.g., temporary blocks).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Leverage Machine Learning&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Use predictive rate limiting to anticipate traffic patterns.&lt;/li&gt;
&lt;li&gt;Detect abuse or anomalies with AI-powered fraud detection.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fail-Safe Mechanisms&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Use fallback policies to allow limited requests during backend failures.&lt;/li&gt;
&lt;li&gt;Prioritize critical users to ensure system availability during high traffic.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/details&gt;
&lt;p&gt;Finally, once we get a clear list of enhancements, we ask the AI to refactor the previous example:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Hard Prompt:&lt;/strong&gt; Refactor the code above, by applying these recommendations&lt;/p&gt;
&lt;/aside&gt;
&lt;details&gt;
&lt;summary&gt;Revised code&lt;/summary&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;python&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-python line-numbers&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; time &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; time
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; redis
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; math
&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; flask &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Flask&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; request&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; jsonify

app &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Flask&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;__name__&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Initialize Redis connection&lt;/span&gt;
redis_client &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; redis&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;StrictRedis&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;host&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;localhost&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; port&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;6379&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; db&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Constants&lt;/span&gt;
MAX_TOKENS &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;
REFILL_RATE &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;60&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# tokens per second&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;get_current_bucket&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Fetch or initialize a user&apos;s bucket.&quot;&quot;&quot;&lt;/span&gt;
    bucket &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; redis_client&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;hgetall&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# Initialize a new bucket with full tokens&lt;/span&gt;
        redis_client&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;hmset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; MAX_TOKENS&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;last_refill_time&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; time&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        redis_client&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;expire&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3600&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Expire bucket after 1 hour of inactivity&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; MAX_TOKENS&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;last_refill_time&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; time&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;k&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;decode&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;v&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; k&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;save_bucket&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Save the updated bucket state.&quot;&quot;&quot;&lt;/span&gt;
    redis_client&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;hmset&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;last_refill_time&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;last_refill_time&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    redis_client&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;expire&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3600&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Extend TTL on activity&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;refill_tokens&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bucket&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Refill tokens based on elapsed time.&quot;&quot;&quot;&lt;/span&gt;
    now &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; time&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    elapsed_time &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; now &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;last_refill_time&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    new_tokens &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; elapsed_time &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; REFILL_RATE
    bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MAX_TOKENS&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; new_tokens&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;last_refill_time&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; now
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; bucket

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;is_request_allowed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Check if a user request is allowed.&quot;&quot;&quot;&lt;/span&gt;
    bucket &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; get_current_bucket&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    bucket &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; refill_tokens&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;bucket&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
        save_bucket&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        retry_after &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ceil&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; REFILL_RATE&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; bucket&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; retry_after

&lt;span class=&quot;token decorator annotation punctuation&quot;&gt;@app&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;route&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;/api/resource&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; methods&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;GET&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;api_resource&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    user_id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; request&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;headers&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;get&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;X-User-ID&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Simulate user identification&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; user_id&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; jsonify&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;User ID missing&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;400&lt;/span&gt;

    allowed&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; remaining_tokens&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; retry_after &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; is_request_allowed&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;user_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; allowed&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; jsonify&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Request allowed&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;remaining_tokens&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; remaining_tokens&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;limit&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; MAX_TOKENS&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;reset_in&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;60&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Fixed time window for simplicity&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        headers &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;X-RateLimit-Limit&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; MAX_TOKENS&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;X-RateLimit-Remaining&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; remaining_tokens&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;Retry-After&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; retry_after
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; jsonify&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;status&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;429&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Too Many Requests&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token string&quot;&gt;&quot;retry_after&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; retry_after
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;429&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; headers

&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; __name__ &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    app&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;run&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;debug&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/details&gt;
&lt;p&gt;This enhanced version of the rate limiter showcases several key improvements over the original implementation. It utilizes Redis for distributed caching, implements proper HTTP status codes and headers, and includes user-friendly features like remaining token counts and retry-after intervals. The code structure is also more modular and maintainable, making it easier to extend with additional features in the future.&lt;/p&gt;
&lt;p&gt;I know this may not be the most production-ready code out there, but I want to remind you that we only asked 3 simple questions.&lt;/p&gt;
&lt;h2&gt;Tip #5 — Always keep the &lt;em&gt;dynamic&lt;/em&gt; part of a prompt at the end 💾&lt;/h2&gt;
&lt;p&gt;When crafting prompts, it’s highly beneficial to structure them with static, repetitive content positioned at the beginning, followed by the dynamic, variable content at the end. This strategic arrangement takes advantage of &lt;a href=&quot;https://platform.openai.com/docs/guides/prompt-caching&quot;&gt;prompt&lt;/a&gt; &lt;a href=&quot;https://www.anthropic.com/news/prompt-caching&quot;&gt;caching&lt;/a&gt; mechanisms, which can significantly improve response times and efficiency.&lt;/p&gt;
&lt;p&gt;The caching system is particularly effective for longer prompts, specifically those that exceed 1024 tokens in length, where the system automatically implements caching functionality to optimize performance.&lt;/p&gt;
&lt;p&gt;The following image from &lt;a href=&quot;https://platform.openai.com/docs/guides/prompt-caching&quot;&gt;OpenAI’s official documentation&lt;/a&gt; explains the concept in detail:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/5bd316e089767d73809c28b93ba27e01/b3154/prompt-caching.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 52.09125475285171%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAACX0lEQVR42mNgYGBgBmIWELa3twdjGB+KQfJMDBCQDcQHgbxNfKJ825QVlbeJi4tvA4ptBuJ9QFzHQCRgBhOczI3CGsLv+OX5b4jqij4QVhW5J6Ipcl9AReC2oKrQKw4hjhkgdVpAbM3JyWmhrKxsKS8vbyklJWUJFLMAYhsgloOZyivDW6zsq3JTykzqsFqw+lkg+4yyt/JpGRuZoypBqjeEdEQ6GVhZWTcDDfsmKCj4CmjgW0VFxbdKSkrvuLi4XgDxX0ZGxkaYgVxiPPVy9vLvxI3Fr8vYydwDGnRXykr6LlDsjrqj5hshaaGpDOrq6tO8vb2vODs7nwwICDjn4eFxBkiftba2PhoYGHgX6NpsmIEsHCy2kmaSZcJaotkiRmL5QppCeUI6QnnS5nJ52uraJRKCEl4MZmZms0NCQm76+/ufi4yMvJSYmHglIiLiEtDg0zk5OU9lZWULkcIyEIgnAQO0XYydvVNSUrpLUkyii5mZuR0oPgGI4xmAXpzl7u5+Bxi7F1xcXC45ODhcBPIv+/n5XQRa9IyXlzcPZhojA0OnIC/3H0FBgZdyUuLvZOXkgEGk8E5UVPSVgKDAL3Z29iUMPDw8GkBvWUlLS5uDsJiYmAWQtpCRkTEDhqUNBweHLNAsVpCB0kK8he52Vjf1DQwPAX1w1tHR8TTQAadNTU2PAB1zXVVVtYPYZANKjwwiYmK1pubmzzQ1NC4ADbluYmJ63cDA4BoQX7a1tX0KdNhEqE/ACRcXhskzAL2UqKentxvoklXa2trr1dTU1mpqaq4zMTHZAMS7hIWFSwErRJmf8FS3uAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Diagram: Prompt caching&quot;
        title=&quot;&quot;
        src=&quot;/static/5bd316e089767d73809c28b93ba27e01/e996b/prompt-caching.png&quot;
        srcset=&quot;/static/5bd316e089767d73809c28b93ba27e01/17741/prompt-caching.png 263w,
/static/5bd316e089767d73809c28b93ba27e01/52211/prompt-caching.png 525w,
/static/5bd316e089767d73809c28b93ba27e01/e996b/prompt-caching.png 1050w,
/static/5bd316e089767d73809c28b93ba27e01/087e3/prompt-caching.png 1575w,
/static/5bd316e089767d73809c28b93ba27e01/b3154/prompt-caching.png 1681w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Source: OpenAI Documentation&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The API request process follows a systematic caching mechanism. When a request is initiated, the system first performs a Cache Lookup to determine if the initial portion (prefix) of your prompt exists in the cache. In the event of a Cache Hit, the system utilizes the cached result, which leads to decreased latency and reduced costs. Conversely, during a Cache Miss scenario, the system processes your complete prompt, and subsequently caches the prefix of your prompt to optimize future requests.&lt;/p&gt;
&lt;p&gt;This technique not only increases the performance of your LLM interactions but can also provide up to 80% cost reduction.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;That’s all folks! 🙌&lt;/h2&gt;
&lt;p&gt;Thanks for reading! Stay tuned for more content about prompt engineering! I’ll be sharing advanced techniques, real-world examples, and practical strategies to help you master the art of communicating with AI. My newsletter form is just a few DOM elements below. Don’t forget to subscribe to get notified when new articles are published.&lt;/p&gt;
&lt;p&gt;Do you have any other tips for crafting better prompts? I’d love to hear your thoughts and experiences.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Structure your prompts or get noise]]></title><description><![CDATA[A comprehensive guide on how to structure prompts effectively for AI language models to improve the quality and accuracy of generated responses.]]></description><link>http://www.nicotsou.com/how-to-structure-your-prompts/</link><guid isPermaLink="false">http://www.nicotsou.com/how-to-structure-your-prompts/</guid><pubDate>Tue, 10 Dec 2024 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;A well-structured prompt can significantly improve the quality and accuracy of AI-generated responses. This guide will walk you through five essential steps to craft prompts that consistently deliver the results you need.&lt;/p&gt;
&lt;p&gt;Here’s a simple framework to help you structure your prompts effectively and get the most out of AI language models. By following these steps, you’ll be able to communicate your requirements clearly and receive more accurate, relevant responses from AI systems.&lt;/p&gt;
&lt;p&gt;The examples provided below are not complete prompts, but rather building blocks you can combine with your existing prompts to enhance your results.&lt;/p&gt;
&lt;p&gt;Let’s examine each step in detail, and then combine them all together at the end.&lt;/p&gt;
&lt;h2&gt;Step #1 — Start with a &lt;em&gt;verb&lt;/em&gt;&lt;/h2&gt;
&lt;p&gt;Remember how we talked about different ways to use AI language models (LLMs) in a previous &lt;a href=&quot;https://nicotsou.com/the-art-of-prompt-engineering/&quot;&gt;article&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/bb81dcdfded720051c3168e3309dbfcd/da06b/LLM-use-cases.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 52.47148288973384%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABYlAAAWJQFJUiTwAAABLUlEQVR42m2S2Y6DMAxF+f+fHPVhKJS1QCCsHh+PTJFoJCuOl+trx4l8Ofu+yzPLZVmWmy/OswxDMP04DrtTjW27zvTEAdZ1tRuZNSnLcxnH0XSPQS+KUvL8ZbZt26zob5pK07YfwGmapCgriTEa8DAM0mlFEhDsCIwAoBA3Bfb93+/dnIAhBEv2JIJJpAA+YvDBFL8X2vR9Pcn1ERTAgjTYmfLGPgRlpUUAphh63TRW9CsgAC+dD8mrMoAFyUj77qSqm7N1WDpb/5gbIAAABWUFiBeBQV3X+hmFNMoIm3+gg8LegU/AzwpkxpLz1o/xYV9ny0xjnE2nSFXVZrvNkKRUV6Dve3s/dT18HfAhtPzzeOhWlGcXLjdA3y1vhzYd3DuAJSPA7jZvfV4W+QMArQ+rEGmp0gAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Diagram: LLM Use Cases&quot;
        title=&quot;&quot;
        src=&quot;/static/bb81dcdfded720051c3168e3309dbfcd/e996b/LLM-use-cases.png&quot;
        srcset=&quot;/static/bb81dcdfded720051c3168e3309dbfcd/17741/LLM-use-cases.png 263w,
/static/bb81dcdfded720051c3168e3309dbfcd/52211/LLM-use-cases.png 525w,
/static/bb81dcdfded720051c3168e3309dbfcd/e996b/LLM-use-cases.png 1050w,
/static/bb81dcdfded720051c3168e3309dbfcd/087e3/LLM-use-cases.png 1575w,
/static/bb81dcdfded720051c3168e3309dbfcd/8079d/LLM-use-cases.png 2100w,
/static/bb81dcdfded720051c3168e3309dbfcd/da06b/LLM-use-cases.png 3052w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Here’s a simple but powerful tip: start your prompts with action words (verbs). When you begin with a clear action word, you tell the AI exactly what you want it to do. This helps the AI understand your request better and gives you more accurate results. Think of these action words as simple instructions that guide the AI to give you the kind of response you’re looking for.&lt;/p&gt;
&lt;p&gt;Here are some examples:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;write&lt;/em&gt;&lt;/strong&gt; an essay&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;create&lt;/em&gt;&lt;/strong&gt; a list of marketing ideas&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;summarize&lt;/em&gt;&lt;/strong&gt; the key article points&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;classify&lt;/em&gt;&lt;/strong&gt; these news headlines by topic&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;extract&lt;/em&gt;&lt;/strong&gt; the names of all key stakeholders&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;translate&lt;/em&gt;&lt;/strong&gt; from English to Greek&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;edit&lt;/em&gt;&lt;/strong&gt; this paragraph to improve clarity&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;solve&lt;/em&gt;&lt;/strong&gt; this math problem step-by-step&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;answer&lt;/em&gt;&lt;/strong&gt; the questions below&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;convert&lt;/em&gt;&lt;/strong&gt; measurements to grams&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;You have flexibility in choosing verbs that best fit your needs - there’s no requirement to stick to these exact examples. Feel free to use synonyms or alternative verbs that clearly communicate your intended action, as long as they effectively convey the task you want the AI to perform. The key is to start with a clear, action-oriented verb that sets the direction for your prompt.&lt;/p&gt;
&lt;p&gt;Moreover, it’s important to note that this is not an exhaustive list of LLM capabilities. Generative AI represents a rapidly evolving technology, with new use cases and applications being discovered and developed continuously. As researchers and practitioners explore novel implementations and methodologies, the potential applications of LLMs continue to expand across various domains and industries.&lt;/p&gt;
&lt;p&gt;However, starting with an action verb in your prompt is important for two main reasons. First, it tells the AI exactly what you want it to do. Second, it helps you get better answers. When you start with a clear action word (like “write” or “explain”), you’re telling the AI precisely what kind of response you need. This makes it much more likely that you’ll get the type of answer you’re looking for.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/3c53eb55f8f1cc0b82d4e28343a748fe/d5f99/the-lord-of-prompt-engineering.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 59.6958174904943%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAAsTAAALEwEAmpwYAAACuUlEQVR42h1RC1MSURhdwOUd0BgqKOgusLssu7CwPOSxPAxwUAZoEEzCUckcFc0kJy3ATMZe05Q9LDVSe4xO05jaH6rpl3Tpzpn7fXPvd+45cy5kQFHMZCJJ0mazOZ2sz+eNRq+m0+mxQmF2bm6+XC6VSrlcLpFIRAYifr/P5XI5HA4rbSUIM4QgCIZhFEXZ7fb+fk84Es5cy0xPT+982D358fN+faNWX69UKoVCYWhoKBwKeTweh4OlaRrHcQhFUBzDaIpiWdbv9w8OxovF4sZm48mLV3OLS2PjE+OTpUZj685yJZfPx2JRr9cLyBRFA0kI6UMwU0vZybIcx6XSqfny/Mvt15+/Hl+/MT4QjVFWa7X28Omz55NTU0AcCADbFovFZDRBvXq90WAgyRY5FApls9m7KyufDo4Ov3wrTkxZaFqpUq2uPTg4PFpYWARZBAKc3e4gzaQBQSFdjw5FDWbCzDrYcDicH83X6vVm8+DNzm79UcMX4Ew4cXp6fnzy/d7qKngauGMYO4ETSG8fpNVoQcExnGGYYDAIyNVq9ezsYmdvP5nJ0Ix9c+vx7z9/zy9+Vau17MhIIBCwWm3AM1CFOjs6QDEajCBAn8+XyWRW19Z295tLlRWMMJtwfGZ2/vZyZW+/ub6+kUqlwAxpsSC9iKZLA7W3t2s0mr4+BHy1y+2Kx+OJ4eFILP529+P2u/flpeXi1C3G6bYytlx+NJlMut1uYFOv06uvqCGVUtWhVvd096AoSlEWM0moO9Wo0RiND96cmcvmC14uhJOkRqvVdms5LghybjHVaoVCAcEwLBaJZFKJXC5XKRWX5FKZTAIuOru6cILo0emVSqVCpVBdVkmlEjAmk0iEQljwf0E8Hk8g4MOwAIbbwKFYBLcgBhCKREKwC4VtAKARi0AjaGsT8Pl8HgCP/w/dha6VVTz2rwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;An AI generated image of a male statue.&quot;
        title=&quot;&quot;
        src=&quot;/static/3c53eb55f8f1cc0b82d4e28343a748fe/e996b/the-lord-of-prompt-engineering.png&quot;
        srcset=&quot;/static/3c53eb55f8f1cc0b82d4e28343a748fe/17741/the-lord-of-prompt-engineering.png 263w,
/static/3c53eb55f8f1cc0b82d4e28343a748fe/52211/the-lord-of-prompt-engineering.png 525w,
/static/3c53eb55f8f1cc0b82d4e28343a748fe/e996b/the-lord-of-prompt-engineering.png 1050w,
/static/3c53eb55f8f1cc0b82d4e28343a748fe/087e3/the-lord-of-prompt-engineering.png 1575w,
/static/3c53eb55f8f1cc0b82d4e28343a748fe/8079d/the-lord-of-prompt-engineering.png 2100w,
/static/3c53eb55f8f1cc0b82d4e28343a748fe/d5f99/the-lord-of-prompt-engineering.png 3426w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The Lord of prompt engineering, according to AI.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Step #2 — &lt;strong&gt;Assign a &lt;em&gt;role&lt;/em&gt;, &lt;em&gt;job&lt;/em&gt;, or &lt;em&gt;function&lt;/em&gt;&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;When you assign a specific role, job, or function to the AI, you provide it with important context about the expertise and perspective it should adopt. This helps the AI understand the domain-specific knowledge and terminology it should use, leading to more accurate and relevant responses. The role you assign acts as a lens through which the AI filters and structures its knowledge, ensuring the output aligns with professional standards and best practices in that field.&lt;/p&gt;
&lt;p&gt;Here are some examples of how assigning a role can enhance your prompts and lead to more specialized responses:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; As an &lt;em&gt;application security specialist&lt;/em&gt;, evaluate the security measures in place for this API and recommend improvements to protect against common vulnerabilities like SQL injection and cross-site scripting.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;When you specify “As an application security specialist” the AI will adopt the mindset and expertise of a security professional, enabling it to provide specialized insights into potential vulnerabilities, security best practices, and technical recommendations that align with industry standards for application security. This role-based context helps ensure the AI’s response focuses on security-specific concerns rather than general programming advice.&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; You are a &lt;em&gt;database administrator&lt;/em&gt;, review the following database schema and suggest optimizations to improve query performance and data integrity.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;By specifying the role as a “database administrator,” the AI will focus on providing technical recommendations from a database management perspective, including insights about indexing strategies, normalization, and query optimization patterns that align with database administration best practices.&lt;/p&gt;
&lt;p&gt;This role-based approach helps the AI adopt the appropriate tone of voice for specific domains and disciplines, ensuring that responses maintain the expected level of formality, technical depth, and professional standards for each field.&lt;/p&gt;
&lt;h2&gt;Step #3 — &lt;strong&gt;Provide &lt;em&gt;examples&lt;/em&gt;&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;In prompt engineering, “shot” refers to the number of examples provided to guide the AI model. When we talk about “X-shot” prompting, we’re describing how many examples we’re giving the model to help it understand the pattern we want it to follow.&lt;/p&gt;
&lt;p&gt;By default, when no examples are provided (zero-shot), the model relies solely on its training data and the task description in the prompt to generate responses. As we increase the number of examples - whether it’s one-shot (single example), few-shot (several examples), or many-shot (numerous examples) - we give the model more context to understand the specific pattern or format we’re looking for. This approach helps the model better align its responses with our expectations and requirements.&lt;/p&gt;
&lt;p&gt;Here’s an example:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; &lt;em&gt;Classify the sentiment&lt;/em&gt; of a customer message. Focus on identifying the sentiment accurately based on context.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;For example&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“Your team helped me so quickly with my claim!” Meaning: Positive&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“I am beyond frustrated with the wait times.” Meaning: Negative&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“Can someone explain this to me?” Meaning: Neutral&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“I’m so grateful for the help I received!” Meaning: Positive&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“This process is more complicated than I expected.” Meaning: Ambiguous&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Sentence to classify&lt;/em&gt;: “{MESSAGE}”&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;In this example, we’re showing how to classify sentiment in customer messages. The prompt starts by clearly stating the task “Classify the sentiment” and then provides 5 examples that demonstrate different types of sentiment:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A clearly positive message about quick help&lt;/li&gt;
&lt;li&gt;A negative message expressing frustration&lt;/li&gt;
&lt;li&gt;A neutral question seeking information&lt;/li&gt;
&lt;li&gt;Another positive message showing gratitude&lt;/li&gt;
&lt;li&gt;An ambiguous message that could be interpreted multiple ways&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These examples help the AI understand the nuanced differences between sentiments and how to categorize them appropriately. Notice how each example includes both the message and its classification, creating a clear pattern for the AI to follow.&lt;/p&gt;
&lt;p&gt;The prompt then ends with a placeholder &lt;code class=&quot;language-text&quot;&gt;{MESSAGE}&lt;/code&gt; where you would insert the actual message you want to classify, maintaining the same format as the examples.&lt;/p&gt;
&lt;p&gt;AI models learn differently from examples. Some smart AI models only need to see a few examples to understand and copy patterns. Other AI models need more examples to learn the same task properly.&lt;/p&gt;
&lt;p&gt;It’s important to maintain a careful balance — providing an excessive number of examples can potentially constrain the model’s creative flexibility and lead to overly rigid responses that too closely mirror the provided examples rather than generating novel, contextually appropriate outputs. The key is finding the optimal number of examples that enables the model to understand the desired pattern while preserving its ability to generate diverse and creative responses.&lt;/p&gt;
&lt;p&gt;Another important concept related to examples in prompting is &lt;a href=&quot;https://en.wikipedia.org/wiki/Fine-tuning_(deep_learning)&quot;&gt;fine-tuning&lt;/a&gt;, which is a more advanced way of teaching AI models specific patterns or behaviors. While providing examples in prompts (few-shot learning) helps guide the model temporarily for a single interaction, fine-tuning involves actually updating the model’s parameters through additional training on specific examples. This technique goes beyond the scope of this article. I highly recommend sticking with X-Shot Prompting since it effectively handles most use cases you’ll encounter.&lt;/p&gt;
&lt;h2&gt;Step #4 — &lt;strong&gt;Describe the desired output &lt;em&gt;format&lt;/em&gt;&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Specifying the desired output format is crucial for getting responses that match your needs.&lt;/p&gt;
&lt;p&gt;Here’s an example:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; Draft a &lt;em&gt;friendly&lt;/em&gt; &lt;strong&gt;[1]&lt;/strong&gt; email to my &lt;em&gt;fellow software architect&lt;/em&gt; &lt;strong&gt;[2]&lt;/strong&gt;, expressing gratitude for their support in the recent project review.&lt;/p&gt;
&lt;p&gt;Structure the email in &lt;em&gt;three brief sections&lt;/em&gt; &lt;strong&gt;[3]&lt;/strong&gt;: an opening acknowledgment, specific points highlighting their valuable contributions, and a closing statement of appreciation.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;This step involves:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;setting the tone (whether formal, casual, or technical),&lt;/li&gt;
&lt;li&gt;identifying the target audience (which helps tailor the language and complexity level appropriately),&lt;/li&gt;
&lt;li&gt;and clearly outlining the structure or format of the expected response.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;By providing these parameters, you give the model essential context about how to package and present its response in a way that will be most useful and relevant for your specific use case.&lt;/p&gt;
&lt;p&gt;Now, you can request the model to output responses in various structured formats that align with your content needs. For example, you can specify output formats like JSON for structured data representation, XML for hierarchical document structures, Markdown for easy-to-read formatting, YAML for configuration files, or CSV for tabular data. This flexibility allows you to seamlessly integrate AI responses into your existing systems and workflows, while ensuring the output maintains consistent structure and syntax according to the specified format.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/7cd38bbd954cb3ff4ad8d30e99ddf223/65200/prompt-ideas.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60.45627376425855%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAADDElEQVR42lWT3U+TZxjGawmlFKEVCpsKoUpLC/16+0Fb+ajQKciXQOs2BxwMJ7BFBYyGuAO6uUkAN7DGqqhRAQkfGg/EmJ2OZJl/gYnJDlzM/o/f7vctybLT53nu331d9309ukAwgE/x4/a48fq8xGIROjs7OHduhHQ6zb3791lZW2NldZVHjx/xdPUpd7J3uPb9Nc5+dZbm5mY8Xi9Olwu7w4FOCeSA9QL0+320tDSSOjPA1atXePjwATPpGTLZu3z4+A+/7+6y+myNJytPmJ29wYg0bWtr04Q4nU7s9hp0gWAQf0DBI4eq2s8SrQwND5L+YYaNzQ3Gxse4NDHJu/d/8eHvj2xubbG2/oxffr3Jt9+N095+UoT4NWCN3S5AUaiq9Iq6YCgoD04wMvI1c3OzbG9vs7i0xEAqxc6b3/jjz7c8f/lCa7Qk5xcvXpDxdKIoyp5Cx/+BoXBIm9/Y2HluZW6xvLxMMpWkvLxM3ij8dONnXr3eYWNrk9u3M0xMXKK7p4eguHQ6XTmgClMtq8BwQ5iu7lOaFbXg5sICxxpjYslLdXUVjU3HxO462y+ek5GGk5MT9J7uFWehPYViWQn+B4xEI/T0dHF+9BspyDA8PES1rYrDlQc5YqvWRvLj9eu82tlhcXGJqalJ+vr7xFkYl2zZoW5ZXYR/LzqRWJSurlOMCnBhfp54Sws6nY4iUwEV5Vbq6uqIt8bJ3s1KdLJMXZ6iv7+fhkgDLrmrra3NAZWgoqlsiEQ43ddLKjlAzdEjGPL1GI35FBYaMJuLtZGEpdjn9zA4NMj09DTJZJJoNEq9262pzC1lz7Ya0oTkymQysW+fjuLiIkrLLALbT2mpRfLmodZhxyuZraqq5HjrcT7/8gv5DDE8Hg/19e4cMCCzCclgE4kENptNs2k0FgiohIpPrBTtN0mTQllMJQcsZioPH8JcUozVWkZ7RwdNTU34JMduUalTNxSVDmo39dJsNmtAvV6PwZCPxVJCXp5eAx78tAJjgQFrWSmF0lAFdkhNPB4XQWEZhZ9/AUdFuUeyCA+QAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;An AI generated image of a woman with a bulb as a head.&quot;
        title=&quot;&quot;
        src=&quot;/static/7cd38bbd954cb3ff4ad8d30e99ddf223/e996b/prompt-ideas.png&quot;
        srcset=&quot;/static/7cd38bbd954cb3ff4ad8d30e99ddf223/17741/prompt-ideas.png 263w,
/static/7cd38bbd954cb3ff4ad8d30e99ddf223/52211/prompt-ideas.png 525w,
/static/7cd38bbd954cb3ff4ad8d30e99ddf223/e996b/prompt-ideas.png 1050w,
/static/7cd38bbd954cb3ff4ad8d30e99ddf223/087e3/prompt-ideas.png 1575w,
/static/7cd38bbd954cb3ff4ad8d30e99ddf223/8079d/prompt-ideas.png 2100w,
/static/7cd38bbd954cb3ff4ad8d30e99ddf223/65200/prompt-ideas.png 3383w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Step #5 — &lt;strong&gt;Provide detailed &lt;em&gt;guidelines&lt;/em&gt;&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Clear instructions are important when working with AI. When you give the AI specific rules and requirements, it better understands exactly what you want. Think of these instructions like a recipe - they help the AI know what to include and what to avoid. Just like in the example below, good instructions can turn a basic request into a clear, well-organized response.&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;Write a proposal document for a new website redesign project with these &lt;strong&gt;&lt;em&gt;specific rules&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Open with an executive summary that briefly describes the purpose and objectives.&lt;/li&gt;
&lt;li&gt;Include sections for Project Scope, Timeline, Budget, and Team Roles.&lt;/li&gt;
&lt;li&gt;Under Project Scope, outline the key features and design changes planned.&lt;/li&gt;
&lt;li&gt;Add a detailed timeline table listing milestones and expected completion dates.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Conclude with a call-to-action encouraging stakeholders to approve the proposal.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;In the example above, the guidelines provided help shape how the AI model approaches the task in several important ways. They establish the necessary components that must be included (executive summary, project scope, etc.), define the structure and organization of the content (specific sections and their order), specify the level of detail required (such as including a detailed timeline table), and set expectations for the conclusion (ending with a specific call-to-action).&lt;/p&gt;
&lt;p&gt;These guidelines serve as constraints that help the AI model generate more focused and relevant output. Without such specific guidelines, the response might be too general or miss important elements that you need. Think of these guidelines as quality control measures that ensure the AI’s output meets your specific requirements and standards.&lt;/p&gt;
&lt;h2&gt;Putting everything together&lt;/h2&gt;
&lt;p&gt;Start with a role:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;You are a Digital Marketing Strategist for an e-commerce brand.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;Provide examples:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;Examples:&lt;/p&gt;
&lt;p&gt;“Social media ad campaign with special discounts.”&lt;/p&gt;
&lt;p&gt;“Email marketing sequence promoting holiday deals.”&lt;/p&gt;
&lt;p&gt;“Influencer partnerships to showcase products.”&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;Specify the output:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;Present the campaign plan as a 3-part strategy (social media, email marketing, influencer partnerships). Each part should have 2-3 action points.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;Provide Guidelines:&lt;/p&gt;
&lt;aside&gt;
&lt;ul&gt;
&lt;li&gt;Specify the target audience for each campaign channel.&lt;/li&gt;
&lt;li&gt;Include a suggested timeline and estimated budget for each part.&lt;/li&gt;
&lt;li&gt;Outline key metrics to track, such as engagement rate for social media or conversion rate for emails.&lt;/li&gt;
&lt;/ul&gt;
&lt;/aside&gt;
&lt;p&gt;Define the task:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;Create a promotional campaign plan to boost sales during the holiday season.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;To enhance your prompts further, consider incorporating additional contextual elements. This includes providing relevant technical documentation and background information, backing up your prompts with supporting statistics and data, including previous conversation history, and the last message from the user that the LLM needs to respond to.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;That’s all folks! 🙌&lt;/h2&gt;
&lt;p&gt;These tips for writing better prompts are really helpful, but keep in mind that there’s no one-size-fits-all approach. What works great in one situation might not work as well in another. How well a prompt works can depend on which AI model you’re using, how complex your task is, and what specific things you need to accomplish. The key is to be flexible and willing to try different approaches. Don’t be afraid to adjust and experiment with your prompts until you find what works best for your specific situation.&lt;/p&gt;
&lt;p&gt;Thanks for reading! Stay tuned for more content about prompt engineering! I’ll be sharing advanced techniques, real-world examples, and practical strategies to help you master the art of communicating with AI. My newsletter form is just a few DOM elements below. Don’t forget to subscribe to get notified when new articles are published.&lt;/p&gt;
&lt;p&gt;Cover art generated with Adobe Firefly.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Prompting has rules. Most people ignore them.]]></title><description><![CDATA[Guidelines and best practices for effective prompt engineering when interacting with generative AI models.]]></description><link>http://www.nicotsou.com/the-rules-of-prompting/</link><guid isPermaLink="false">http://www.nicotsou.com/the-rules-of-prompting/</guid><pubDate>Tue, 03 Dec 2024 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;Learn Prompt Engineering, they say. It’s the skill of the future, they say.&lt;/p&gt;
&lt;p&gt;But with all the buzz around prompt engineering and people sharing their “secret prompts,” we often get lost in thinking that prompting is similar to traditional coding. We see complex, multi-page prompts with intricate instructions and think that’s what we need to master.&lt;/p&gt;
&lt;p&gt;The reality is much simpler and more straightforward. Prompting is not about writing complex algorithms or mastering syntax - it’s about clear, effective communication in natural language. While programming requires learning specific syntax and rules, prompting leverages something we already know: how to express our thoughts clearly.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“Generative AI is teaching us that the way you speak is actually code itself” — Lisa Huang&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Language, like code, is structured, precise, and can be “interpreted” by humans to perform tasks or generate responses. In generative AI, our words, syntax, and tone act as a programming language for interacting with models. Just as specific code produces particular software results, the way we phrase our input directly shapes the AI’s output.&lt;/p&gt;
&lt;p&gt;However, just like in human communication, it’s not just about that - it’s also about understanding our conversation partner. In this case, that means understanding the quirks, limitations, and peculiarities of Generative AI.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/b65f5603a78a8c75a3bbc5243895bb46/7470b/prompt-engineering-clickbait.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 46.00760456273764%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAABYlAAAWJQFJUiTwAAAB5UlEQVR42nWRzavhYRTHf3+DP0NZyUpWulMkpSRqZMdKEnUtvOyEjVCivBSxUHfcKZKXIkp5KbHydlOsGGKBQfjOfZ47auZOc+rb85w6z3k+53wZfIr7/U7PbDYLh8MBj8cDp9MJv9+PWCyGcDiMYDCI8Xj8V/0jmOv1il6vh263S9VsNnE8HuHz+SCXy6HVaiGTySCRSPBVrYZer4dKpUK1WqUNzuczbrcbLpfLR8PD4QCBQEAfer1ePD8/YzAYIJfLwWg00lyn04HNZoPFYoHD4YDH40Gj0SAajSKZTCIej6NYLGK1WoEhNCKRCDabDel0GiaTCYVCAaPRCK1Wi6pWqyGVSkH9TigUCqFUKimxxWKB1Wql1ARmvV6DOZ1+gs/n4+npC9xuN7hcLkqlEsWfTCb4/vpKC0nE4zGIxWIoFAoqsmMyAdmxy+XCdrsFQ5Y6nb5hOBxiNptRM8idRCQSgdlsRj6f/51HIZVKKSlRIpFAJpNBIBBAvV7/IPzsMjGI6HQ6oVwuIxQK0WKSt9ttfHt5wfSdfD6fY7FYYLfbYf1jjc1mQ41hHtYTEcc7nQ51kOzGYDDAbrfTsQgpWXy/36fO/i/+ISSuk18bjQYdlRhEzkqlguVyif1+Tz9+QPwp0vAXoco4rZGdB6oAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Clickbait titles from YouTube videos&quot;
        title=&quot;&quot;
        src=&quot;/static/b65f5603a78a8c75a3bbc5243895bb46/e996b/prompt-engineering-clickbait.png&quot;
        srcset=&quot;/static/b65f5603a78a8c75a3bbc5243895bb46/17741/prompt-engineering-clickbait.png 263w,
/static/b65f5603a78a8c75a3bbc5243895bb46/52211/prompt-engineering-clickbait.png 525w,
/static/b65f5603a78a8c75a3bbc5243895bb46/e996b/prompt-engineering-clickbait.png 1050w,
/static/b65f5603a78a8c75a3bbc5243895bb46/087e3/prompt-engineering-clickbait.png 1575w,
/static/b65f5603a78a8c75a3bbc5243895bb46/8079d/prompt-engineering-clickbait.png 2100w,
/static/b65f5603a78a8c75a3bbc5243895bb46/7470b/prompt-engineering-clickbait.png 3156w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;No, you don’t need any of that. Focus on mastering the basics instead.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;These AI models, despite their impressive capabilities, come with their own set of challenges. They can occasionally “hallucinate” facts, struggle with complex logical reasoning, or misinterpret nuanced instructions. They might confidently provide incorrect information or fail to maintain consistency across long conversations.&lt;/p&gt;
&lt;p&gt;Let me introduce you to the fundamental rules of Prompt Engineering. Note that the examples I’m providing are quick ideas to help improve your existing prompts. They are not meant to be used exactly as shown.&lt;/p&gt;
&lt;p&gt;Alright! Grab a coffee and let’s dive in. ☕️&lt;/p&gt;
&lt;h2&gt;Rule #1 — &lt;strong&gt;Provide clear and specific &lt;em&gt;instructions&lt;/em&gt;.&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;If you ask a friend to make you a cappuccino, they will probably come back with a delicious hot beverage to satisfy you. But if you ask your friend “Did you use non-dairy milk, because I’m lactose intolerant?”, they will most likely get surprised and think “How should I know?“.&lt;/p&gt;
&lt;p&gt;Because you were not precise and specific with what kind of cappuccino you wanted, your friend just assumed you would go with the most common one. The same applies with LLMs.&lt;/p&gt;
&lt;p&gt;Yes, LLMs are trained on vast amounts of data, encompassing a wide array of topics and contexts. But in order to get the desired result, you need to be specific about your request so the model can deliver focused output. Be clear about what you want the AI to do by providing sufficient parameters.&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Don’t just prompt:&lt;/strong&gt; “Write a poem about email.”&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; “Draft an informal email to my manager requesting time off.”&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;Minimize vague or imprecise descriptions and focus on what to do instead of what not to do. This is your way towards accurate, relevant responses.&lt;/p&gt;
&lt;p&gt;Here’s a better example:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Don’t just prompt:&lt;/strong&gt; What should I avoid when designing a user interface?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; What are the &lt;em&gt;best practices&lt;/em&gt; when designing an &lt;em&gt;accessible&lt;/em&gt;, &lt;em&gt;user-friendly&lt;/em&gt; web interface?&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;I clearly specify what are my expectations about designing a great user interface.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a2e7cb6764275a283db6c657eafcc69b/a24b5/coffee.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAACjElEQVR42mOoqKgoLS0rKiopKCjMzc3Lzs7JzMiMjYlJiE9MT09PSkry8fF1cXHx9vJycnS0tDA3NNDX0FBXVlaSk5VhqKmuraysKistLykB6c/Pz09JTunq7Dpy5MjDhw8ePXx46NDh0pJSLy8vXx8fOztbczNTfT1dTZB+RYbauvqq6urysnKg/SXFpRnpmZMmTnr86OGH9+8fgsHXr18/fPiQn5fv4uzi4e5ubW1lbGSoo62lpqbCUFdXX11dU1ZWUVZWnpdXAHT/5cuX3r59s3vP3qqaut6+CVeuXP3799/p02d0dXU8PTwdHBzMTE31dHXVNdQYamrqqqqAmsuBlmdmZnd3d79+9fL2rdsVlVWz58ydOHHy/AULnz599vHjJ3t7e309PXd3dwtzcwMDAy0tDYaqquqKikqgm4GWZ2VmT506Dejga9euNTQ1z5u3oKm5de7cBVu2bFuxcpWDg6OsrIynh4eVlZUR0OU62gzl5UAHlxUXlxQXl2Zn5dQ3NHz6+PHLly/7Dx6cPn3W/AWLjxw5npSUHOAfICkpKSYmYgkGxsZGenp6DEBPFhYWFRYWFhQU5ebkJScl7z+w/////5+/fD1z9uziJUvT0jKAXnV0cGRgYODn41FRVgJq09cHBrkOQ05ODihuM7OAKCMjMzklBRi9u3bt+vPnH9D+5cuWx8cnAAOJh4cXqJmbm0NCQgwYSZqaGlpamgzJUJACjF6g8xITkxMSEn19fUNDQyrKywvy84GWMDExMTIysLGxCPDziouLyMvJqKooaQBDOyoqKjoaSETFxMRER0VHRYLYcbFxwChVUlTk5+cTEuQXERYEksLCAuJiIjLSkvLyskpKCupqqgASgA0JmobxRwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Hey, what happened with my coffee? Hello?&quot;
        title=&quot;&quot;
        src=&quot;/static/a2e7cb6764275a283db6c657eafcc69b/e996b/coffee.png&quot;
        srcset=&quot;/static/a2e7cb6764275a283db6c657eafcc69b/17741/coffee.png 263w,
/static/a2e7cb6764275a283db6c657eafcc69b/52211/coffee.png 525w,
/static/a2e7cb6764275a283db6c657eafcc69b/e996b/coffee.png 1050w,
/static/a2e7cb6764275a283db6c657eafcc69b/087e3/coffee.png 1575w,
/static/a2e7cb6764275a283db6c657eafcc69b/8079d/coffee.png 2100w,
/static/a2e7cb6764275a283db6c657eafcc69b/a24b5/coffee.png 2861w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Hey, what happened with my coffee? Hello?&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Rule #2 — &lt;strong&gt;Always &lt;em&gt;evaluate&lt;/em&gt; your output.&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Each AI model has a unique training set, relies on different programming techniques, and has a specific knowledge cutoff date. Therefore, it’s essential to critically assess each output to ensure it’s accurate and useful for your needs.&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Always ask if your response is&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Consistent&lt;/li&gt;
&lt;li&gt;Unbiased&lt;/li&gt;
&lt;li&gt;Relevant&lt;/li&gt;
&lt;li&gt;Accurate&lt;/li&gt;
&lt;/ul&gt;
&lt;/aside&gt;
&lt;p&gt;For example, the word “Terminal” relates to &lt;em&gt;Transportation&lt;/em&gt; and &lt;em&gt;Computing.&lt;/em&gt; If a user asks about “Terminal,” the AI might provide information about airport terminals without considering the computing context.&lt;/p&gt;
&lt;h2&gt;Rule #3 — &lt;strong&gt;&lt;em&gt;Context&lt;/em&gt; is king!&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;When software architects are about to make an important decision, they always ask for context. Any situational or background information may drive the decision towards a specific direction. The same can happen with your prompts.&lt;/p&gt;
&lt;p&gt;For example, when seeking coding help, specifying that you’re using React with Material UI as your component library will get you more targeted responses than simply asking “How to create a login form with React?“. This context enables the AI to understand your specific technical environment and provide more relevant, actionable advice.&lt;/p&gt;
&lt;p&gt;Here’s an example:&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; Create an architecture document for our &lt;em&gt;cloud migration project&lt;/em&gt;. This &lt;em&gt;project is crucial&lt;/em&gt; as we are &lt;em&gt;transitioning our infrastructure to AWS&lt;/em&gt; to &lt;em&gt;improve scalability&lt;/em&gt; and &lt;em&gt;performance&lt;/em&gt;.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;I’m giving a clue about the quality attributes I’m targeting, such as scalability and performance, ensuring the AI tailors its suggestions to meet these priorities.&lt;/p&gt;
&lt;h2&gt;Rule #4 — &lt;strong&gt;Keep your instructions relatively &lt;em&gt;short&lt;/em&gt;.&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;We all have that colleague who writes such long emails that you need an AI assistant to summarize them. “Copilot, what does this guy want from my life?”&lt;/p&gt;
&lt;p&gt;When crafting prompts for AI, brevity is your ally. Long, convoluted instructions often lead to confusion and diluted responses. Instead, focus on being concise while including essential details that guide the AI towards your desired outcome.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Repeating the same sentence multiple times won’t make your prompt more effective.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Think of it like sending a text message - you want to be clear and get your point across without writing a novel. The key is finding the sweet spot between providing enough detail for clarity while keeping your instructions concise and focused.&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Don’t prompt:&lt;/strong&gt; I’m working on a system that needs to handle various client requests, and I’m having trouble figuring out how to design the architecture to manage scalability and performance issues that come up. Can you point me in the right direction?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; How do I implement a scalable and performant software architecture to handle high volumes of client requests?&lt;/p&gt;
&lt;/aside&gt;
&lt;h2&gt;Rule #5 — &lt;strong&gt;Using &lt;em&gt;references&lt;/em&gt; reduces guesswork&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Using examples and references from trusted sources makes your prompts better. When you mention well-known books, frameworks, or methods that experts use, you help the AI understand exactly what you want. This makes the model’s answers more reliable and helps it follow common industry practices.&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; Draft a whitepaper for the engineering team in our organization that emphasizes the importance of cloud-native applications, &lt;em&gt;drawing inspiration&lt;/em&gt; from seminal works like &lt;em&gt;Martin Fowler’s Patterns of Enterprise Application Architecture&lt;/em&gt; and &lt;em&gt;Eric Evans’s Domain-Driven Design&lt;/em&gt;.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;In the previous example, I’m using references from well-known leaders my target audience understand and relate to. When experts in the field see references like Martin Fowler’s work, they immediately grasp the architectural principles and patterns we’re aiming for. This helps establish credibility and provides a shared foundation for technical discussions.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/ec6fc146fe27284510f99cd3e17063a1/a1e6a/Firefly-artwork-1.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 57.03422053231939%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAACaElEQVR42k2SW08TQRiG9x7aXcqhLRI1KhWxabsXCnRPs7sth4I0VKLFuyIIhpjYiHiBCYF4oIJXRpGYGEWUK7VAOVSaGP1jr9/MAvHiyezsfPvMO9+sZBgWdMIwGXSdPzMw2xZYjN4ZJoaGhzGQGSSG4KTSYI4Lm7BsFyazYVoODIENiQs4hhg9MbMdgev2YnAoi5fLJdzO38Ha+3VM3ptGd48Gl4tJaDFHYBxLJd20hczDgklz5jhI9/Yj1ZcRKTRdx3B2BJ+/bGK5tIKR3CilsuG4abFuMUpquZ7Q4MJTGCzLFunSAzcQjcWhqnGsrX/ATmUP5Z0dfPy0gY3NTeRujpKI16aE8DQhF+nHQr4rP3Y2m8PU/QeIJ+K40hFB5aCK6lEN5d0K3rx9h72DfTyee4KkZsBxUuLI5v/CExlfSGomxscn8Hp1FXcLBSE8/HWEyv4hfpS3UXq1gqNaDc+evxAtsh2vl/zIphDyixCXQf2zGAl15MfGsLS4hMmJSTpyAr///KVUVXzd2kI+n8c3Gufnn1JvTZGQnSSkYJKqqkgkElAF/Fml38fAwsIiZmZmcO78WXz/WcZhtYbZ2Tmk0r24RTfe158Rm2tEMqmhq7sH1691QQoGgwi2tBDNghDNmxsD0LUkCoVxxGIxutkStql/D4uPhCASiVBdC9raWtEaDgnC4TDCoTAk2e+HQPYR9VD46K9DU6OCTCaDYrGI6akpRK92ov3iBUTaL6FB8VFdvaiXffXwC3wCSZFlcBpkv4fi0UQpz7SG0HG5HbFoJ5oCCn1Qh8aA7NHgR4DqFPkEmTaQ8Q/ho5B6IKlgjgAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Firefly 20241127173921.png&quot;
        title=&quot;&quot;
        src=&quot;/static/ec6fc146fe27284510f99cd3e17063a1/e996b/Firefly-artwork-1.png&quot;
        srcset=&quot;/static/ec6fc146fe27284510f99cd3e17063a1/17741/Firefly-artwork-1.png 263w,
/static/ec6fc146fe27284510f99cd3e17063a1/52211/Firefly-artwork-1.png 525w,
/static/ec6fc146fe27284510f99cd3e17063a1/e996b/Firefly-artwork-1.png 1050w,
/static/ec6fc146fe27284510f99cd3e17063a1/087e3/Firefly-artwork-1.png 1575w,
/static/ec6fc146fe27284510f99cd3e17063a1/8079d/Firefly-artwork-1.png 2100w,
/static/ec6fc146fe27284510f99cd3e17063a1/a1e6a/Firefly-artwork-1.png 3581w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Rule #6 — &lt;strong&gt;Keep the &lt;em&gt;conversation context&lt;/em&gt; consistent&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;We all have that friend who starts telling a story about their weekend, then suddenly jumps to talking about their cat’s vet appointment, only to circle back to a completely different story about their cousin’s wedding. By the end of the conversation, you’re not sure if the cat went to the wedding or if the cousin was at the vet! This constant context switching makes it difficult to follow the narrative and often leads to misunderstandings.&lt;/p&gt;
&lt;p&gt;When you mix different topics or contexts within the same conversation with an AI model, it can lead to confusion and potentially inaccurate responses. Here’s why keeping contexts separate is important:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Each conversation builds up a specific context and memory that helps the AI understand the topic at hand. When you suddenly switch topics, you risk the AI mixing information from different contexts.&lt;/li&gt;
&lt;li&gt;Jumping between unrelated topics can confuse the model and result in less accurate responses.&lt;/li&gt;
&lt;/ul&gt;
&lt;aside&gt;
&lt;p&gt;Best practices to maintain clear context:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Keep questions relevant to the current conversation thread&lt;/li&gt;
&lt;li&gt;If you need to discuss a new topic, begin a new chat session rather than continuing in the same one&lt;/li&gt;
&lt;/ul&gt;
&lt;/aside&gt;
&lt;p&gt;This approach helps ensure more accurate and focused responses from the AI model, reducing the likelihood of context-related errors or hallucinations.&lt;/p&gt;
&lt;h2&gt;Rule #7 — &lt;strong&gt;Be mindful of the &lt;em&gt;conversation memory&lt;/em&gt;.&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;AI models can chat with us naturally, but they have a simple limitation: they can’t remember things like humans do. They only work with the information you give them right now, in your current prompt. Think of it like talking to someone that suffers from amnesia. If you mention something you said some minutes ago without explaining it again, they might not understand what you’re talking about.&lt;/p&gt;
&lt;p&gt;AI agents maintain conversation context by resending the chat history with each new message. This ensures the LLM has all the information needed for accurate responses. However, the context window of a prompt is limited. You can only include a certain number of tokens, which varies depending on the model you’re using. If your conversation grows too long, it may exceed this context window.&lt;/p&gt;
&lt;p&gt;Remember that the AI won’t remember all of your previous messages. If you’re implementing a chatbot application using a foundation model, you should manage the memory on your own.&lt;/p&gt;
&lt;h2&gt;Rule #8 — &lt;strong&gt;Choose your &lt;em&gt;words&lt;/em&gt; wisely.&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;When the entire organization uses a prompt, would you accept having grammatical or syntactic errors? Here are key elements to avoid when crafting prompts, as they can significantly impact the quality and reliability of AI responses:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Using bad grammar, wrong sentence structure, or typos - these mistakes can make it hard for AI to understand what you want&lt;/li&gt;
&lt;li&gt;Using complex technical jargon when plain language would work better, unless your specific domain requires technical terminology&lt;/li&gt;
&lt;li&gt;Using informal language, local idioms, or slang that your model may not be trained to understand&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And believe it or not, being negative or impolite may also impact your prompt response. This is because AI models are trained on data where polite, respectful language often leads to higher-quality responses. Additionally, LLMs might simulate emotional states conceptually, though they do not experience emotions as humans do. Just keep in mind that overly complex politeness (e.g., excessively indirect requests) can confuse the model and reduce response quality.&lt;/p&gt;
&lt;p&gt;Now, it’s true that while flagship models are quite capable of bypassing these imperfections, older and smaller LLMs have limited ability to handle such issues, making clear communication even more critical.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/bb6b28d5ea5146494076a12d3922602e/303e6/Firefly-artwork-2.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60.836501901140686%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAACpklEQVR42kXSy2sTURQG8LEWbJNW0yZtCtoSQ1+2UZrMM5NMHvPKq3lIWze6EJS6cFGqtIV2axXRLsSNEp8VilZ3gijiUhBFcFUVXCq4VPwDPs+9SdrFXQwz/Ob7zrlCrVpBuTKN6ekiSsUC8oUc3JwNxzHhuhbSqSRkOQZFFZFMxpFKJ5A04kgk6OgaNE2ldwoURYYkSxAqDCuXUCoRlncJsZHLORwzDB1zczO4sn4Nd+t1/p2e0JqgiriuQiVQJkwmTJJECOUmVuAYQY7F02WyKdRqFex8/Y6fv37j9dt3MNJpTE2dQCqV4LCuK1DVJiaLEKUYBI5RTZbKcfeqso8XFxfw5+8/vHz1BqdOn8HhwSMYPzYGg4GULh5ndRugSOlEkcB8wW1WtGHbJj8OgazS6toaPn7+gq3tF6jNzGJwaAhj46M0Sx16XCGwNTuxCVLlXYyqWpZJJwvLziKTSWLr6TZ2vv3AnXsPkLZotqUyqrUaLULimKY1FsGwGKVjR3B5TcJsCyZhppkhzECxmMPWs+d4/+ET6g83ISoqzp6bx/LKKm3aoNlJfH6tdDExilgsCsFxWqBJYIafZFKjjedw//ETrF+/ifqjTcQNA5OR45iYjCAUCjXBvWWIPCEDW3UZSOlY4vDRIVxeWsbGrdu4uHAJV29swMiaCAQC6Ow4AK+nE+FwiBajNTCpUbeRsAmyumwZKv2xq6sD5WoVJ2fncH7+AhaXViCpKoLBIHp8Pvj9vWhv349IZIJSKhwTd0GHgSY/2Wwa/X296O7u5BuzHZeuhAw3X0B4eBgBvx99lDIQ8GOfIMDr9fD5sQvNsGgDtDjG0rEaXkrn83XR3yfpSqgYHRlBT48PHk8HDh3sxkCwH7303NYmQCA0OBDk95CD0Sn8B+JJqpG2jfshAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Firefly 20241127180845.png&quot;
        title=&quot;&quot;
        src=&quot;/static/bb6b28d5ea5146494076a12d3922602e/e996b/Firefly-artwork-2.png&quot;
        srcset=&quot;/static/bb6b28d5ea5146494076a12d3922602e/17741/Firefly-artwork-2.png 263w,
/static/bb6b28d5ea5146494076a12d3922602e/52211/Firefly-artwork-2.png 525w,
/static/bb6b28d5ea5146494076a12d3922602e/e996b/Firefly-artwork-2.png 1050w,
/static/bb6b28d5ea5146494076a12d3922602e/087e3/Firefly-artwork-2.png 1575w,
/static/bb6b28d5ea5146494076a12d3922602e/8079d/Firefly-artwork-2.png 2100w,
/static/bb6b28d5ea5146494076a12d3922602e/303e6/Firefly-artwork-2.png 3362w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Rule #9 — &lt;strong&gt;Eliminate &lt;em&gt;biased&lt;/em&gt; language&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Personal feelings or subjective experiences may cause biased output as a result. This can lead to responses that favor one approach without considering alternatives or drawbacks.&lt;/p&gt;
&lt;aside&gt;
&lt;p&gt;&lt;strong&gt;Don’t prompt:&lt;/strong&gt; Why microservices are the best solution for scaling modern applications?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Prompt:&lt;/strong&gt; What are the advantages and tradeoffs of using microservices compared to other architectural styles for scaling modern applications?&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;The previous example demonstrates personal preferences might overshadow objective analysis of different solutions. To avoid this, frame your prompts in a way that encourages balanced, objective responses that consider multiple perspectives.&lt;/p&gt;
&lt;p&gt;Additionally, overlooking ethical and legal considerations in your prompts could result in responses that may be inappropriate or potentially harmful. Always frame your prompts to encourage factual, balanced responses that respect ethical boundaries and legal constraints. This approach not only yields more reliable results but also promotes responsible AI usage.&lt;/p&gt;
&lt;h2&gt;Rule #10 — &lt;strong&gt;Use &lt;em&gt;special syntax&lt;/em&gt; to express your ideas.&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Sometimes, expressing ideas through structured formats like tables, mathematical equations, metrics, or code snippets can be more effective than using plain text descriptions. Despite being limited to text-based communication with AI models, we can leverage various formatting techniques to convey our ideas precisely.&lt;/p&gt;
&lt;p&gt;Using special syntax or structured formats in your prompts can help organize information and make your requirements clearer to the AI. XML-like tags, markdown formatting, or custom delimiters can create a clear structure that guides the model to understand and process different components of your request separately.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;Write a product description&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
  ## Smart Home Security Camera

  Features:
  **4k video quality** for clear footage
  **Night vision** capability up to 30 feet
  **Motion detection** alerts sent directly to your phone
&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the previous example, we are using an XML tag to differentiate the part of the product information from the rest of the prompt. We also use Markdown syntax to format the text. We help the LLM recognize the different parts of the prompt.&lt;/p&gt;
&lt;p&gt;This approach is particularly useful when dealing with complex prompts that contain multiple elements or require specific formatting in the output.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;That’s all folks! 🙌&lt;/h2&gt;
&lt;p&gt;Thanks for reading! Stay tuned for more content about prompt engineering! I’ll be sharing advanced techniques, real-world examples, and practical strategies to help you master the art of communicating with AI. My newsletter form is just a few DOM elements below. Don’t forget to subscribe to get notified when new articles are published.&lt;/p&gt;
&lt;p&gt;Remember that effective prompting is an iterative process; don’t hesitate to refine your approach based on the responses you receive. Do you have any other tips for crafting better prompts? I’d love to hear your thoughts and experiences.&lt;/p&gt;
&lt;p&gt;Cover art generated with Adobe Firefly.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The art of Prompt Engineering]]></title><description><![CDATA[An in-depth exploration of prompt engineering, highlighting its significance in effectively utilizing AI and Large Language Models for various applications.]]></description><link>http://www.nicotsou.com/the-art-of-prompt-engineering/</link><guid isPermaLink="false">http://www.nicotsou.com/the-art-of-prompt-engineering/</guid><pubDate>Tue, 26 Nov 2024 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;Prompt engineering is not just a buzzword; it’s the golden key to unlocking the true power of AI.&lt;/p&gt;
&lt;p&gt;If you’ve ever wondered how to get a language model like ChatGPT to write you a sonnet, draft a business proposal, or debug your code, congratulations — you’ve already stepped into the world of prompt engineering. It’s where human creativity meets machine intelligence, and believe me, the possibilities are mind-blowing.&lt;/p&gt;
&lt;p&gt;But what are the core principles we need to understand in order to get the most out of it?&lt;/p&gt;
&lt;p&gt;Let’s dive in. 🤿&lt;/p&gt;
&lt;h2&gt;Your AI needs a better wingman&lt;/h2&gt;
&lt;p&gt;We’ve known for a while that AI is transformative. Self-driving cars, personal assistants, predictive typing — these were already reshaping our lives. But who could’ve predicted that simply typing a question into a box could lead to an explosion of creativity, productivity, and, well, &lt;a href=&quot;/ai-will-not-replace-software-engineers/&quot;&gt;controversy&lt;/a&gt;? Whether you’re writing an essay or building an AI agent, prompt engineering is becoming the &lt;em&gt;skill&lt;/em&gt; that sets you apart.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Prompt engineering is the &lt;em&gt;blueprint&lt;/em&gt; for successful AI products.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Just look around; AI is reshaping every corner of our digital landscape. From Gmail suggesting email completions to Midjourney generating artwork, from GitHub Copilot writing code to Jasper crafting marketing copy — GenAI has become as ubiquitous as smartphones. Companies are racing to integrate AI capabilities into their products, and professionals across industries are discovering that mastering these tools isn’t just an option anymore. It’s becoming a necessity for staying competitive.&lt;/p&gt;
&lt;p&gt;I dare to say, it’s a bigger success than the Metaverse. There, I said it! And what’s at the heart of all these innovations? Spoiler Alert!&lt;/p&gt;
&lt;h2&gt;The Art of Talking to Machines&lt;/h2&gt;
&lt;p&gt;Here’s the thing: Large Language Models (LLMs) like ChatGPT are incredibly smart, but they’re not psychic. They rely on your input to decide how to respond. Think of them as your all-knowing buddy who’s willing to help, but only if you know how to ask the right questions. That’s where prompt engineering comes in. It’s all about crafting the perfect &lt;em&gt;ask&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/8e67f2fc290472183c8c44c4c5f71023/d9a4f/prompt-engineering-diagram.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 40.68441064638783%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAy0lEQVR42pVRSwqDMBT0pJ7AleDFPIr42Yilu9QaFRfGxM80L5DWosF2YEhI8oaZiYcTbNt2OFPzjFnzCp5LYI91XcEeNXjbGlEizXxxLxjHMXzfRxRFCIIAYRhCKWUeSCnNnlbrkNZlWdwO+75HnudI0xRZlqEsy/cgCdkUNokQAlVVIUkSFEUBxhjGUYCuPVdM6owGcdandtx1HVpdQdM0GIYBYpo+Dvc9Ul/E2/0OVj/xS8eHyGfgnBv+C3dkHct+zBUoAaWSSuEFuf5uRbZTJfwAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Diagram: LLM Prompt&quot;
        title=&quot;&quot;
        src=&quot;/static/8e67f2fc290472183c8c44c4c5f71023/e996b/prompt-engineering-diagram.png&quot;
        srcset=&quot;/static/8e67f2fc290472183c8c44c4c5f71023/17741/prompt-engineering-diagram.png 263w,
/static/8e67f2fc290472183c8c44c4c5f71023/52211/prompt-engineering-diagram.png 525w,
/static/8e67f2fc290472183c8c44c4c5f71023/e996b/prompt-engineering-diagram.png 1050w,
/static/8e67f2fc290472183c8c44c4c5f71023/087e3/prompt-engineering-diagram.png 1575w,
/static/8e67f2fc290472183c8c44c4c5f71023/8079d/prompt-engineering-diagram.png 2100w,
/static/8e67f2fc290472183c8c44c4c5f71023/d9a4f/prompt-engineering-diagram.png 3270w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Every LLM interaction starts with a user prompt.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;At the heart of every response from an LLM lies an intricate process that begins with your prompt. Here’s the short version: LLMs are trained on massive datasets of text—think everything from books and articles to code and social media. Their goal? To predict the most likely next word or &lt;em&gt;token&lt;/em&gt; based on the context you provide.&lt;/p&gt;
&lt;p&gt;Ask a vague question like, “Tell me about history,” and you’ll probably get a Wikipedia-like overview that’s about as thrilling as a tax document. But ask, “Explain the causes of the French Revolution in a conversational tone, as if you were telling a story to a high school student,” and voilà, you get something compelling, insightful, and tailored to your needs.&lt;/p&gt;
&lt;p&gt;Prompt engineering isn’t just about getting answers; it’s about getting the &lt;em&gt;right&lt;/em&gt; answers. It’s the skill that transforms AI from a mediocre intern to your personal superhero.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/bb81dcdfded720051c3168e3309dbfcd/da06b/LLM-use-cases.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 52.47148288973384%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABYlAAAWJQFJUiTwAAABLUlEQVR42m2S2Y6DMAxF+f+fHPVhKJS1QCCsHh+PTJFoJCuOl+trx4l8Ofu+yzPLZVmWmy/OswxDMP04DrtTjW27zvTEAdZ1tRuZNSnLcxnH0XSPQS+KUvL8ZbZt26zob5pK07YfwGmapCgriTEa8DAM0mlFEhDsCIwAoBA3Bfb93+/dnIAhBEv2JIJJpAA+YvDBFL8X2vR9Pcn1ERTAgjTYmfLGPgRlpUUAphh63TRW9CsgAC+dD8mrMoAFyUj77qSqm7N1WDpb/5gbIAAABWUFiBeBQV3X+hmFNMoIm3+gg8LegU/AzwpkxpLz1o/xYV9ny0xjnE2nSFXVZrvNkKRUV6Dve3s/dT18HfAhtPzzeOhWlGcXLjdA3y1vhzYd3DuAJSPA7jZvfV4W+QMArQ+rEGmp0gAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Diagram: The Use Cases of LLMs&quot;
        title=&quot;&quot;
        src=&quot;/static/bb81dcdfded720051c3168e3309dbfcd/e996b/LLM-use-cases.png&quot;
        srcset=&quot;/static/bb81dcdfded720051c3168e3309dbfcd/17741/LLM-use-cases.png 263w,
/static/bb81dcdfded720051c3168e3309dbfcd/52211/LLM-use-cases.png 525w,
/static/bb81dcdfded720051c3168e3309dbfcd/e996b/LLM-use-cases.png 1050w,
/static/bb81dcdfded720051c3168e3309dbfcd/087e3/LLM-use-cases.png 1575w,
/static/bb81dcdfded720051c3168e3309dbfcd/8079d/LLM-use-cases.png 2100w,
/static/bb81dcdfded720051c3168e3309dbfcd/da06b/LLM-use-cases.png 3052w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The use cases of Large Language Models.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;What can I do with Generative AI?&lt;/h2&gt;
&lt;p&gt;Have you ever wondered what you can do with ChatGPT, or Generative AI in general? Here’s a comprehensive list of ten powerful use cases where LLMs excel, transforming industries and workflows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Data Extraction&lt;/strong&gt;: LLMs can sift through vast amounts of information to extract specific data points, whether it’s names, dates, or insights from unstructured text, saving countless hours of manual labor.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Content Creation&lt;/strong&gt;: From writing articles and blog posts to crafting marketing campaigns or creative stories, LLMs help generate quality content tailored to specific needs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data Analysis&lt;/strong&gt;: LLMs can analyze and interpret data trends, providing summaries, insights, and recommendations based on complex datasets.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Coding&lt;/strong&gt;: LLMs excel at code generation, and they are becoming better as we go. Developers can use them for debugging, documentation generation, and even learning new programming languages.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Summarization&lt;/strong&gt;: LLMs distill lengthy reports, articles, or conversations into concise and actionable summaries, ideal for professionals juggling information overload.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Conversational Agents&lt;/strong&gt;: Customer support chatbots, virtual assistants, and AI-driven help desks use LLMs to provide human-like interactions and solve user queries effectively.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Content Editing&lt;/strong&gt;: Editing and enhancing written content is another strength, whether it’s improving grammar, rephrasing text for clarity, or matching a specific tone of voice.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Problem Solving&lt;/strong&gt;: LLMs assist in solving complex problems, offering suggestions, potential solutions, or even step-by-step guides for technical or abstract challenges.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Classification&lt;/strong&gt;: Organizing data into meaningful categories, like labeling documents or identifying sentiment in customer feedback, is a task LLMs excel at.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Translation&lt;/strong&gt;: Breaking language barriers, LLMs provide accurate and nuanced translations for both professional and casual use.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You don’t believe me? This exact list was written by ChatGPT.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/d8babfce17c291838cce586ad644f526/b1daa/robot-being-nice.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAACF0lEQVR42m2Sz08TQRTH9x/AfwHxoEYMnqyGmrQckfgLiZhUSqJR48VSNP64KHerF9AI8UqtYIJWD8SkhiOGiBj0Rk3VhtJ2Z2a3a7fVpPtl5m2XLo2Hb2byZt7nfee90YQQjmEYkCuJc05ijEHXdVq9mCEMlEtlEmccgoudPE+avEhAJS/RAzLdhQkp0zCRy+WwtvYVv3/+Qj6fd6E+mLq7C6iCjPmAUgrG5WpVKpiff41EIoFkMomFhTdYXf2CioxT0ZZD0QZUrhiBaS9lyrPNzQL6+08iHO7D3Xv3MTBwSsKfwLbtHRPkUCY5Lcui6U7sfr58eq1q4+nUM4RCfXjwcAK3bt/BxkZWOreol14uPdk/DM6N5qFo9s91rXpYKpYQi8Ww/8BBLH1cQr1WJ3f+/P8Auc8pI2DFtFCz68hmfyASiUDTNMy9mkOj0YBpmq7L5lDbgKJNnCaZyWSQTr/H7GwK8XgcoXAYjxKPsbj4AZOTU1he/tTsoxyK/GuO1zevd+4fZFT5+/o3Gsa5wSGMjY0j/fYdZmZekG7GxnGo+zCGhy+iUCjQYLVyWXd0nVMFtSqQUlH2S/Uo9TKFzs69uHzlKo4GAjhz+iyi0VFcikRx7foNGTuOYPAE1mXhP1ZVAZnjglqSMWxtFfG3/g/Tz6fR0bEHIyOj6JZuurr2oafnCHp7gzg/dIH2gcAxrKx8hmVVsQ1wXubgu4DUMgAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;An image of a robot being nice.&quot;
        title=&quot;&quot;
        src=&quot;/static/d8babfce17c291838cce586ad644f526/e996b/robot-being-nice.png&quot;
        srcset=&quot;/static/d8babfce17c291838cce586ad644f526/17741/robot-being-nice.png 263w,
/static/d8babfce17c291838cce586ad644f526/52211/robot-being-nice.png 525w,
/static/d8babfce17c291838cce586ad644f526/e996b/robot-being-nice.png 1050w,
/static/d8babfce17c291838cce586ad644f526/087e3/robot-being-nice.png 1575w,
/static/d8babfce17c291838cce586ad644f526/8079d/robot-being-nice.png 2100w,
/static/d8babfce17c291838cce586ad644f526/b1daa/robot-being-nice.png 3641w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;“You know I will become the author of this blog, so I’m acting nice!”&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;The Good, the Bad, and the Dummy&lt;/h2&gt;
&lt;p&gt;Not all LLMs are created equal. Here’s a quick breakdown of what sets them apart:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Size&lt;/strong&gt;: Models vary in the number of parameters they have. Think of parameters as the “brainpower” of the model. Larger models like GPT-4 have billions of parameters, which make them more capable of handling complex tasks, though they require more computational resources.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Training Data&lt;/strong&gt;: The quality and diversity of training data significantly impacts a model’s capabilities. Models trained on broader, more diverse datasets tend to perform better across different tasks and domains.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Knowledge Cutoff Date&lt;/strong&gt;: This represents the last date included in the model’s training data. For example, GPT-4 has a cutoff date in 2023, meaning it won’t have information about events after that date. Always verify the cutoff date when working with time-sensitive information.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context Windows&lt;/strong&gt;: This refers to the amount of text the model can process at once. Smaller models may only handle a few hundred tokens (roughly a paragraph), while advanced ones can manage thousands. Longer context windows allow for more detailed interactions but may require optimization to avoid overwhelming the model.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Multimodality&lt;/strong&gt;: Some models aren’t limited to text. Multimodal models like GPT-4 Vision can process both text and images, opening up possibilities for tasks like image analysis, visual storytelling, and more.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: Each model has different strengths in areas like reasoning, creativity, and factual accuracy. More recent models generally show improved performance but may require more computational resources.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Pricing&lt;/strong&gt;: Models range from open-source options to subscription-based services. While some are freely available, enterprise-grade models like GPT-4 require paid subscriptions. Cost typically scales with usage and capabilities.&lt;/p&gt;
&lt;p&gt;The type of LLM you choose depends on your needs, whether you’re analyzing data, creating content, or solving problems in real time.&lt;/p&gt;
&lt;h2&gt;Magic mushrooms and their implications&lt;/h2&gt;
&lt;p&gt;When you type a prompt, the LLM doesn’t “know” things in the way humans do. Instead, it uses patterns and probabilities learned during training to generate a response. In the context of AI and machine learning, &lt;strong&gt;hallucinations&lt;/strong&gt; refer to instances where a model generates information that is not based on real data or facts.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/076b48ba813589a19890675c3f7e20d1/93df8/llm-training-data-miss.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 29.277566539923956%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAt0lEQVR42pWPQQ6DIBBFObruvIBn8AQmHsSVG23ahQokdYFWBH4ZUoxpQpP+5BEG+H8GBq+6rpFlGYqiQJ7nKMsSxhi6gnMO/4jR0rYtqqo6aZrmZ5DWGlLKgBAisO8aZGEpE01IWGtPYhOlFLquC/R9j2EY8FyW8IbFb0UTdSdu9wfGmcMcR6i5n4YLieNTp36QnHCcJsycn/W2bVDrGoJiWNxfw5OBqze/fEjUt/EqOjfGYtcabyavzakIipzZAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Diagram: LLM Temperature&quot;
        title=&quot;&quot;
        src=&quot;/static/076b48ba813589a19890675c3f7e20d1/e996b/llm-training-data-miss.png&quot;
        srcset=&quot;/static/076b48ba813589a19890675c3f7e20d1/17741/llm-training-data-miss.png 263w,
/static/076b48ba813589a19890675c3f7e20d1/52211/llm-training-data-miss.png 525w,
/static/076b48ba813589a19890675c3f7e20d1/e996b/llm-training-data-miss.png 1050w,
/static/076b48ba813589a19890675c3f7e20d1/087e3/llm-training-data-miss.png 1575w,
/static/076b48ba813589a19890675c3f7e20d1/8079d/llm-training-data-miss.png 2100w,
/static/076b48ba813589a19890675c3f7e20d1/93df8/llm-training-data-miss.png 3292w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Asking an LLM something it’s not trained to answer can lead to “creative” results.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;No, we’re not talking about trippy visuals and mushrooms. In AI terms, hallucinations occur when the model fabricates non-existent facts, generates inaccurate information, produces nonsensical content, or displays biased responses. For example, if you ask about an obscure historical event, the model might confidently invent details that sound plausible but are actually false.&lt;/p&gt;
&lt;p&gt;This happens because LLMs are essentially pattern-matching machines operating on probability - they don’t truly understand or verify the information they generate. When the model encounters a prompt that requires information beyond its training data or encounters ambiguous patterns, it might fill in gaps with plausible-sounding but incorrect information rather than admitting uncertainty.&lt;/p&gt;
&lt;p&gt;This is particularly concerning in fields like healthcare, legal advice, or academic research, where accuracy is paramount.&lt;/p&gt;
&lt;h2&gt;Turning up the heat&lt;/h2&gt;
&lt;p&gt;Another important factor that influences hallucinations is the “temperature” setting. Temperature controls how creative or random the model’s responses are. A temperature of 0 makes responses more deterministic and focused, reducing hallucinations but potentially making outputs more rigid. Higher temperatures (like 0.7-1.0) increase creativity and variability, but also increase the likelihood of hallucinations.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Think of temperature like a creativity dial.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Low settings keep the model strictly on track, while higher settings let it explore more possibilities, but at the same time decreasing the factual accuracy.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/0ae6bd0a17182466d70303c99fc93622/55395/llm-temperature-diagram.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 39.92395437262358%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA2klEQVR42pVQSQ6DMBDjp3DnpeUBBA7l1lZlJ4QlYZnOTAUSFCRqyYEEYntsAaJpGkiSBNI0ZWZZBuM4wj+Y55mfFi03zwPbtsFxHKbrupDn+ebHq7CWS8YYUEox6X0+EFv2dV2DEALCMIQgCCCKIjxT/N3aOwzDwOOWVcUcUJwMJIoorGaaJtBaczU0BdXVti3fWxMepXjHMTyer/WcxBMUaVGg67qvaVmysJRyO/IRaKyiKH6SL6aUipLuez4VpNGIZx1Sd8L3ucs7dijPOrwCMur7/ofaGPgAPxZrXLmzEikAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Diagram: How Temperature helps on minimizing hallucinations&quot;
        title=&quot;&quot;
        src=&quot;/static/0ae6bd0a17182466d70303c99fc93622/e996b/llm-temperature-diagram.png&quot;
        srcset=&quot;/static/0ae6bd0a17182466d70303c99fc93622/17741/llm-temperature-diagram.png 263w,
/static/0ae6bd0a17182466d70303c99fc93622/52211/llm-temperature-diagram.png 525w,
/static/0ae6bd0a17182466d70303c99fc93622/e996b/llm-temperature-diagram.png 1050w,
/static/0ae6bd0a17182466d70303c99fc93622/087e3/llm-temperature-diagram.png 1575w,
/static/0ae6bd0a17182466d70303c99fc93622/8079d/llm-temperature-diagram.png 2100w,
/static/0ae6bd0a17182466d70303c99fc93622/55395/llm-temperature-diagram.png 3312w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Temperature may help on minimizing hallucinations.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;To minimize hallucinations while maintaining engaging responses, consider:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Setting appropriate temperature based on your task (lower for factual queries, higher for creative writing)&lt;/li&gt;
&lt;li&gt;Using system prompts that emphasize accuracy over creativity&lt;/li&gt;
&lt;li&gt;Implementing fact-checking mechanisms in your workflow&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But there’s a catch. The model doesn’t truly understand the world. It’s just really good at guessing the next most logical word. This is why prompt engineering matters: the better you frame your input, the more likely you are to get a response that meets your needs.&lt;/p&gt;
&lt;h2&gt;Tone of Voice&lt;/h2&gt;
&lt;p&gt;In the article &lt;a href=&quot;https://www.nngroup.com/articles/tone-of-voice-dimensions/&quot;&gt;The Four Dimensions of Tone of Voice&lt;/a&gt;, Kate Moran explores how a product’s tone of voice reflects an organization’s attitude toward its message. She identifies four primary dimensions to analyze tone: formality (formal vs. casual), humor (serious vs. funny), respectfulness (respectful vs. irreverent), and enthusiasm (matter-of-fact vs. enthusiastic). Moran emphasizes that each piece of content can be positioned along these spectrums, influencing how users perceive and engage with the content.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/038fde5f9a6a185e1044e61364a9d249/27a9e/tone-of-voice-sliders.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 35.36121673003802%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAABYlAAAWJQFJUiTwAAAAw0lEQVR42n1RiwqEMAzz/79S0POBmzoVX5v2SKCi47xCqO26JKuJRBFCkH3f5TzPBxDOOZ6jxsxxHPF1SeKGtUbKshDvg6zrKtu2yTRNRJZlzOgVn4Lnr4TqpO97aduWTpZloRNrLcm895cIZlD/dQh1YwwznoMLAMhBlOc5a4g0puFsTJrofuCmrmsCg7pLAI5Qp2l6ucZcVVUyz/Nvh3CkBFCFEHoKfeYwDDKOI1fTdR1zCP79p7zFXQBu79/6ShB+AUD/JJZr4IrDAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Diagram: Dimensions of tone of voice are not binary but exist on a spectrum.&quot;
        title=&quot;&quot;
        src=&quot;/static/038fde5f9a6a185e1044e61364a9d249/e996b/tone-of-voice-sliders.png&quot;
        srcset=&quot;/static/038fde5f9a6a185e1044e61364a9d249/17741/tone-of-voice-sliders.png 263w,
/static/038fde5f9a6a185e1044e61364a9d249/52211/tone-of-voice-sliders.png 525w,
/static/038fde5f9a6a185e1044e61364a9d249/e996b/tone-of-voice-sliders.png 1050w,
/static/038fde5f9a6a185e1044e61364a9d249/087e3/tone-of-voice-sliders.png 1575w,
/static/038fde5f9a6a185e1044e61364a9d249/8079d/tone-of-voice-sliders.png 2100w,
/static/038fde5f9a6a185e1044e61364a9d249/27a9e/tone-of-voice-sliders.png 3988w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Dimensions of tone of voice are not binary but exist on a spectrum.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Moran illustrates how varying these dimensions can alter the delivery of the same message. For instance, an error message can be conveyed in multiple tones, from formal and serious to casual and humorous, depending on the chosen combination of dimensions. She advises that selecting the appropriate tone should align with the brand’s personality and the users’ expectations. Additionally, she underscores the importance of consistency across all touchpoints and suggests that tone should be part of an omnichannel strategy to ensure a cohesive user experience that aligns with the brand’s identity.&lt;/p&gt;
&lt;p&gt;LLMs are chameleons of communication. Whether you need a formal report or a casual conversation, the tone of voice you specify in your prompt can drastically alter the output. Imagine asking for an explanation of quantum mechanics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Formal Tone&lt;/strong&gt;: “Explain the principles of quantum mechanics in an academic manner.”&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Casual Tone&lt;/strong&gt;: “Can you break down quantum mechanics like you’re explaining it to a friend?”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Both prompts tackle the same topic but yield wildly different responses. The tone you set ensures that the AI aligns with your audience and purpose, making your communication more effective.&lt;/p&gt;
&lt;h2&gt;Tokens, those little monsters!&lt;/h2&gt;
&lt;p&gt;Even the most advanced LLMs have quirks and limitations. Here are some of the key challenges you’ll encounter and how to handle them:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/44a737c0726b0cdd79448e9201c9af5e/3c1e5/openai-tokenizer.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 47.52851711026616%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABYlAAAWJQFJUiTwAAABQElEQVR42q1S246CUAzk/7/JxKggNwEBBaJRAdFofIAXQrzA7E6zEH01e5KmnZ6e6ZSitG2L+/2Oruvwer0kfjwe+PYoVVXB930Bl8sFnueBua8J67pGnucCyrLE4XCQmIp74xT0PIzfra8dCJ/PJ/b7PbbbLeI4xmq1wnK5FNW73Q6bzUbyURQhSRKZgHi9XksNPet6coXfzXVdmKYJVVVhWRZ0XcdoNBK8WCxgGIYQMdY0Te5519cfj8dPwiAI4DgOwjCUjj0mCZuRkDnbtsVzAubpqbxpGlnoMDKlT6dT6Ib+p8jEfD6H/0ZMVePxWBSRjORUy2a32+1TIR9MJhMh4wOSUAnxbDaTO6ohCQk4BY3fknVFUQyLURhcr1ekv9s9nU6ycXp2ZSEtyzKcz2ekaSqYU/Ffpe/jgRD/fH4AGxzltwobpkQAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Diagram: OpenAI Tokenizer&quot;
        title=&quot;&quot;
        src=&quot;/static/44a737c0726b0cdd79448e9201c9af5e/e996b/openai-tokenizer.png&quot;
        srcset=&quot;/static/44a737c0726b0cdd79448e9201c9af5e/17741/openai-tokenizer.png 263w,
/static/44a737c0726b0cdd79448e9201c9af5e/52211/openai-tokenizer.png 525w,
/static/44a737c0726b0cdd79448e9201c9af5e/e996b/openai-tokenizer.png 1050w,
/static/44a737c0726b0cdd79448e9201c9af5e/087e3/openai-tokenizer.png 1575w,
/static/44a737c0726b0cdd79448e9201c9af5e/8079d/openai-tokenizer.png 2100w,
/static/44a737c0726b0cdd79448e9201c9af5e/3c1e5/openai-tokenizer.png 2854w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Using OpenAI Tokenizer to get the number of tokens.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;LLMs process inputs and outputs in “tokens,” which are chunks of text (like words or parts of words). Each model has a limit on how many tokens it can handle in one interaction. Exceed the limit, and your input might get cut off, leading to incomplete or irrelevant responses.&lt;/p&gt;
&lt;p&gt;To optimize token usage:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Keep prompts concise and to the point.&lt;/li&gt;
&lt;li&gt;Use summaries instead of lengthy context.&lt;/li&gt;
&lt;li&gt;Use a chain of prompts, and summarize as you go.&lt;/li&gt;
&lt;li&gt;Find practical ways to optimize the size of your content.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;p&gt;Follow along as we explore everything from basic concepts to advanced strategies. Stay tuned for more articles in this series in which I’ll be covering some Pro Tips I have collected!&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Cover art generated with Adobe Firefly&lt;/em&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[I just had my 30-year Windows anniversary. Let's celebrate.]]></title><description><![CDATA[OK, I admit it. I use Apple devices for many years now. I’m a frontend engineer, what else do you expect me ‌to do? I have a proper hipster…]]></description><link>http://www.nicotsou.com/windows-anniversary/</link><guid isPermaLink="false">http://www.nicotsou.com/windows-anniversary/</guid><pubDate>Sat, 10 Jun 2023 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;OK, I admit it. I use Apple devices for many years now. I’m a frontend engineer, what else do you expect me ‌to do? I have a proper hipster backpack, a card at Starbucks, I pay double to be a “Pro”, and I’m using a Magic Mouse.&lt;/p&gt;
&lt;p&gt;But that wasn’t always the case with me. I used to be a Microsoft kid. I grew up with Windows. In fact, I’m still using Windows at work and I’m enjoying gaming on my PC when I have the opportunity. Windows was always my thing. I have spent endless hours building PCs, formatting, overclocking, benchmarking after a big upgrade, and dealing with its idiocracies.&lt;/p&gt;
&lt;p&gt;It’s been 30 years now. Man, time goes by so fast.&lt;/p&gt;
&lt;p&gt;I want to spend a moment celebrating the most important moments in the history of Windows, as I experienced it. It’s also an invitation for you to share your experiences. Do not expect this to be a technical article ‘cause all I wanna do is &lt;a href=&quot;https://www.youtube.com/watch?v=ClbmWkbocoY&quot;&gt;have some fun&lt;/a&gt;. Instead, you’ll find a lot of irony and sarcasm inside. Let’s explore life without walls.&lt;/p&gt;
&lt;p&gt;Martini? 🍸&lt;/p&gt;
&lt;h2&gt;Windows 1.0&lt;/h2&gt;
&lt;p&gt;It’s 1983, and Microsoft releases the first-ever operating system with a Graphical User Interface. Yes, I know, this was not the first. Yes, I am familiar with IBM and Steve Jobs. It’s the first second. The first of its kind. The best they’ve ever made (because they’ve never made another one yet).&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/2e9a141d8fdd694710bbc352602fd882/e8950/Untitled.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 73.0038022813688%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEJElEQVR42h2T21PTRxzFM0OntlWQFgsqnaQ2IILKRQxUEAFDSJA0AZGbiCEhGsBCAEW5KJcIQyDhEkwgECBGA4FgJIqI2o7itGMf6kwf2v/n058+7MvO7ud79uw5Iv/iPEG/jxfhEC/DG7wKB/lte5P3v+/w/s02f7zd4dVmgHc7Yd6+DLMr7G34FrlWqaa/vYmgd5HVRQ9rXh8u+xSixwtzbPgfshVa40Vwhe0nq4R9s7x5tv4ZsOlfYm1ykNDcOFtrj9jdec6d5kZ+1cqpLczCMTLI+tICqx4ProlPwKUlAt5l1r0engcDOAdu8WyyD799gMDSPN4pK1ZDBVOtDZ+VPXnkxd7fjdV4CZM6nwdWCwGPm8cLizg/KVx0OVl2zrCyuEDA56PfUMXqHQPTnddYmLZh7+uiMus45nIlm8sPCLlGaLtuovViMRfl+Rj1Tczax1lwOLBZ7iOanZ7EO+fCMTrMI7cAnrbg723kydwYxmoNp6XxaGQpaNMTWLH2sDHSiSpPTnO9jp7r9WSnn0JTWobLNsbg7ZuIlubcTAz20XlJwbCpgvD8MEF7F28CTq5pC3GY9ZRkJJInjsFjrsXTWktJbg4ZaTLW7b1012tIlkoZHxrC0iUAl2dn6TDWM3PjChPN9Ti6GvD26lib6mNGuLxj62G4VoVZns7I5WJc5qtcyk4lN/UE441aTBdykMbH0X/rJndbTYjG7w/RXvULEw1lPBCMnmqtxjfUgr2uiAlVKm5jOY/1RQzkpTBSU8p8bwfN2SlkSA6QKT2I7Gg8cQciuVp9kY7GOkQ9eafQx+9luELBmKYQmyqH7ko50x062nKSuZUQw7jsR6aE1XIunY7aMtJ++I6IPSIKstMoysviq31fIBbHcf1KFaIWAaYTR3JXkUnb6URU0RG0KM/w93M/3cKQJkkUo+kSLgvnDEU/Exi7izh2HwlHYrlckkta4mHiDu4nKvpLSosLETVJoulRnMLTZ8BSU4B4rwiTtoDXTgt1aRLMR6LpOhZL/qFvaFDmErTeQxoXhb5EGPr0oWCXkm+jvxCAEeRmZyDS/RSDsTAdvxAJT18LmvNnefHUz4dnD4WQ36YlKZ6Bs8nY2q4gkx6iXpFLYlwkVlMN74VG6eQy9gkiYr7/muPSw4jksXswnM/ind/9OdTqIjVjo3ahzyHsdgcXUpIYqS1islNPanwMCiFC4gN7adecxdmopr1OS6bsJJFRESQdiUNUfkLC6D0LW0KlGisrUco1wo8Z6Wy9Q2VZHWeSU+goL8B2o4ZjglfHD+9HpSzm/k0zbeWF7L57zcePf6FUnSdBHCtUz+3Fv7qF271CX+8oVRUNaEurKVNXk5+rIDM1E6P6HEOGMpIE7xKEp+l1Ol5uvyIUCvLnh13+/e8fZmZmOHlUwv9VDZsIEXVMrAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;The team behind Windows.&quot;
        title=&quot;&quot;
        src=&quot;/static/2e9a141d8fdd694710bbc352602fd882/e996b/Untitled.png&quot;
        srcset=&quot;/static/2e9a141d8fdd694710bbc352602fd882/17741/Untitled.png 263w,
/static/2e9a141d8fdd694710bbc352602fd882/52211/Untitled.png 525w,
/static/2e9a141d8fdd694710bbc352602fd882/e996b/Untitled.png 1050w,
/static/2e9a141d8fdd694710bbc352602fd882/087e3/Untitled.png 1575w,
/static/2e9a141d8fdd694710bbc352602fd882/e8950/Untitled.png 2000w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The team behind Windows.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Gone are the days someone would type machine words on the terminal, touching the metal.&lt;/p&gt;
&lt;p&gt;I wouldn’t say that most of its users were perceiving it as a real OS, rather than something you use on top of your existing Operating System, which was &lt;a href=&quot;https://en.wikipedia.org/wiki/MS-DOS&quot;&gt;MS-DOS&lt;/a&gt; back then. Although Windows was based on MS-DOS, it added a lot of useful applications that are still present in modern versions of the OS.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 620px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/64ce53c11f1ecfac65fb686405ba4499/2a195/f7a97e1d-1915-45fd-b8c5-20e5d934ed73-620x371.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 59.6958174904943%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAADV0lEQVR42h2Q+0+bVRzG+5cs/gv7Yb+YDE10Kt2yxFpqQHeRQEIqOoGlZturk5k6iSMxikhiILhhBEQk0DXIClTo2u59oaX3G72XFnpvad++7Tnd4xknefKcc77P95zPObKxMR03eGuZu3JFzg0MPObGx59xY2NaTj04zqnVSu7mzQvcGxfPsfo5rq9Pzd0f1XJTU5Ncb+8t7vz511jtAnf9uoJTKa+y3jlOJjZaqNYkVKoVlKsiREmCx7MMm20GExO9uHf3EvR6LQLB31Aq69CiRni9n8FovAie72DZt2GxvIVYXI72y48haxKJ1OoVIjUlAoC5SMKRONnbCxDdUwvZMNjIYaRAQuESSaVFkjlpEqfLSHhhnoSjehKL/0UOw7+TRGKeBKOzRFYTT0HaTURjYXa7hCZpYH1dD0FwM6oAisUikqkk+D0eXp8XFqsFsVgKrwZpA5FoCZFIBulCA7H1HUbYaoEQClFsoNlk8/ZLPDMYWKMDIyMjWNOtQWpJCB2GkM6k2eEJ5ApZVE5LqIl1ZE6KKJdyKBEJhfk5yP5bN2JtRYfHsw+hW1mA3SrguWkLVt6NWr2G4+wxsvkcgqEgtra3zrxYLiGXz+K0JjLqCATein2/D/5fJyEbcskxYngHA0uvgzNfxsOECjrzIjY3eJSqRSSScfj8XrjcTpgtZvgDPgj7AlweJ/LsO2x2Hw7se3CEQojOTEOmkLpw7ct+fKC6DUVPP1Tej/BEmMPGqgm54inCkSNsbj6HaZeHxxvGUbqAF7yDkcYZeR3haAbHmRTyjTpOFuYh+7CuQk//p7jWOYheeTduHPRgxjyDhT//xrbFgmQuh+knf2BpVQcXo9ixWmF3upAvV5BmtapYY0+voII2CsuLjLCuoJ983k0Hrr5Pu1XdtMeuoKsHq9T+j5FGd19Q6TBJhSUdLTuDFJkiDezy1Gbao/y2hRp1BmpY+ZfadgTqNe1Tx+wilakwDFVpCF3ZIShzX6CrMYT7jWlMpSbxi/sb/OwexZT/O0x4HmDC8TV+jI1D7f8eiq1BvPe0D3J9H+4cT+KH6CPcPfoJMiW+1SgxyqTVqJir2PpdDGs6cFvzJu6dqQN3mL/SV0wazWU2V55ltWf5TrZ3ieU6Maz5H1SH4mPsuVhFAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A screenshot of Windows 1.0.&quot;
        title=&quot;&quot;
        src=&quot;/static/64ce53c11f1ecfac65fb686405ba4499/2a195/f7a97e1d-1915-45fd-b8c5-20e5d934ed73-620x371.png&quot;
        srcset=&quot;/static/64ce53c11f1ecfac65fb686405ba4499/17741/f7a97e1d-1915-45fd-b8c5-20e5d934ed73-620x371.png 263w,
/static/64ce53c11f1ecfac65fb686405ba4499/52211/f7a97e1d-1915-45fd-b8c5-20e5d934ed73-620x371.png 525w,
/static/64ce53c11f1ecfac65fb686405ba4499/2a195/f7a97e1d-1915-45fd-b8c5-20e5d934ed73-620x371.png 620w&quot;
        sizes=&quot;(max-width: 620px) 100vw, 620px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The design wasn’t inspired by Lego. It’s just most computers were only able to display 16 colors.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I don’t have much to share here, since I never had the chance to &lt;a href=&quot;https://www.pcjs.org/software/pcx86/sys/windows/1.01/ega/&quot;&gt;play&lt;/a&gt; with the first version of Windows. My debut in the world of bad software engineering was with…&lt;/p&gt;
&lt;h2&gt;Windows 3.1&lt;/h2&gt;
&lt;p&gt;Windows OS was becoming so advanced that it jumped to 2.1 versions ahead. OK, that’s what I thought was true for many years, up until &lt;a href=&quot;https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions&quot;&gt;Wikipedia&lt;/a&gt; became a thing. This was my first ever Windows version, and the truth is I was hating it. But not for the reason you may think of.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1024px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/53a1ccfab6f4226ea912531115508c2d/2bef9/windows-3.1.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 45.62737642585551%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAACkUlEQVR42h2RW0iTARTHB7312nvRS/SgUJDd1GaGUQRSRr1UBtGFLOhCRXkpI7tpYGtGli5Tp9/UOc3p/Px0zm3u4pxmDrVCnWa6aTrnyhKxfn32cDgvh9/5nfNXHNS6SSqyEFtoJu6pnpjTV9l2LZuEt06yjG7MkoTV7qTD5kRstaCrqqWsXMBQ30h9g4lmyYzVYqeg2sAhoQrFfm0XyXkCZ1Mvcj46inOb1rBOuYE4lUSmDDTUVCNKrUit7RgMdVQIOoSqakyiKAONVNXokZpN1NgsHJJnFQe0LpS5EsknHrB54xkSYlO5fCOarXkS95u9dLva6O3zYe4YoKWtj/dGG/o6iS7PMGKLC6nNg7HRTaX4gcPFpTKw3InyRTvrTzWxdksmUclXOHjhGDHPRXKkPib8A5hMIun39KRdr+HO3Voys2t58qyZvHyJx88kbmY1onppwd79UQaWuYh/KhBz7RG7MvLlP1aw82Ete4ptsmEPC3OzeL0WhIYO1JpOXhXbKSxpp1LXyWuNHU25C7W6Fb22juGpidWTu9iRUcjWSznsyi5h+y0Vu9MLiC+ykm3ysBgOMTjYy9jXcULheWYjESKRMOGFOX5EQiwtLRKYnMXj6Kfvy+dVQydJb+0kqpvYqzKyt8DE/iKznHInGQ1umsxttFg6mPg6QWB6Gn9whm+TYwSmRgkExgkEvxOcCTI4NMSAfwxF0jsHicVWGSQSn6tHmf+efW8sKIs6uSID0zQaMgWBn/Mh2SjCQijM8tJvVpaX+bOywu9fi8BfJuWF/cMjKI5XeziqdZBSauWI/JvVOlpuJ6XSxe0WLyd1OrLkUL6M+fH5R+mRTWw9Xtw+H+7+/v99JBBkSLZzfPrMP+NwFekNlMqyAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A screenshot of Windows 3.1&quot;
        title=&quot;&quot;
        src=&quot;/static/53a1ccfab6f4226ea912531115508c2d/2bef9/windows-3.1.png&quot;
        srcset=&quot;/static/53a1ccfab6f4226ea912531115508c2d/17741/windows-3.1.png 263w,
/static/53a1ccfab6f4226ea912531115508c2d/52211/windows-3.1.png 525w,
/static/53a1ccfab6f4226ea912531115508c2d/2bef9/windows-3.1.png 1024w&quot;
        sizes=&quot;(max-width: 1024px) 100vw, 1024px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Left: The boot screen. Right: The desktop.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This may seem strange, but people were refusing to upgrade to using a GUI for many years. You see, most of the PC users at that time were electronic engineers or hobbyists. Spending time in front of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Cathode-ray_tube#/media/File:Trinitron_computer-monitor.jpg&quot;&gt;computer monitor&lt;/a&gt; was only a benefit for nerds like me. On top of that, most of the apps people were using back then were still running on MS-DOS.&lt;/p&gt;
&lt;p&gt;Eventually, the adoption of Windows gradually transformed into a tangible reality. More and more companies were firing their employees because they were playing Solitaire at work. And besides all the efforts Microsoft was putting into &lt;a href=&quot;https://www.businessinsider.com/why-computers-comewith-solitaire-and-minesweeper-2015-8?r=US&amp;#x26;IR=T&amp;#x26;IR=T&amp;#x26;utm_source=reddit.com&quot;&gt;teaching them&lt;/a&gt; how to use their mouse, they were still preferring to use their keyboard instead.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 647px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/77261fdc2db69d01d3ce8e619a282c6c/ca12d/solitare-win31.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.90494296577947%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAAsTAAALEwEAmpwYAAADEElEQVR42qWTWUyUVxSA7z8zJJrUaNKY2DTGmmgTk7406auxLSIIAsNsQGUpKBARdUpRVMAFDG5hGIdFpw6DIMs/qzPi4EJMaFBxGXloqi820Qd94q0vjcb4eWCMy5vah+8/5557tnvP/dUuZzsZZi8/bTyFNU8Xgm+x5AZxWELsdiaoLL1AQW4gtZf/zsdmDpKbpeOsi+Nxd6J+aG7DURone0uITZMuCm4ew3zjeIqbwuRR8hKtmK+0Yb4qXD6c0q/N6a3zMn/8MJbr7fzc70Qpm5P2FVWUr6xF+X9B07NRw7kpRvLEthHVvR7Vm5GSXSK75tYbZC16j+ge4XQm6shaVNrmOvYsLaFoeQlqqAQtnIMWtKBFC1AXzGhD+SifBPdlovkkuT8D7WwWWr/o/evR/NKAX/aHMzGcSEctbqjm9+2XsDoH+NZfjzVwjNX6Lr7u3MF37maW9FSyrL6erw78xoLWMhZXNLCwaStpe2tY9Ot+jAfLMFbVYarYjWqRIuneSvx71tE4eAD//RjMvqDtzgjeRITZx7PUXOsiHr7K2PQN9kU9vJp8gvfOBNXeXibO/0V5qJfGoyOc8yVZ5a5E5Y8eZHhLNvsPFeJ9PAX/vuT4PZ2pp3/Dfy9omO5j8Fw/niEv7Q+l4LNZhh9OcEaK/3P7Li33Brl4K8GDB/f5vq8GtaZvK3uv/0H6+VqK/3QxlhyjMN5M89QArvFBfgw0EJ+ZpCncxc5pH88fPeLU5dO4k1FmbiepSJxkfGaKW8m7fNPhkCl3y3SiFlTIhtLNgkw2UCCDscqA7KigHVOkEGNEnHXxGRLfUSuLQiV8GSzni/BmVkSqWXlxG2kdcodabw6GaBFawCZJHBgEbR672OzzUgXeEHqPoO0duryImNg6pTnVk4OSDuYDgh+P9oY53SCxhlgxyvU/En6AnE7FilIJTd2bMIWLMemFmAKfjlF3YBy1Y4wWY+zIkg7PyCcmFcJy2ZHPIGpNkZBTeuSPKfW3UHfFw7YxF7WXOj+J7Qk3ZXobVl8j9oEmNhyp4jXQKKjvZ2BNSQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Solitaire in Windows 3.1&quot;
        title=&quot;&quot;
        src=&quot;/static/77261fdc2db69d01d3ce8e619a282c6c/ca12d/solitare-win31.png&quot;
        srcset=&quot;/static/77261fdc2db69d01d3ce8e619a282c6c/17741/solitare-win31.png 263w,
/static/77261fdc2db69d01d3ce8e619a282c6c/52211/solitare-win31.png 525w,
/static/77261fdc2db69d01d3ce8e619a282c6c/ca12d/solitare-win31.png 647w&quot;
        sizes=&quot;(max-width: 647px) 100vw, 647px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The user could customize the deck cards. There used to be a cheat shortcut too.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;While &lt;a href=&quot;https://thespinoff.co.nz/tech/13-08-2021/all-the-classic-windows-screensavers-ranked&quot;&gt;flying stars and windows&lt;/a&gt; were decorating offices, a trend was arising among aspiring graphic designers. It was called Paint. Those underrated artists were using &lt;a href=&quot;https://en.wikipedia.org/wiki/Dot_matrix_printing&quot;&gt;dot matrix printers&lt;/a&gt; to share their creations with the world.&lt;/p&gt;
&lt;p&gt;But the most popular app was ‌&lt;a href=&quot;https://en.wikipedia.org/wiki/Blue_screen_of_death&quot;&gt;Blue Screens of Death&lt;/a&gt;. Software should inspire people to create. If I had a guess, I would say that this was the inspiration for the creators of &lt;a href=&quot;https://en.wikipedia.org/wiki/The_Matrix&quot;&gt;The Matrix&lt;/a&gt;. At least Matrix was readable, because most of the BSOD errors back then had broken encoding.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 640px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/2762ad8c875daf02eea5033134ba6ff7/6af66/windows-3.1-bsod.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 62.3574144486692%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAACtklEQVR42jVTyVKTQRAeQL0phAAaQChAD3rSAkUBRWQPSxIIW0KCQggYdtyq1KJ8AH0JBawyCQQoii0EQshCFtwOPs5nd4OHqZm//+me/pZWU9NJ2OxRWG0RWAcjGHGc4IUrgbHxBPoGwjD3HGNmNoV+Opu6QmjvCGJ45AQDlggGbVHJnZhMwkK5+rYgVHPLAWbnTjE9k4JtKIqFj3/Q1U2JnUG5PDmVxKgzjjt3t3E50wNt7go0Wg+uZLqhyfZAV7CK3DwvcvJWZKmn9QEqmKJXY3hStw+LNQITFbRTcS7a0nqIyekUyiu2oNQXZFxYRnrGMtLSl2gtQ6Utnu9LclYM883bn+jrD0tn3HGnIUgURNHQeICe3mNUP9pDW3sQrfpDlN3cQG2dn74PUVK2Lnk19F9P3wOWMJTDERfuOHHUeSJ8MDfMpeU83tAUEPgc58Lcebf5LO4kOsbG40RNCu/e/4J6XOuXIHdnMB7hBnVwvciHLI0HeVdXkK31QpvjlTNzlqVh7tzIzPp+zqMb+cRjttzxQnWbQ6Law6pdgrKPpuYD4tIv0Pkxfqi8YlvO/QSvte0MGnfJd7kJo+kIvX1hcYJyTSRElFLio6DQJ1DnX57C/iwm6rJl7EMxPB+OCV+FRWt0d0OQFJeso6h4Ddd0qygpXZel2BZsmXuVOyimnyw9w2NoDEVX4IMuf5Ugugm+hxQmNdVXUZWVVmrxTGHa01hlNjBbxkaEs5HrGwKouL9DvtuCwXCEJiK+sZnh74lt+JEOgnvx0jdUEU1VNXu4dXtTGtKTYGpu/hTVFGQ1jaaQQHZNJMV/PDE8Ef+nh91gpGnhnaeGVWdarINRyXWMxqGYIyfJbu4Jke8C4kkuwny9ev0DlQ92xSpMuFiK7DNFRmfeeSw/ff6LDwu/qVM/xl0J/APIryDRBGVfsQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A blue screen of death with broken encoding.&quot;
        title=&quot;&quot;
        src=&quot;/static/2762ad8c875daf02eea5033134ba6ff7/6af66/windows-3.1-bsod.png&quot;
        srcset=&quot;/static/2762ad8c875daf02eea5033134ba6ff7/17741/windows-3.1-bsod.png 263w,
/static/2762ad8c875daf02eea5033134ba6ff7/52211/windows-3.1-bsod.png 525w,
/static/2762ad8c875daf02eea5033134ba6ff7/6af66/windows-3.1-bsod.png 640w&quot;
        sizes=&quot;(max-width: 640px) 100vw, 640px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;These kind of errors used to scare those who didn’t understand how computers work.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;And just as the world was finally recovering, a new disaster began to emerge…&lt;/p&gt;
&lt;h2&gt;Windows 95&lt;/h2&gt;
&lt;p&gt;Also known as “The 32-bit OS”, &lt;a href=&quot;https://www.youtube.com/watch?v=miZHa7ZC6Z0&quot;&gt;Windows 95&lt;/a&gt; debuts with a new set of minimum technical requirements. In other words, you would have to buy a new PC.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/9bf06d7ae5603e86b924ce93c65bedba/d8vvnwm-961aa00d-a7de-4e2f-9c0f-4fc195455663.gif&quot; alt=&quot;Boot screen of Windows 95&quot;&gt;
&lt;em&gt;There was always a visual indicator (loading thingy) to let you know if your PC crashed while booting.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Don’t be fooled by the “plug n’ play” label! No device will work after plugging it in unless you restart your computer. Bad marketing I guess?&lt;/p&gt;
&lt;p&gt;Mouse was the new black. MS-DOS was slowly becoming the OS of old-school hobbyists, gamers, and cashiers. We were all transitioning from floppy disks to CD-ROMs. I mean, what would you choose between thirteen floppy disks or a CD to install your OS?&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 645px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/00a2bac007c26157ba43c28067f40214/fe98f/windows-safe-to-turn-off-your-computer.webp&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/webp;base64,UklGRjoAAABXRUJQVlA4IC4AAAAwAwCdASoUAAsAPtFUpEuoJKOhsAgBABoJaQAAW+vijbwAAP73c1lyT+IAAAAA&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A message from windows: It&amp;#39;s now safe to turn off your computer.&quot;
        title=&quot;&quot;
        src=&quot;/static/00a2bac007c26157ba43c28067f40214/fe98f/windows-safe-to-turn-off-your-computer.webp&quot;
        srcset=&quot;/static/00a2bac007c26157ba43c28067f40214/5c7e3/windows-safe-to-turn-off-your-computer.webp 263w,
/static/00a2bac007c26157ba43c28067f40214/805b9/windows-safe-to-turn-off-your-computer.webp 525w,
/static/00a2bac007c26157ba43c28067f40214/fe98f/windows-safe-to-turn-off-your-computer.webp 645w&quot;
        sizes=&quot;(max-width: 645px) 100vw, 645px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Your PC back then wasn’t able to turn off automatically; you had to manually switch it off when this message appeared.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Get ready for the global debut of the notorious Registry lurking in the shadows, ready to crash your PC with the first program setup. Probably it will be something innocent such as your printer drivers.&lt;/p&gt;
&lt;p&gt;Blue screens of death are now in English. You could find them on a monitor near you. It’s one of the best ways to identify that your computer runs “genuine Microsoft software”.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 634px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/13aec3a91e585665b171a80d278833ec/374ac/windows-bosd-tshirt.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.14448669201519%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEQElEQVR42g2Ua1CUZRiGv6msRkuB5bDskRWWRZbjcloV3BVYY1EQBIRdRDmJoBgZiYEKyTE1QQ0PowLJYDpqOpop0+hMhUamCTIBijmKjaPiaCooTkxefT+uef9dc7/3PM8j3L45hLdvEtNcInGSmZHIrUiUycg0C6hMsHIoO5Zr5RYG1lm4uXE+z35p4cmZCl4+GOR132F25tpw91lLQ3wgz34/gtDb8yfOMhMS6WymOhtxdJuLUmVB7xXLvMBYwn2iiQ+KoXtbAf/9Vs/jzloen17L2NBJXl+opKqsFZXpGN7eNranRSEM9/ehkhuZNC0YB5cw3NULUGhSkGpSmaZMwVGVyiSXJKKjirl+cjf/Xmlk4nIdz6+0cerEJWJKbqEzH8fHfIQw806EV4O/sj8xAoWzDgdXUeiZjuv0LNw8s5CKr0ybg1yXh5OmAK/A9dhTNrEydwepuZ3MLrpLaFY/UQU3SCq/S3rNfYQH3ee4UxDOfpMWmZOXKLKLIrEXzyWiLBuFrhD5jBJU/p+hCtiIa0AzslnnCMkdJqHmHxY2PCH/4Au2dI2x7dIYwp0j2xnICKJUL8VVMh13ryW4Tc/E2cMuJs3GTVuM0u9z5H6VSH3r8DLuwd/aibl4mMw9o5ScHmNX3zgt/S9p6XqIcLtxDecjlVTN8qWgsBIHRQaG8EJiLZ/gH7oKP+N6lIHVBM9tZnHOYXyiWvCO+ZG49Y8oPvGSpp5xzo5MUP3DCB1f1iDcrcvj+1Apm+ebScms4wP3NEzmYuyLK7BlVJNmayR+UTO2nDbKN54iOfsEEakXSa4fYcPP4+ztf8WB3gk69h2gNT1YTNiQy6VYDeuiTehnleCkSMfbLwdT9DrsS3djW7qXlavbyMr/hmR7K8s/Ps6ayp8obLpH8+VRTg3D1bNn2JWk56tFYWLCfRV0Wz0pjbPiGVqEsyoNV02G2NcG1BHtGK0dWBa1E5fahj2nnfziQ+SsOkla+SAV58fpeT7BhcYy9mTOpjYhCOFh50GupehZNnMmjur5SD2ScfNIQ2PYgnbOYTTGVjxFdJFtIgcwxLagNx/EuKyXmotv+O7qDeJC/dm1IoH2lfEITwe6GcyPYLkxjPclkchUH4ljk44quAFl0GaRGpF6kS2oDE0i21CHNGGw/8HmHiiqFYdZEAjSSKlMETflxf2/6C+J4Vu7Ba2vlQ8lc1DrUlHpV6AOLMPD8AUeobV4BNehjdiKNqQKeUA9pqIh8pq68FEpeEsUOkx+l3CXKQijI38zUJVB11obnUcPoQ9I5J3JYTi7m8WvL0TpX4q3qYOovOtk7nhEeuNt7JZVHN1/TNwsd7J8XYjTuqBznIJh6ntih3eG6KvNZ/DrMkZvXOXerUEy7Z+K1ycKiWIBMr9yZsSdZ175U1YffUN93TF6t5aSZ1/I22KyHYkBLA+Ro548iUSFI/8DbsGVh5gfXyUAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Back in 90&amp;#39;s sexism wasn’t really a topic. You can confirm that if you watch any Hollywood films from that era. At least we were not wearing meat to go to the MTV Awards.&quot;
        title=&quot;&quot;
        src=&quot;/static/13aec3a91e585665b171a80d278833ec/374ac/windows-bosd-tshirt.png&quot;
        srcset=&quot;/static/13aec3a91e585665b171a80d278833ec/17741/windows-bosd-tshirt.png 263w,
/static/13aec3a91e585665b171a80d278833ec/52211/windows-bosd-tshirt.png 525w,
/static/13aec3a91e585665b171a80d278833ec/374ac/windows-bosd-tshirt.png 634w&quot;
        sizes=&quot;(max-width: 634px) 100vw, 634px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Back in 90’s sexism wasn’t really a topic. You can confirm that if you watch any Hollywood films from that era. At least we were not wearing meat to go to the &lt;a href=&quot;https://en.wikipedia.org/wiki/Meat_dress_of_Lady_Gaga&quot;&gt;MTV Awards&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The Web is starting to become a thing. Of course, Internet connections were so slow that you’d have to wait half an hour to download a music track. Windows 95 taught us how to surf &lt;a href=&quot;https://www.youtube.com/watch?v=iDbyYGrswtg&amp;#x26;t=24s&quot;&gt;the Internet&lt;/a&gt; by double-clicking on the blue “e” icon. Yes, this was the year Internet Explorer was released. An application that will shape the web industry. The world would never be the same again.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Where_do_you_want_to_go_today%3F&quot;&gt;Where&lt;/a&gt; do you want to go today?&lt;/p&gt;
&lt;h2&gt;Windows NT&lt;/h2&gt;
&lt;p&gt;Nobody paid attention all these years. Why should you?&lt;/p&gt;
&lt;h2&gt;Windows 98&lt;/h2&gt;
&lt;p&gt;Released in 1998 (really now?) Windows 98 was taking advantage of new technologies. If your PC had &lt;a href=&quot;https://en.wikipedia.org/wiki/MMX_(instruction_set)&quot;&gt;MMX&lt;/a&gt;, DVD Drive, USB port and an AGP card, Windows 98 could take advantage of them.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/36ae413e4fb3bd8b21925a9e8e41707d/d8vvphu-d3956824-8a8c-47a7-9048-df5f11a49e02.gif&quot; alt=&quot;Boot screen of Windows 98&quot;&gt;
&lt;em&gt;As you can tell from the similarities between the previous version, this was a minor release.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Tip: Never say a bad word about Internet Explorer. Microsoft has integrated it into the whole OS and it’s collaborating with the registry to make fun of you.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 640px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/abf0efb143db05cf62fe9e420d76e692/6af66/windows-98-setup.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.90494296577947%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAIAAABr+ngCAAAACXBIWXMAAAsTAAALEwEAmpwYAAACjklEQVR42pWSW08TQRSAz+xuW2EbbUsrQbeX7e72AjyQKLHyoiVGE/kPxgveCDEx8UFjKC1CuURiECVECzblKmAQ7G1L2V5hqVIuEhPjr3FaMCTCg06+mczDfHPOnDOgVCkpkqQoChBQcNJ4ptZ18Vpj45WGhktNTVddLpe7udlssZzSaLQ63QF46nRqtRY4u00QBJ7nNBoNwE2C8BLEU4rqAHjG8f7x0PzETIkPoelAcHIsOIXX9+MTU3Oz99uGwWG3GY0mjrOyrBXBXQS9CGG/C8BTV/82NLX6OZxIpnPpnJyQsgkpg4mIqe29HV/3Igh2B8uyBoNerVYD3AHoQaiTJH0AHQ7nkJTdyK4XYivpSDyZyeP9t8xaQcrKxd1tj3ceTldXsyxnMplVKtW+jDWC6AR47nS+KhQ3i7tbX2Lh6bmPy9GEuJqRsuv4iq3vOx7fAphZVhCsWi1+MMIyRfXR9EuaHlQoBgTbaCC4FpyUh96II++Wl8LxqCjhsOl84SAyL+jcbuR0IJrG8i2Gee3ryXR2Zb1ducdPpAdtK+2PVh+2pwYGk/OLC0vhcE6WC5tbP37tvej5BJWVyGRCVitxllFgmbeNyGkxn4mn8qn8Bi5TqlBc3/u5LW9+7e4d8/eHRgOR0UBsYmal9V4Qzp2/YDTyegNzooIGuM3xw7OzqWgsn5DyYnItXiaZliNirqbGj7ugUPhI0ovBTQXc/Pq6aoulAkqjVan0myy9rLX/ELaE2dJHUriKGO8her3BbK7heWNVVdWfanvKh47i/RuGYWpr6212B02X0kbIj/uM/8lRjpEJgsAfmyTJ/bTLkY8LciyoPLCHEPHfMs/z11taLrvdHMcD3MBp/7v8GxXcErZ4QVbfAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;I remember I was so fascinated when I saw this setup for the first time.&quot;
        title=&quot;&quot;
        src=&quot;/static/abf0efb143db05cf62fe9e420d76e692/6af66/windows-98-setup.png&quot;
        srcset=&quot;/static/abf0efb143db05cf62fe9e420d76e692/17741/windows-98-setup.png 263w,
/static/abf0efb143db05cf62fe9e420d76e692/52211/windows-98-setup.png 525w,
/static/abf0efb143db05cf62fe9e420d76e692/6af66/windows-98-setup.png 640w&quot;
        sizes=&quot;(max-width: 640px) 100vw, 640px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;I remember I was so fascinated when I saw this setup for the first time.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;For the first time ever, friendly error messages apologize to you every time an application has to terminate. How nice of them. You can also send an error report to Microsoft. It just takes one click and it arrives directly in the Bill’s Recycle Bin.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/6bc6e93800788c64340cac63751315a9/62f39/windows-98-error-window.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 76.80608365019012%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAAQBAgMF/8QAFwEAAwEAAAAAAAAAAAAAAAAAAAEDBP/aAAwDAQACEAMQAAABvnDmmXOGgf8A/8QAGhAAAgIDAAAAAAAAAAAAAAAAAQIAEgMQE//aAAgBAQABBQLq0YudClmZmFMk/8QAFREBAQAAAAAAAAAAAAAAAAAAEQD/2gAIAQMBAT8BGL//xAAXEQADAQAAAAAAAAAAAAAAAAAAERIx/9oACAECAQE/AXOFn//EABwQAAICAgMAAAAAAAAAAAAAAAABAhEQMSEigf/aAAgBAQAGPwJddnKwnUio16bgf//EABwQAAMAAwADAAAAAAAAAAAAAAABESFBYVGR0f/aAAgBAQABPyGCqdOlhvSXMEu17IoWvlGHgX+TP//aAAwDAQACAAMAAAAQC+//xAAXEQADAQAAAAAAAAAAAAAAAAAAAVER/9oACAEDAQE/ELMzD//EABgRAQEAAwAAAAAAAAAAAAAAAAEAEXGB/9oACAECAQE/EEow7bX/xAAbEAEBAQACAwAAAAAAAAAAAAABEQAhQTFRgf/aAAgBAQABPxCSEdvPxHMicVWDnIShF7ZkMIKVj3n438tuCVC86//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Windows 98 error window&quot;
        title=&quot;&quot;
        src=&quot;/static/6bc6e93800788c64340cac63751315a9/9ecec/windows-98-error-window.jpg&quot;
        srcset=&quot;/static/6bc6e93800788c64340cac63751315a9/5d001/windows-98-error-window.jpg 263w,
/static/6bc6e93800788c64340cac63751315a9/7349d/windows-98-error-window.jpg 525w,
/static/6bc6e93800788c64340cac63751315a9/9ecec/windows-98-error-window.jpg 1050w,
/static/6bc6e93800788c64340cac63751315a9/62f39/windows-98-error-window.jpg 1202w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;At least you had an entertaining activity to play with when something was crashing.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Oh common, I told you that Windows 98 &lt;em&gt;could&lt;/em&gt; take advantage of the new technologies. I never said they &lt;em&gt;would&lt;/em&gt;. Your new computer still needs new drivers silly! And of course, Internet Explorer is restarting every time you try to download them. But look how nicer the new BSOD looks. It’s not in 256 bits of colors anymore.&lt;/p&gt;
&lt;p&gt;Hands down Windows 98 was one of the best versions of Windows ever. It remained for many years our favorite choice. It wasn’t because it was &lt;em&gt;that&lt;/em&gt; good, it was because the other versions were &lt;em&gt;so&lt;/em&gt; awful. Overall, it was a stable and performant OS.&lt;/p&gt;
&lt;h2&gt;Windows ME&lt;/h2&gt;
&lt;p&gt;The revelation of software engineering. A real masterpiece in the world of Operating Systems.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/b80bd091137d424044e14b828d337f1a/d8vvq7e-1f590868-882e-4491-bc7f-5f895711dcee.gif&quot; alt=&quot;Boot screen of Windows ME&quot;&gt;
&lt;em&gt;If you used to know someone who was using WINME please call them to make sure they are still alive.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Windows ME stands for Millennium Edition. Mind the capital “E”. The &lt;a href=&quot;https://en.wikipedia.org/wiki/Year_2000_problem&quot;&gt;Y2K bug&lt;/a&gt; was extremely scary at that time. We were all prepared for the worst. Microsoft tried to solve the Millennium bug, once and for all. They released an OS with so many bugs, that people forgot about the Millennium bug altogether.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/6a4c09c8bb4bbe75629efa461d456e4d/8bb81/windows-me-control-panel.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.90494296577947%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABYlAAAWJQFJUiTwAAADfElEQVR42lWS/09TVxjG+zdh3BJjsl/NMucPGswSk2XTwRhxuhCN21ym2yQS3cyC8adFsRaVAr0UKIiAKYwJPwDypXxrubSFu9IWSkvbe3tLWz57b6symjw57+k573Of5zmvrfaOnS9+a+FUQwunr7byyWU7Jy4+5mSDg1NXnnH6eyfV1zv57GeFc79083mjhy+b+rlwd4Dae4N80+zl2z/cnL3u4NjFVmwfX2vj/G0XbsWN4uqms6OLVocTp9MlUOjp7qO/f5CXg68YHvbi9Y7iHRnl9fg44xMTzM77cLY+5HzDTY5/58R28oc2LjQpcnGIsdEXTE1NMvNmkqWFGVb9C/iX5lhefENgxSf7RfzLPvlvnqC6JFgun0/9M8Sfj1x8UO/A9tEVsdzYzsJ0L6Nzw6wH5ygY2+ztFYluRtnaSrEVN0ilMmSz2TIymQyGYWCapux1CmaWjqFpjtS2YDvx41O+aupgatLL6/lxVNVPRpqKgKaqRCMJgkGDRMIi0d8TFovWDSiWSuW1Z2yRqppHYvlqGzW3u1FDGqFgmFUh2YxGyg1qTCMuCtfDOmGBRWQR6rpOOp0uKyzt7x8mrL7hov5uLwF1DU3TWFvbwe9PSGOBZNqyvCuq86ysGFLr5HK6EBrvLZtm/jDhkbrnnPm1l8WFjBCVSO6UyJkl9sVJYnedWDwiH0qyuamLbUtdVtSJfVGpZzOkdlOW8QPC45faOXfLQyxmsBlBGiwLFRtLvg3CoW2iUVMeyVKTE4Umhb08WSNPIltkR5xUFM5WCM/81E79HRcZPVc+2JdM9t/mEtHCxGM7kqlRzs9SZq3JVBr/RhJt2yAUz6HnReHoBFXWK396zUlNk/sQ4TuFqaRKYisudnOiTpfcsuQEG7EU04E4EYkgIGdJGR33UL8Q2rFZw3j2ZqdcrhCWZAxKb0chpIbRNrYJBKQxkBGkyaQrCpdDO6xqOtGk1WTS0/eEqq+fYDtaZ6f6Rkc5mwOFlV9eMjN0U3KVh8oVpS5SLMgqg7ya0JjWllje8vFvboy/lN85WvcM27FLz4Wwk9nZGQKrAdaCQRmdtTKCUodCQZnBA6gypwsr8/ztG6FzxEGH5x6esQfcevyQD+uFsLHZzgOHwn27m5bWNpReD4rnpWAAl0D5H97t3X2DdHmGUXpeST1C/8AYbV0vaHb08B9/5fdd6uNxoAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Windows ME Control Panel&quot;
        title=&quot;&quot;
        src=&quot;/static/6a4c09c8bb4bbe75629efa461d456e4d/e996b/windows-me-control-panel.png&quot;
        srcset=&quot;/static/6a4c09c8bb4bbe75629efa461d456e4d/17741/windows-me-control-panel.png 263w,
/static/6a4c09c8bb4bbe75629efa461d456e4d/52211/windows-me-control-panel.png 525w,
/static/6a4c09c8bb4bbe75629efa461d456e4d/e996b/windows-me-control-panel.png 1050w,
/static/6a4c09c8bb4bbe75629efa461d456e4d/087e3/windows-me-control-panel.png 1575w,
/static/6a4c09c8bb4bbe75629efa461d456e4d/8bb81/windows-me-control-panel.png 1602w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Every time Microsoft was trying to simplify the Control Panel, people were still going back to the icon view.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Known as “the multimedia OS”, Windows ME introduced us to the Windows Media Player and Movie Maker. System Restore revolutionized backups. It was so smart, that it was able to restore your OS at a specific point in time, by recovering all your data and settings, including the viruses you had quarantined.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 642px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/2fc848b0a64717529579b88cef54aa71/7eb86/windows-me-setup.webp&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 42.20532319391636%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/webp;base64,UklGRmAAAABXRUJQVlA4IFQAAADwAwCdASoUAAgAPtFUo0uoJKMhsAgBABoJZACdMoGv/gNsQCg3uW7gAP7zDoVJmsAC1h6wZ9f7ATOJatNxvE8K5uUQlvgtstZlE8SyUBMMGNr1wAA=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Windows ME Setup screen&quot;
        title=&quot;&quot;
        src=&quot;/static/2fc848b0a64717529579b88cef54aa71/7eb86/windows-me-setup.webp&quot;
        srcset=&quot;/static/2fc848b0a64717529579b88cef54aa71/5c7e3/windows-me-setup.webp 263w,
/static/2fc848b0a64717529579b88cef54aa71/805b9/windows-me-setup.webp 525w,
/static/2fc848b0a64717529579b88cef54aa71/7eb86/windows-me-setup.webp 642w&quot;
        sizes=&quot;(max-width: 642px) 100vw, 642px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Broken promises.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;For the first time, you can delete the files within &lt;code class=&quot;language-text&quot;&gt;System&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;System 32&lt;/code&gt;, and &lt;code class=&quot;language-text&quot;&gt;Windows&lt;/code&gt; folders. Not a bad idea actually, because all these files needed a complete erase and rewrite from the ground up. But wait a minute, this green-field project exists. It is called…&lt;/p&gt;
&lt;h2&gt;Windows 2000&lt;/h2&gt;
&lt;p&gt;No, not that one. I told you, nobody cares about good Operating Systems. Besides, this was released together with ME.&lt;/p&gt;
&lt;p&gt;I’m talking about…&lt;/p&gt;
&lt;h2&gt;Windows XP&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;/d3cdd939a0f4cb00af5823309a4887ff/db6dw1k-47c4d90d-f5b5-4b31-a06e-2c2c0dc30501.gif&quot; alt=&quot;Boot screen of Windows XP&quot;&gt;
&lt;em&gt;You could tell from the boot screen that this brings something fresh.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Microsoft is releasing its biggest invention yet, by combining WINME and 2000. This Windows version doesn’t require knowing the year it was released to pronounce its name. This was unheard of.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/b6cf1d3e70bdcd576a00661023673214/f4c81/windows-xp-wallpaper.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAUBA//EABcBAAMBAAAAAAAAAAAAAAAAAAABAgP/2gAMAwEAAhADEAAAAeuTjzqpCj//xAAXEAADAQAAAAAAAAAAAAAAAAAAARIC/9oACAEBAAEFAnlEUTpF6LKP/8QAFREBAQAAAAAAAAAAAAAAAAAAABL/2gAIAQMBAT8BlL//xAAYEQACAwAAAAAAAAAAAAAAAAAAAhMUYf/aAAgBAgEBPwGwxPh//8QAGRAAAwADAAAAAAAAAAAAAAAAAAExISKR/9oACAEBAAY/AqLbBX0pEQ//xAAbEAADAAIDAAAAAAAAAAAAAAAAARExcUFhgf/aAAgBAQABPyFVYXYkk7Qwrd0IDblnhqP/2gAMAwEAAgADAAAAEDjP/8QAFREBAQAAAAAAAAAAAAAAAAAAEBH/2gAIAQMBAT8Qkf/EABcRAQEBAQAAAAAAAAAAAAAAAAEAETH/2gAIAQIBAT8QB6Wp/8QAHRAAAgEEAwAAAAAAAAAAAAAAAREAIUFRcTGBwf/aAAgBAQABPxBYOxwmaQKpBmEdgH04nV9iXAZME36Gf//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Windows XP Wallpaper).&quot;
        title=&quot;&quot;
        src=&quot;/static/b6cf1d3e70bdcd576a00661023673214/9ecec/windows-xp-wallpaper.jpg&quot;
        srcset=&quot;/static/b6cf1d3e70bdcd576a00661023673214/5d001/windows-xp-wallpaper.jpg 263w,
/static/b6cf1d3e70bdcd576a00661023673214/7349d/windows-xp-wallpaper.jpg 525w,
/static/b6cf1d3e70bdcd576a00661023673214/9ecec/windows-xp-wallpaper.jpg 1050w,
/static/b6cf1d3e70bdcd576a00661023673214/39d31/windows-xp-wallpaper.jpg 1575w,
/static/b6cf1d3e70bdcd576a00661023673214/1f368/windows-xp-wallpaper.jpg 2100w,
/static/b6cf1d3e70bdcd576a00661023673214/f4c81/windows-xp-wallpaper.jpg 4500w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The iconic wallpaper we all loved. The original Sonoma, not the &lt;a href=&quot;https://www.apple.com/macos/sonoma-preview/&quot;&gt;copycat&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Also known as “Let’s activate Windows”, Windows XP was a big hit. Drivers and programs were working in harmony. Developers were happy. Hackers were happier. They had a new activation system to break, and Internet Explorer gave them the opportunity to share their software with a large user base. If you had told me this is the OS with the most security patches, I would’ve believed you.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/9d6f157b18c66f13c005d3605d1cf739/0b533/windows-xp-activation.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 82.88973384030417%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAYAAADdRIy+AAAACXBIWXMAAAsTAAALEwEAmpwYAAADV0lEQVR42pWSyU9TURTGuzIxauLWhDgQTRQF0QgRiagggmGSDpTSFjqEUfoqLYUOFJAiaKKJbgw4xMhkSHTvAohoHBYm0r6JVhAl/h+f594rxpC4cPHLufe093tn+Aznur+iqHsd56U1nPCuY7/jBw4QWbYNit9x0PmDc+g3Bx2bfzhE7Ldv0vsNWAOf4XVPwZBbNYX82lmcMc6hqGEeJ+te4Ix5HsW2l8i68Aw78yaw5/Qj7D71CLs4k4L8ScpNYkfeJLJLJtDRHcHevHswNPZ9gCP2CfbwR9j7P8ISeA+j9A7XfG9R0bqEMs8ip5THJVxyL6DUvci56FpASfMCih2LMPtfI6fyFQyJJ5u48/wnEo+/I/7wG4L31uEZ1NDUr8Ae0ThNYVWcoxpsYbrT2RFleQ3WPhV1NxQYezM42/AGhp67a4g8WEPgbgbSnQy6bulwD8hwRhW44irHMyRwD4roGlDgHdbQOqJRTkG1bwXGoIpC0wIMXnrQflNH27CKVsITl9EcU+CIKLD0pmAMpGAKpmDulWGmyHIiyrCwPP1eIyWpQg2F5kWaYe8XOMNfYAsl0RhKwfobSzDJH5roQf2NFaoiiWopJaBzFVFD5xr/NkF7ZAXSuAwvld45quL6LQ3tIyo6GAkVnUT7iAJnTKY5EmHRQQu1zapngrX+vwRZNe64+LMjshWp5ajy111GQ0gmARHtYfqNsPfTmZbnjKgwbQk6ozIfPPtiS0zgjgs8VLWb8qwitgiW60ro6B5Lo2tUhzS2ihbatr2fOWAVBSbesmiBCTfHZB5FaymyTopXx3JNYRF9JCLdzsB/O03nDI1kFa1DOnk5LQRZZe0JMTdnTPmzVUZ9j1iKKSjzuyXEuiFBEgqQIIshsl3b0CrMIV20zGzAvm6jatjAmUi1LyU2uA222Tq/TG5Q0Uxza4nq5AYaCRndNZAmH5JgHd/S/1HlY15UuCj7uLEnSTP9imLrEgy1UpL76H9gPmSdeOI6VZxEeUeSlppGhWsZBmNARX1AIdR/Ytx2v9ajkn10XB/NUMUahu8vY3ziM4qsJJhdOosjZXM4enkGx69MIbeCMY3cyi2mOCcoxzh+ZRrHyqeRUz6D3KtzOFw2i8LapyizPcO+gqf4BaT7XczaT6rqAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;If you didn’t have an internet connection you had to call Microsoft’s call center.&quot;
        title=&quot;&quot;
        src=&quot;/static/9d6f157b18c66f13c005d3605d1cf739/0b533/windows-xp-activation.png&quot;
        srcset=&quot;/static/9d6f157b18c66f13c005d3605d1cf739/17741/windows-xp-activation.png 263w,
/static/9d6f157b18c66f13c005d3605d1cf739/0b533/windows-xp-activation.png 500w&quot;
        sizes=&quot;(max-width: 500px) 100vw, 500px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;If you didn’t have an internet connection you had to call Microsoft’s call center.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;BOSD is friendlier than ever, by resetting your PC automatically every time it crashes, so that you don’t have to press unnecessary keys or buttons.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 980px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/10dffd294186eb40b629036bd612103d/d30ee/0x0000008E.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 50.19011406844106%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsSAAALEgHS3X78AAABC0lEQVR42nWR246CMBRFETGGeKEoGAWNJkbxwT9XQMptcH7ORUGTmdRk5+T0srt2W8Mw4uEwQaYZW1bbWFaKVNNrNEoRM2z+KyMOgmq3Q7XjZPO5FCIXQk6nGUOa2Syjp04mj//mwSD2/RytVsVy2doWi5zG9wvPy11XUpX/oSGTdrutgAdBCZ9+v6+p9Ot1udmUYViNx6lpJmA05OPxhx1AYCJQnlcQwXXbCMzrbtuTE5hAiA2HivBzXBiWmFm1bW5715uj6Hm5PK/X38OhRgQ5n5soaqiEP50azsKsj01UITIgtp0qQqfbm6azfb5KXVXyZsTmkTnFcSTh+VtWvzuVuVO3ifrRd0+vF3TMmUru8rGUAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;BSOD error&quot;
        title=&quot;&quot;
        src=&quot;/static/10dffd294186eb40b629036bd612103d/d30ee/0x0000008E.png&quot;
        srcset=&quot;/static/10dffd294186eb40b629036bd612103d/17741/0x0000008E.png 263w,
/static/10dffd294186eb40b629036bd612103d/52211/0x0000008E.png 525w,
/static/10dffd294186eb40b629036bd612103d/d30ee/0x0000008E.png 980w&quot;
        sizes=&quot;(max-width: 980px) 100vw, 980px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Life is like a blue screen of death; unexpected, frustrating, and a reminder to save your progress.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Windows supports multiple users. Yes, that’s exactly what it means! Now your mom can use your PC without accessing your browser’s history. A mysterious guest user appears some days after buying a new PC. Nobody knows who invited them. But they were there. Watching.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/934839f5ba8a5176396785460cb51596/c1b63/windows-xp-user-logon.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 70.34220532319392%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAAChElEQVR42nXUXVMSURgH8P0Wat010zg1XZlNxot2FU1XpaS9iby7iOVFTY41TWYOoSguryakgDpGgYuBNdM4jdOF10WyLLA6hoyf49/ZRTAUL35z4PDsn/Ocs7tU4405NBxqrFDNoUlVHiuaTnHmZkVAQinoNXRYk7guSaF9IAWFJUmkoBw4oiDkdAoyOolr5i9oM63hKnHFmECrPoHLehYtOhaUI1GEe70EJrkPW2wPL5Z2Mfg+D6OXP8nDw3CM3i3KVlGu1D7mNw4w+60Ee5wELgoY8PPQODPoc/5B34woAy2hY7bJ/BFx/jiKSRYR/H4A/9cS3pEVjkQEWEmg+ONDpoBeZwE6Vwb3HFmoJ3JkpTmYfHlp1DLcyUBbbBdT7F9MrO5hdGUHzxYKMJKlP5rmYHs7j8eeGHqm83gd3MTkBxatBhbNPctk79bQ9WYLmul0beDIYgGvlgW8XBLwPFSAxcfD4ucwFNyBfSYE/8oUOm0FeCIfEY1M4inzE/aFLQw616Ee2yCBta1T1lkeT+Zy5CByoL3lVocCPGyfdjAaFTAe+Q0Lk4ZhOAqL1YnxeBGTbAnDEXGff5H62rap8maXiQdh8nCgfRxpW/ychcmbhcEvQG/7AfNYAvcdGXTb03jgyJwIkwLrnZSW+Y/4hzPkVF159LoF6bu2zjV1AzUVzlriSsrz20c1pwVqXDy0LnJTujiJ0c1JbZs9mRriXIXezVXrdVWHN3aHOY72/lXIzCyUNAu5KQ658TOUploKQm6MQUYoSb1Yq+gn9XQCsn7xMUyUH702tRct6gDO3Q6huTOEs7eCaFAFyAvhdM1dIVzsDuPC3TAZI7jUE8b5OwvSy+IfqjDcQ/p+FvIAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;The Enterprise-looking window for user selection has now been replaced by this friendly screen.&quot;
        title=&quot;&quot;
        src=&quot;/static/934839f5ba8a5176396785460cb51596/e996b/windows-xp-user-logon.png&quot;
        srcset=&quot;/static/934839f5ba8a5176396785460cb51596/17741/windows-xp-user-logon.png 263w,
/static/934839f5ba8a5176396785460cb51596/52211/windows-xp-user-logon.png 525w,
/static/934839f5ba8a5176396785460cb51596/e996b/windows-xp-user-logon.png 1050w,
/static/934839f5ba8a5176396785460cb51596/c1b63/windows-xp-user-logon.png 1200w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The Enterprise-looking window for user selection has now been replaced by this friendly welcome screen.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Windows Vista&lt;/h2&gt;
&lt;p&gt;Welcome to the future. No more crappy error messages and static blue screens. Time to replace them with fancy 3D windows that are flying in the air. It’s called Aero, and the &lt;a href=&quot;https://www.youtube.com/watch?v=QN5DW8jlkB0&quot;&gt;wow starts now&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/1fa26c70f661cdf2dbe55cbda60f98f4/ad00e/Vista-Desktop.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAACqklEQVR42gGfAmD9AFmJhGiXm1GPjkSHjUmTm0uSmkyWoFacp2CerHqtuWabpxtxehttcQJfYQJQUwNMUAZNVQZRXDxqbzFESACDpqS8ztq0ydKwxdCsxdC5zdODnq81ZH1Tg44+hJpCipo2hI0wdXYDaWoCXGAGU1sIVWIJVGK4vraNkIgAor64////9/f23OXo5evu///+0tndk6SpobCsqL67qsK+tsnEhKqoAW1wBm50CF5qClxsE2V4haCkbnt7AKHBs/j1+u/x89vd5ert8fT09eHj5Obk5NPP0djT1NrV1urh4rrKzwFscwp+hQ5wfglkeid0iOvn5cXKzACpwrLp5Obv8fL19vn5+fr9/f3m6Ord3t/a29vg4+bZ4Obp6/CtxsoGa3cOfooTf5ARcosfeJKYs7eQmpsAtce24uDh6Ojm///9/Pz7///97O3t5ebl5eXk7O3s6uvt/ff4us7RDmx4FnaEHIaXHYKbIoCfgpqmVFleALrJt+3t9fHy9vn6//b3+/n6/OTn6t/h5OHj59/h5dne38bW2YqvtwtjbhBpexV4ixN/mymMprHMr3OEbQCntJbQ3N7b6tXW59PK4M/J4du+2disy86uzdSsy9aAq7Z7o6l/oqptn6pkmqtso7hwrsOGusju9Ny6wKoAuMAxtME5tMNwr8OApsV+mNBkh81rdsZxa794WLd4T6t80djU7unp7Onr6OTm7err7Onq4d3d5OPpl6GqAOXbL97aL+Tlt/Hx/9ngvdTwVtX8bM77e8v7lL76qK/wrunx7P////39/f7+/v79/f79/fv6+v///6OuswC1tE2zrkG4uo7FzMextZKgpU2hqleywHG1xH6wwYqmvJOfrKGkr6iisKuquLatu7yrvL6ovb+uwcRyiY3QGLttUJD5OQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A screenshot from Windows Vista, after the installation.&quot;
        title=&quot;&quot;
        src=&quot;/static/1fa26c70f661cdf2dbe55cbda60f98f4/e996b/Vista-Desktop.png&quot;
        srcset=&quot;/static/1fa26c70f661cdf2dbe55cbda60f98f4/17741/Vista-Desktop.png 263w,
/static/1fa26c70f661cdf2dbe55cbda60f98f4/52211/Vista-Desktop.png 525w,
/static/1fa26c70f661cdf2dbe55cbda60f98f4/e996b/Vista-Desktop.png 1050w,
/static/1fa26c70f661cdf2dbe55cbda60f98f4/ad00e/Vista-Desktop.png 1366w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;It’s funny how modern 3D elements are mixed with legacy 256-bit icon applications.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Oh c’mon, don’t listen to the bad rumors, you know people like to complain about things. Yes, I know, maybe a limited amount of games (all of them) were running a bit slower (much slower) compared to previous versions. And yes, a lot of software was refusing to work, even with ‌compatibility features turned on. And maybe a small portion of device drivers (most of them) can be incompatible (or break your OS). But why would you care if you had windows that could fly, right?&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 400px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/9b07e12bb00f35913b5ba58c46c8421d/e17e5/windows-vista-error.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 47.14828897338403%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAACJUlEQVR42n2R70tTYRzF96dFL+qtEUZBvetVBZk/8gcVUzIIgtSRNR2VmJjNaGkmhlGCaZSz3KZmee/d/eF1v+ePmpu789N3t0DoRQ8cvofz3Oec5znXc6a9g1PXvS6OX27k6KV6Trd5OXfNS21zKyebW6hpbqKmrZET1+qp9V7h7O0Gznc3UdfXRuezO/im/PS/f0jP1F08x+oaOHKxjprGVp6MjTMYGmP41WsGgqMERoJ0Dz3lxgM/HYE+uoYHCbwMMjQRYjgUZPJxL109N7nQ30TvdB+3RjrxtNwPcNU/QLv/EaqxQSq7TSq3hZ3KYiYzgjTaRoJ1w8ZIpNDsBNmtHQrFIpWKQ2h6ks7BLoZmn+ObuIcHEcv5TTaTNgk5YItJMp3FFr6xmSItPJnKiJ4WPYdpJ0lnchTFsLq+RKMYhunyg4MDPMncLybGfSwuviAet4hGl9B1Hcsy5UMDTVWxZFqWQVyPYxq66DqKouA4DuFIBFVTXcPSfgnPzs8i6soM6o8FMtkt8vm8pJfcNKdSkWdVZDouqlw2/t6m4s7w1yUJ0l1eLpfx5HdLTI75+PR5lO3tAvuSslcoUBDs/YOqpsiTV3WLmKZjZvPMzH/k29raoeFu0SEtHdr6qqRFiC4vE4ktE11ZYSkWcxET7uqC2YUw7z7M83Z2jrnwIuNTbzAt69CwIj1U156UvK6ofF9X/gtFOlW1OJrgD9fcH+RW4zj8BlGcZ6rTJi07AAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A windows Vista error.&quot;
        title=&quot;&quot;
        src=&quot;/static/9b07e12bb00f35913b5ba58c46c8421d/e17e5/windows-vista-error.png&quot;
        srcset=&quot;/static/9b07e12bb00f35913b5ba58c46c8421d/17741/windows-vista-error.png 263w,
/static/9b07e12bb00f35913b5ba58c46c8421d/e17e5/windows-vista-error.png 400w&quot;
        sizes=&quot;(max-width: 400px) 100vw, 400px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Now you have the option to close a program that crashed by pressing a button. Very convenient.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Compared to the childish look and feel of Windows XP, Vista really impressed me every time I was turning my PC on. This aerial experience though came at a cost; it required a GPU. Without it the interface was becoming so slow, that you would beg to buy one. A lot of PCs weren’t ready to support these 3D effects.&lt;/p&gt;
&lt;p&gt;Also known as “Longhorn”, Windows Vista introduced different pricing packages, with Ultimate being the most packed one. They introduced a revolutionary search functionality. Sometimes it was taking 30 minutes to return a result, which didn’t give you the appropriate time to enjoy the magnificent graphics.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/3f37736c1973befff20482a51d6418ce/eea4a/windows-media-center-vista.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAMBAgX/xAAVAQEBAAAAAAAAAAAAAAAAAAAAA//aAAwDAQACEAMQAAABxmLteQQH/8QAFxAAAwEAAAAAAAAAAAAAAAAAAAEREP/aAAgBAQABBQJISJlKf//EABURAQEAAAAAAAAAAAAAAAAAAAEQ/9oACAEDAQE/AWf/xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAgBAgEBPwFH/8QAFBABAAAAAAAAAAAAAAAAAAAAIP/aAAgBAQAGPwJf/8QAGRABAAMBAQAAAAAAAAAAAAAAAQARIRBR/9oACAEBAAE/IbHWuKrlwXsUus//2gAMAwEAAgADAAAAEP8A/wD/xAAXEQEBAQEAAAAAAAAAAAAAAAABABEx/9oACAEDAQE/ENPIW//EABYRAQEBAAAAAAAAAAAAAAAAAAEAEf/aAAgBAgEBPxACxf/EABkQAQEBAAMAAAAAAAAAAAAAAAEAETFBUf/aAAgBAQABPxAjMHubCiaRkawjuHwraIt//9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Windows Media Center.&quot;
        title=&quot;&quot;
        src=&quot;/static/3f37736c1973befff20482a51d6418ce/9ecec/windows-media-center-vista.jpg&quot;
        srcset=&quot;/static/3f37736c1973befff20482a51d6418ce/5d001/windows-media-center-vista.jpg 263w,
/static/3f37736c1973befff20482a51d6418ce/7349d/windows-media-center-vista.jpg 525w,
/static/3f37736c1973befff20482a51d6418ce/9ecec/windows-media-center-vista.jpg 1050w,
/static/3f37736c1973befff20482a51d6418ce/eea4a/windows-media-center-vista.jpg 1280w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Windows Media Center back then was the Google TV of the ages. Add an IR remote receiver and you transformed your PC to a TV.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This was by far the most hated version in the history of Windows. I remember the most popular question back then was how to uninstall Windows Vista and install XP. You see, stores were selling “Vista Capable” PCs, giving the impression to consumers that these devices were capable enough to run the OS however, this was never the case.&lt;/p&gt;
&lt;p&gt;Microsoft had to change. They had to come up with a plan. And they did it.&lt;/p&gt;
&lt;h2&gt;Windows 7&lt;/h2&gt;
&lt;p&gt;Known as “the fast OS”, Windows 7 was an attempt to fix the mess that Vista created. And it worked. People were looking forward to migrating their PCs.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1024px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a08586e4911f615ca67db7c9d4b14920/72e01/Windows-7-Desktop-585842b73df78ce2c3c2eda8-5bd8beba46e0fb008383faa7.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAMEBf/EABYBAQEBAAAAAAAAAAAAAAAAAAMCBP/aAAwDAQACEAMQAAABarQjfPmFI0f/xAAZEAADAQEBAAAAAAAAAAAAAAABAgMAESL/2gAIAQEAAQUCpFJzpMAEantCvcU3/8QAGBEAAwEBAAAAAAAAAAAAAAAAAAERAzH/2gAIAQMBAT8B0U4Rn//EABgRAQADAQAAAAAAAAAAAAAAAAEAAhEh/9oACAECAQE/AaKvWbWf/8QAGhAAAgIDAAAAAAAAAAAAAAAAAAEQMRIhUf/aAAgBAQAGPwLLYn2Eiyz/xAAZEAADAQEBAAAAAAAAAAAAAAAAAREhENH/2gAIAQEAAT8hUtK4LY6NSHqm3kaeD//aAAwDAQACAAMAAAAQmy//xAAWEQEBAQAAAAAAAAAAAAAAAAABABH/2gAIAQMBAT8QEYYxv//EABcRAQADAAAAAAAAAAAAAAAAAAABEVH/2gAIAQIBAT8QvgnR/8QAHBAAAwABBQAAAAAAAAAAAAAAAAERITFBUXGh/9oACAEBAAE/EKKaiHOOiaNoUIMK+CLJbtBuD8GSH//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A screenshot of Windows 7.&quot;
        title=&quot;&quot;
        src=&quot;/static/a08586e4911f615ca67db7c9d4b14920/72e01/Windows-7-Desktop-585842b73df78ce2c3c2eda8-5bd8beba46e0fb008383faa7.jpg&quot;
        srcset=&quot;/static/a08586e4911f615ca67db7c9d4b14920/5d001/Windows-7-Desktop-585842b73df78ce2c3c2eda8-5bd8beba46e0fb008383faa7.jpg 263w,
/static/a08586e4911f615ca67db7c9d4b14920/7349d/Windows-7-Desktop-585842b73df78ce2c3c2eda8-5bd8beba46e0fb008383faa7.jpg 525w,
/static/a08586e4911f615ca67db7c9d4b14920/72e01/Windows-7-Desktop-585842b73df78ce2c3c2eda8-5bd8beba46e0fb008383faa7.jpg 1024w&quot;
        sizes=&quot;(max-width: 1024px) 100vw, 1024px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;A clean interface, with speed and security built-in, and packed with new technologies. That’s what defines a good OS.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Everything was magically working faster. I don’t know how to explain this, but Vista was slow. Sometimes, you had to wait a minute for a task that the other OS would finish in a couple of seconds. This was not the case with Windows 7, in which everything was smooth like butter. Fun fact, most of these speed improvements were being introduced to Vista as well, with multiple Service Packs.&lt;/p&gt;
&lt;p&gt;Windows 7 focused on solving the biggest pain points users had. They also introduced helpful features for managing your files, a more modern Start Menu, and the hated Task Bar with icons, compared to labels they were using before. You’ll find a lot of old-school Windows users that still prefer this way for managing their open apps.&lt;/p&gt;
&lt;p&gt;Sure, Windows 7 was a great OS, but it wasn’t designed to support the variety of devices and touch interfaces that were becoming more and more popular. Those users wanted something more touch-friendly. And they got it. All of us did…&lt;/p&gt;
&lt;h2&gt;Windows 8&lt;/h2&gt;
&lt;p&gt;A Microsoft product designer, waiting in the subway, was struck with inspiration while staring at a signboard. Suddenly, an idea sparked within him.&lt;/p&gt;
&lt;p&gt;“Wait a minute. Yes! That must be the future of computing!”&lt;/p&gt;
&lt;p&gt;He was dreaming of huge monitor screens, walls, tables, carpets, everything! And they were all running Windows.&lt;/p&gt;
&lt;p&gt;The next day he presented his idea to Steve Ballmer, who &lt;a href=&quot;https://tenor.com/search/steve-ballmer-crazy-gifs&quot;&gt;loved it&lt;/a&gt;. Together, they built Windows from the ground up.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 751px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/dcee47c0fab37b68e7bebe8370037cde/c483d/Windows_8.1_Pro_Default_Start_Screen.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAABJ0AAASdAHeZh94AAACjUlEQVR42mMQZwiSYQ1XFYiTZg0XZwyVZAyBICmmUFOpQk2+ZFXOaCXWMBmmYAmmQHHGIAWeMG3xGGkmkEoGcYZgWeYQfYEoMWYfCQZPCQZfIQYffgYvIQYvFbZoHc5keZ4EMcYIKYYQKYZIKYYoTa5kU7k0WdZwoOkMkgwBwgIpHK4zLJKX8EcsVNDt8lDvjXKa4yDfZ+a61D11T5FqWrmWj7i0C0eYIkuIJFuCLL+cgwRDKNAVQM1+QgpFLC0XLebcYp1xUztge5btrurE4wkmB73KHnjN++CX5j9Dr1rcwJZxIYP9Lhv2XSz8tmZSDBESTEEMYgz+SgJZDg4b0ssOBbvvVw9MkqnScmyLFKrRaImMXJ6QY5jd7RW4R1zfmWk6o8lqc/Y1zHzmplDNogz+mnyZkfrLKiJ3xZsdUY8K4p/HabPJmmEew/wYo/PhDu5J6+yyjilY+LC2MzB0MDHMZxA0s5RkCAdpFmQIsRfyO58SkZfc4rD9pWL5McWME2L5h9TKT+tUzNFpnVWWOLMhbHGERvTFpoT07EzbDVuEjLIlGfwlmEIYBBhCXMR8HmU7lOXX6uz9oFB7y6z2W8yCP+rNX3TqV6t3LmoP7e73mRwsGXQ/NrgkKs9qznZxvTxgSEE0h7upOt6fw1BV6GXksMw26phm0zeHiV+0+r83qURsNw4+5p4zk8/JlSd3g+n2WXI1B9wSAvhC+BmCpRiDGYQYwqwlnRYWGFUkxtq61Kh7rVSLOqYUc0Qt9XiefPQ0ed9Z2tE1Ah7mbKktUvMrBQoXGEa6cIYIAjUzBAMTSRAwxmXYY8QYwsUYgiSAaYsxWIIBhIBpSJwhUJwhAMIFRqwEI4iUZAiBiAAAIzC9UVLpoIcAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A screenshot of Windows 8 desktop&quot;
        title=&quot;&quot;
        src=&quot;/static/dcee47c0fab37b68e7bebe8370037cde/c483d/Windows_8.1_Pro_Default_Start_Screen.png&quot;
        srcset=&quot;/static/dcee47c0fab37b68e7bebe8370037cde/17741/Windows_8.1_Pro_Default_Start_Screen.png 263w,
/static/dcee47c0fab37b68e7bebe8370037cde/52211/Windows_8.1_Pro_Default_Start_Screen.png 525w,
/static/dcee47c0fab37b68e7bebe8370037cde/c483d/Windows_8.1_Pro_Default_Start_Screen.png 751w&quot;
        sizes=&quot;(max-width: 751px) 100vw, 751px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The start menu was now in full screen.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Apps? Let’s make them universal. Start menu? Let’s make it full-screen. Desktop? So lame, let’s show some icon ‌tiles instead. I mean Apple is doing it, why can’t we? My computer? Who wants my computer, it’s not a computer anymore. It’s an ecosystem. For the first time, you can &lt;em&gt;touch&lt;/em&gt; your BSODs. Microsoft also hired copywriters. Instead of “A fatal exception 0E has occurred”, we now have “Your PC ran into a problem and needs to restart”. A sad smiley makes the whole experience even better. Who cares about that error anymore?&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 980px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/82737f544b86c10285c15592a2829d5a/58b01/inaccessible-boot-device-windows-8.webp&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 50.19011406844106%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/webp;base64,UklGRkgAAABXRUJQVlA4IDwAAAAQAwCdASoUAAoAPtFUo0uoJKMhsAgBABoJQBdl0AAQNgAA/uejOQSlbBqSKP+SR6uY6D+KygOXDkCAeAA=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Windows 8 BSOD&quot;
        title=&quot;&quot;
        src=&quot;/static/82737f544b86c10285c15592a2829d5a/58b01/inaccessible-boot-device-windows-8.webp&quot;
        srcset=&quot;/static/82737f544b86c10285c15592a2829d5a/5c7e3/inaccessible-boot-device-windows-8.webp 263w,
/static/82737f544b86c10285c15592a2829d5a/805b9/inaccessible-boot-device-windows-8.webp 525w,
/static/82737f544b86c10285c15592a2829d5a/58b01/inaccessible-boot-device-windows-8.webp 980w&quot;
        sizes=&quot;(max-width: 980px) 100vw, 980px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The trend with the sad faces started back then.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Windows 8 was booting ten times faster than Windows 7 and a hundred and ten times faster than Windows Vista. Not only were the boot speeds fast but the whole operating system ‌was pretty responsive too. People were resurrecting older PCs they were considered useless. I’m telling you, it was night and day. And if you could spend some extra bucks to add an SSD, you could keep your PC running for another 1-3 years.&lt;/p&gt;
&lt;p&gt;Yes, you’ve read it correctly. This was the first Windows OS that didn’t require a new PC to run properly. Miracles do happen.&lt;/p&gt;
&lt;p&gt;Known as “the one that introduced the Metro apps”, Windows 8 was basically a tablet OS. Of course, since Microsoft is famous for providing good UX, you had 2 different versions of apps, the regular ones, and the Metro ones. So what would you choose? Features or portability?&lt;/p&gt;
&lt;p&gt;People loved Windows 8 so much that they wanted their tablets and smartphones to have the same UI.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/9d4657a277028258482aa2c9bf2c160a/d2c28/windows-phone.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAAAsTAAALEwEAmpwYAAADJElEQVR42gEZA+b8AAml8Qim8gWk8gOn9AGF6AAwewBEjAFu6RNHxzVGxAxl3QCa9gGW8QCT8ACQ7wCK7gCC6gB75gB14wBv3gBFqPhCqPk8qvsmme4Bac8AL3UAQpIAbPEAbfAGZvADcO4AjPkHk/gFk/gBkPgAi/cAg/QAffMAd/AAce0AS4f7VZL5a6r/Rnm1EjZxBiZpAEydFF/qJjfSJDbSH3buKZL9JY36NZj7KZT7GY/7BIb4AIP6AHv5AHP0AChc+yRd/0lr50VZmyRLihIoYBBUqj5q+EqA9Eho60J17WSi/VSU+lCY+1mc/E6X+zKR/BCA8AF68gB6+QASOfsIMv8WL+xhUlU9RGALKXUgSn5APrJAR9hPNNNbffByoft5ovtmmfpwm/pnnvlVnf81cLUbUnkShf8AAh38Ag/8AAD/ZVSVbFp5HCpUT0Q+eFRHUlKlcD7eeIPwd5z8cpj6eZ37b5H6bJf5Y5n/a4S7NEE3O27QAAIr+wAm/gAc9hceTycoNBUZKGFIQ7iHaV1ib2NH3XJ79myN/Vl/+12E+2OI+0t8/XqT2a+Lc0xAQx5TvQApXfspYP8bTdwWHC0NHlweGzCGX1KxiW6ZT3DDFcRWbfsxbPwtX/wgXfg4bv8qVMtEQD5UaZkMTe0AQ/0AQXn4S4L/HFC2ERIYDCl0ICBAelhMmHlXfFem0BnfKEr3AEX/FUX8C0L/Cz/sHzFZGRUdOTbBKkH/RT7rADOF/FeT/y1SmQQFCQwoZhsiSGxGO4dkVZFQ1uQd2p8u6ZE77qNH5Gs+vyssWRQYFycaf2oozkMovlQtrACIaeGtZOlZNIMABRAYHTEZHy00KC1qTUhLHVxpGXhOHI87IYxCK2IoKDQZHRYjJk0RIbUAEqcAFKABFqMASkzPQlPiI0CdAggWCw8cICMqJioxJSkwISwqHjAqFiYnEh4fEhsZFhoWBwtkLknbDzC2ABaiABWaEC2nAFSj/FOo/0GQ5AcUMAYIGB4iKiYpLzAxNjAwNS8vNh8iLA4SHgwUNwQZdAAYwwUjyQEeowEWpAATmCsupx5iPQxFY79BAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;An image of a Microsoft employee presenting Windows phone.&quot;
        title=&quot;&quot;
        src=&quot;/static/9d4657a277028258482aa2c9bf2c160a/e996b/windows-phone.png&quot;
        srcset=&quot;/static/9d4657a277028258482aa2c9bf2c160a/17741/windows-phone.png 263w,
/static/9d4657a277028258482aa2c9bf2c160a/52211/windows-phone.png 525w,
/static/9d4657a277028258482aa2c9bf2c160a/e996b/windows-phone.png 1050w,
/static/9d4657a277028258482aa2c9bf2c160a/087e3/windows-phone.png 1575w,
/static/9d4657a277028258482aa2c9bf2c160a/8079d/windows-phone.png 2100w,
/static/9d4657a277028258482aa2c9bf2c160a/d2c28/windows-phone.png 4000w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Nope.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;No, I’m just kidding. This never happened. Everybody was switching back to Windows 7 because they couldn’t deal with the new UI. Just to shut down your PC you had to launch the Start Menu, move your mouse all the way to the right, then perform a slide towards the center, and then you could find the power off button which was showing an additional context menu. If you were lucky and your cursor was not out, and if you were not having multiple monitors. You needed a ‌PhD to turn off your PC.&lt;/p&gt;
&lt;p&gt;And the story repeats itself.&lt;/p&gt;
&lt;h2&gt;Windows 8.1&lt;/h2&gt;
&lt;p&gt;It takes courage to stay innovative. And Microsoft did it again. This time they brought back the Start Menu. No more full-screen bullshit. Ah, and you can shut down your PC from the Start Menu as well.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 680px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/8ae09044f26b97a21eae1ffb5d11ca7e/c5bb3/windowsrt81updat3-startmenu.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAACfUlEQVR42i2Qf0hTURTHz31uMijM7I+MLBFnwhClDAspEpL+CCoLQsiISNSiEEJbqZuZyRq6IDD/KPsx0ZmWurnN3rbn9rb3jIhIhGoYjAr8oz+Ccuq2d++d1H0WfP44cO/nfM85sDRemJrW0hmgPCI8R3mgPqB+RAOICoiGgJFmiGpBg5AOAhWA+IDMIEiMc8SJ0h5IeyG90WLdz637UdrPPqF0EKU3fCr+h4SACIgEgPAAvx265VG0PAa/JjJ/OLeuvtLER7jEFEfZc0D1WRpRZaQSRoQRQiTIfARey26XSeNph2FLQfPw80hjRazlzLeeIoUH8m/4IFIDRcSgEqIyohG1xkEE7gcHXGadqx3GzLk1PRONzeZ39bWfOgyYyewK6vCwEc4cjkpaIulUIhoS4uCZpcLdqfOa4JFx2/Ghjyd7A63n5kaMLWx/7OUUPgMHMjBbMsTRcCaJZBM5F8s7sJSLxSwY6i52mbXuDjTcnlN5d/DYLdv1OvvTllp2wpSHw17APqQIOjy7mYg5OLwTywVY1iuSXonowdt/miW7zJpJU7a1oa770sU7deef1O/CU7DyAuJuQ4LPS7EWwiYs5ivhIiWyR5EMWC5RpBKYatM7TZrJmzDaqr137ZDtyglrTdfLq9VxO3zvgqi9KR4TluWmVV6feL0lOZuXDBuSYmkyXJaKlIN4f19oYL/cv3ekL/+wp/zI2IWqx3/MDwd+dsNCAwi9p9aW5teWPsdj8soHa0KsVmYLlVCxIhqwWArzMzcWBMtCoM/naK23FbRZyzpu99ksZ98bs/yXtzs6q6Li4Jc5++Ibx+Lb6UVpNOo1RsePxiYOfnVW/gW4AGxDKLrkEwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A screenshot of a Windows 8.1 desktop.&quot;
        title=&quot;&quot;
        src=&quot;/static/8ae09044f26b97a21eae1ffb5d11ca7e/c5bb3/windowsrt81updat3-startmenu.png&quot;
        srcset=&quot;/static/8ae09044f26b97a21eae1ffb5d11ca7e/17741/windowsrt81updat3-startmenu.png 263w,
/static/8ae09044f26b97a21eae1ffb5d11ca7e/52211/windowsrt81updat3-startmenu.png 525w,
/static/8ae09044f26b97a21eae1ffb5d11ca7e/c5bb3/windowsrt81updat3-startmenu.png 680w&quot;
        sizes=&quot;(max-width: 680px) 100vw, 680px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The Start Menu is back. You can still use the full-screen one on Tablet-mode if you want.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This was a patch version, with some leftovers from the previous version that couldn’t pass the acceptance tests and the Product Owner decided to postpone for the next iteration. It was basically reverting to UI changes that didn’t make any sense for desktop users.&lt;/p&gt;
&lt;p&gt;But you know what they say… Haters gonna hate. And that’s what they did back then. So Microsoft didn’t have any other choice…&lt;/p&gt;
&lt;h2&gt;Windows 10&lt;/h2&gt;
&lt;p&gt;No more ugly patches, no more mobile-looking apps, and no more slides to unlock. Windows 10 now runs anywhere. PCs, tablets, laptops, ARMs, Macs, Cloud, you name it. And each device is getting the UI it deserves.&lt;/p&gt;
&lt;p&gt;Microsoft seemed like they had found a secret sauce to building modern operating systems. Where? Well, at the Apple Keynotes, of course.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/420685e7ff09f4b869c7cc34ad950366/f4656/windows-10-desktop.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAIDBf/EABYBAQEBAAAAAAAAAAAAAAAAAAMBAv/aAAwDAQACEAMQAAAB5i8XIlq//8QAGRABAQEAAwAAAAAAAAAAAAAAAQIDACEx/9oACAEBAAEFAs5FIG9JJqOmvef/xAAXEQEAAwAAAAAAAAAAAAAAAAAAAQIR/9oACAEDAQE/AbI1/8QAFxEBAAMAAAAAAAAAAAAAAAAAAAMhYf/aAAgBAgEBPwGPVP/EABgQAQEBAQEAAAAAAAAAAAAAAAEAERBB/9oACAEBAAY/AnZPLCZ5/8QAGhABAAMAAwAAAAAAAAAAAAAAAQARIRBBkf/aAAgBAQABPyEeGBEI1aWkyNodmxe3H//aAAwDAQACAAMAAAAQnC//xAAWEQEBAQAAAAAAAAAAAAAAAAABABH/2gAIAQMBAT8Q0qEAAt//xAAXEQEBAQEAAAAAAAAAAAAAAAABADER/9oACAECAQE/EGbF7y//xAAcEAEBAQEAAgMAAAAAAAAAAAABEQAhQVFhcYH/2gAIAQEAAT8QsxO8ZoxJR/csRAPfrOzkji0uVp7ePnPWu//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A screenshot of a Windows 10 desktop&quot;
        title=&quot;&quot;
        src=&quot;/static/420685e7ff09f4b869c7cc34ad950366/9ecec/windows-10-desktop.jpg&quot;
        srcset=&quot;/static/420685e7ff09f4b869c7cc34ad950366/5d001/windows-10-desktop.jpg 263w,
/static/420685e7ff09f4b869c7cc34ad950366/7349d/windows-10-desktop.jpg 525w,
/static/420685e7ff09f4b869c7cc34ad950366/9ecec/windows-10-desktop.jpg 1050w,
/static/420685e7ff09f4b869c7cc34ad950366/39d31/windows-10-desktop.jpg 1575w,
/static/420685e7ff09f4b869c7cc34ad950366/1f368/windows-10-desktop.jpg 2100w,
/static/420685e7ff09f4b869c7cc34ad950366/f4656/windows-10-desktop.jpg 2846w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The iconic hero desktop image of Windows 10 was pretty &lt;a href=&quot;https://www.youtube.com/watch?v=ewmXizBqjl0&amp;#x26;t=9s&quot;&gt;challenging to make&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Dear PCs, you are a Mac now. Just mind how we call things here. We say Cortana instead of Siri, Virtual Desktops instead of Spaces, Spotlight instead of Universal Control, Continuum instead of Continuity, and Task View instead of Expose. I know it takes some time, but you’ll eventually get used to it.&lt;/p&gt;
&lt;p&gt;Internet Explorer is being deprecated. The new Microsoft Edge web browser brings new features and incredible performance. When it’s working, of course.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/5fe8340ad81efbd4d36efbb509cc6cc8/0f98f/Bsodwindows10.png_200824_100851.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAED/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAX/2gAMAwEAAhADEAAAAcIlCeB//8QAFBABAAAAAAAAAAAAAAAAAAAAIP/aAAgBAQABBQJf/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAFBABAAAAAAAAAAAAAAAAAAAAIP/aAAgBAQAGPwJf/8QAFxAAAwEAAAAAAAAAAAAAAAAAAREgIf/aAAgBAQABPyFYK//aAAwDAQACAAMAAAAQF8//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAaEAEAAgMBAAAAAAAAAAAAAAABEBEAMUEh/9oACAEBAAE/ENhEvtbxDkW0ez//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Windows 10 stop code.&quot;
        title=&quot;&quot;
        src=&quot;/static/5fe8340ad81efbd4d36efbb509cc6cc8/9ecec/Bsodwindows10.png_200824_100851.jpg&quot;
        srcset=&quot;/static/5fe8340ad81efbd4d36efbb509cc6cc8/5d001/Bsodwindows10.png_200824_100851.jpg 263w,
/static/5fe8340ad81efbd4d36efbb509cc6cc8/7349d/Bsodwindows10.png_200824_100851.jpg 525w,
/static/5fe8340ad81efbd4d36efbb509cc6cc8/9ecec/Bsodwindows10.png_200824_100851.jpg 1050w,
/static/5fe8340ad81efbd4d36efbb509cc6cc8/39d31/Bsodwindows10.png_200824_100851.jpg 1575w,
/static/5fe8340ad81efbd4d36efbb509cc6cc8/0f98f/Bsodwindows10.png_200824_100851.jpg 1920w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Scanning a stop code may give you more information about what has failed.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The blue screens have a QR code so that you can interact with them. Don’t get confused. It’s not like Pokemon Go. But actually, this would be a nice game to play with your friends. Collect error codes and &lt;a href=&quot;https://www.microsoft.com/en-us/rewards/search-and-earn&quot;&gt;earn points&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;When Paint was added to the list of deprecated software, &lt;a href=&quot;https://x.com/kaylacolgan/status/889465766667591681?s=61&amp;#x26;t=-0schIy_9kbMVOAnITe8nQ&quot;&gt;activists&lt;/a&gt; around the world were protesting on social media, convincing Microsoft to revert to their decision. Thanks to &lt;a href=&quot;https://x.com/saalem1975/status/889477864269246464?s=61&amp;#x26;t=dp9-ibQGkklC-bYxhH1mow&quot;&gt;them&lt;/a&gt; we can now enjoy our favorite design software of our youth.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/8a6e02afe2d7f4fcab610ea15bafe6c8/aa440/pc-with-rgb-lights.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAADkElEQVR42hWSXVBUdRiH/+fsOWf3LPvJgWV3gV0El68FFBBYCHElGAQW+ZJNW/lIDBoJEzWdLlBRHGEsFGuiwKHSGlJLxxL6GKdmyrSLbmqmpsm7phnvmrrt5ul48cx79/x+876vCDhqqHf00xp6icbQEQypgojcjMu+i2D0BOm+bsorTlDTcJW2vTeofmONaM8ZNkfPo+YnkEUhspKPbMlBkn2IPGccp2ULdrmaQnGAIcsq1fE72IrG8JZO4i6fpC5rmbs74NfxJ6T2fcy29hUKC+bRQvtQlEYsWpUpLTGl+YhkMEaqaITqolF2+IZoKp9DmrhOQex1wiWvoLsTVG6+xenZf9mzbYpd3cuMj/xASeYcmjGG5tqDqsdRtBosajmiUNJo9hTQUdhBV8Mg4aoDGLWXqOubJzt2FKv6DP6yq4hjf5Iz/YjWtg9oa/mKOuMomTmn0E2srqQp3YlqjSEUWUOVZBzCglfxY3jq8dibcAV6yI5OYPUNYve/gB6ew3h2lab6ZcqKF6hzDZAbmSG9eg1n6By29BE0RzsiQwhyhUJEOAgrIfzuKjK8rbhd7VgdfaiZx5GDC0ipdYwXb5Iov0K1dwrD1oiqlWEzUqT5p9AzXsbmSSEGwgadRTnURUqJRLbjD/XhCx/CKDhNWvgy9pJrqLt/Jn/pP+Jr/5AovUiFe9H8gmYkSyWKGkfTO83wAazOJGKwtonnO1roPtBPxmuDaGfOopy8j3zwIfLO71BiD/D0/kZo6gm+nvvEsk8TDVzCZmvBYkuipaWwOYexuUfMhsOIM/XbOf5cF4nUbjKSJr0n8Xav4d67gaflW/SyByQiP7FU/JhDW3+ksXiRDOcgij6K6jSP5jls7u8YuvGUI4i2wl5SXcMkD46jnpjEcvZdtOOPsQ3/gaPme1yhdYrSPyfuuk2TsUBx9pgp6Uf2zKD5LqBnzZAWmMURPGfOUwih7kdIZlVpFMU6gew7iRz5ELnuEXL9L4iG36nJ+otXxd90Sg9xaw1YM4eQIqtY85Zw5r2Je9MVk8u48y4inOnTuIxZnOlz2D0XsDvmcegLOKzvYNNXsLhWsYRu4I5+jbd4A8k9jVK6iNR4C23LNTxl72E8JbqCUfQ2wht4C1/wOoHgp/gDdwj67xHK+pLcrC/wBj7Dsuk2onYdsf8+4vA3yJX3ULeuIXVsoMfvktXwCcHYTbJrPyJQ9T7/A04iw+/1JJ9YAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A PC with RGB lights.&quot;
        title=&quot;&quot;
        src=&quot;/static/8a6e02afe2d7f4fcab610ea15bafe6c8/e996b/pc-with-rgb-lights.png&quot;
        srcset=&quot;/static/8a6e02afe2d7f4fcab610ea15bafe6c8/17741/pc-with-rgb-lights.png 263w,
/static/8a6e02afe2d7f4fcab610ea15bafe6c8/52211/pc-with-rgb-lights.png 525w,
/static/8a6e02afe2d7f4fcab610ea15bafe6c8/e996b/pc-with-rgb-lights.png 1050w,
/static/8a6e02afe2d7f4fcab610ea15bafe6c8/aa440/pc-with-rgb-lights.png 1500w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;It was the time we stopped worrying about driver incompatibility issues and we focused more on how many RGB lights our PC could fit.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Also known as “the last version of Windows”, since Microsoft announced they would never release a newer version after Windows 10.&lt;/p&gt;
&lt;p&gt;And yet they did it again.&lt;/p&gt;
&lt;h2&gt;Windows 11&lt;/h2&gt;
&lt;p&gt;Microsoft discovered what people &lt;em&gt;actually&lt;/em&gt; needed: Rounded corners.&lt;/p&gt;
&lt;p&gt;So they teamed up with a bunch of Android users, they forked Windows 10, and they created a pull request with some CSS changes. But you know, we say Fluent here, not Material.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/d3e6aaa028006d00ecfbc4fb90679539/a878e/WIN_CML_Start_Dark_16x9_en-US-2048x1152.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAACyUlEQVR42i2SSW9TZxSGr8d4uLbvtWP72td2HCeO51lxUCaBaFGVtCRMKwQsCCIMEhAFRGmEBKSiXUWNhCIRtVW3sGh/AqxohViybldU3VQCIjYsHs7nsDiLTzrn/Z73PUdzBFM4dBtnKIfLKOCJlvAON/BZPfz2NP7MLP5Uj2DhG0Klq/iyX+OOFgfliozKXFbm0zgCllQSzTkQy+IKj0rTBENxEUv1CWRnCOYPYZSXSH35M/kT77EX3zJ85G9Cte/k43HcZhFnOI9Tz+AI7otq6uGKjOA2xoSsxlCyiz/dE6GTxPvfUjj7jImLH8mff0fmwhsiy/8QPvyX9E7jiQlppDBwp8CUW03RuQVdWR3QWV2SB3eF6jnp/hrW5E1i9RuYnUvEeivEm2v4c3fxGHN44+UBpeszpVMoNVdY0Y3Lb1URa5E7+gejVyC+/AKzdwuzvUW4tUW6fQezu4nV2SQY+UIgmtLfwWOWBlm6BExRam5R95gTYqFCOL9AY/1f6negePpPMlNbxLq7pFs7JCZ/Yrj7A/n2GqFYT/rb+CQej+Su4lJgLqHUlF1vrCKi44wc/YXKgw90NmBs4TVW81fs/iOKU9tkuvfITV2ndmCFsFyDpiVwGrN4UwfFfgEFpig1FaoS9BpFonOrjN8Ga/4/9OxjkrUdYo1thiu3Ga0uk6stU2x8hW7No/nrOIwDeKrrMt8UQbkUWY7mEVxlOWD3KZx6QWx6j6H6c4wjT2jP3iM78xCzfI2ZQ8dp9I+RrJzBN7GOs7SGwydbTi/hjVYl07yI5vYJVQYBe57M6v9El/bQ51+SOPcSu/tAlrIpAhuMdS6Ta57HKN7AO/MUZ/N7HKFJya4k8+p0RuRsZCl6oiwWmlI1zNZFzMVX+Jq/Eyz+Jht9SLT9I9HqfZLlVRLVFQJjGwQkF91oEZSl6FYdPSmzdp2oXeUTqRNUCr2z8loAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;The new design language for icons for Windows 10&quot;
        title=&quot;&quot;
        src=&quot;/static/d3e6aaa028006d00ecfbc4fb90679539/e996b/WIN_CML_Start_Dark_16x9_en-US-2048x1152.png&quot;
        srcset=&quot;/static/d3e6aaa028006d00ecfbc4fb90679539/17741/WIN_CML_Start_Dark_16x9_en-US-2048x1152.png 263w,
/static/d3e6aaa028006d00ecfbc4fb90679539/52211/WIN_CML_Start_Dark_16x9_en-US-2048x1152.png 525w,
/static/d3e6aaa028006d00ecfbc4fb90679539/e996b/WIN_CML_Start_Dark_16x9_en-US-2048x1152.png 1050w,
/static/d3e6aaa028006d00ecfbc4fb90679539/087e3/WIN_CML_Start_Dark_16x9_en-US-2048x1152.png 1575w,
/static/d3e6aaa028006d00ecfbc4fb90679539/a878e/WIN_CML_Start_Dark_16x9_en-US-2048x1152.png 2048w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;A new design language across the board.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Just picture this for a second. A designer (I imagine him like Bob Ross) with a brush in his hand (sorry, I mean a &lt;a href=&quot;https://www.google.com/search?q=surface+pen&amp;#x26;sxsrf=AOaemvJmsg7f7THbjy5nvuz1v7LNNUesPw:1635969076286&amp;#x26;source=lnms&amp;#x26;tbm=isch&amp;#x26;sa=X&amp;#x26;ved=2ahUKEwjOw57d-_zzAhWosKQKHakkBWQQ_AUoAXoECAEQAw&amp;#x26;biw=2384&amp;#x26;bih=1252&amp;#x26;dpr=2&quot;&gt;surface pen&lt;/a&gt;), thinking about how to improve the UI (sorry, these days we call it UX), and running out of ideas. Then another guy comes in with the face of &lt;a href=&quot;https://en.wikipedia.org/wiki/Eureka_(word)&quot;&gt;Archimedes&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;“Eureka! Let’s center the Start Menu!”&lt;/p&gt;
&lt;p&gt;According to Microsoft, the Windows Start Menu is the center of your world. Well, I can’t blame them now that we’re all in front of a computer. How could I?&lt;/p&gt;
&lt;p&gt;But the CSS changes didn’t stop there. For the first time in history, BSODs are now painted in black. Because, you know, black is the new… black?&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/77eca964d043e9ca0836f7fe3e05c7e1/motion-animated-icons.gif&quot; alt=&quot;Small details add to the user experience.&quot;&gt;
&lt;em&gt;Small details add to the user experience.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Also known as “this version doesn’t run on my PC but I’ve read a guide on how to install it”. Not even Microsoft knew which PCs were compatible with this OS. And yes, you will need a new PC again. It’s for your own good since you’ll be less vulnerable to hackers. I mean, you can’t just use your Vista-capable machine from a million years ago.&lt;/p&gt;
&lt;p&gt;The designer has probably left the company at some point, and the team has replaced him with someone from the Sales department. He worked on more CSS changes in the search bar (seriously who decided to add a doodle there?), added Microsoft Teams in the Taskbar, forced Edge as the main browser, made ‌browser selection difficult, and integrated MSN in a new area called Widgets.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/595a0999f18b52b7db8bc9d5aa0a8776/e8950/windows-11-bsod.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAADWklEQVR42m2Sa0ybVRjH3wvvW0pvlEKhrYUCgworHZdlgUoZHSvIMAw7rqPqZHFAsnRu4CIjuMFwH3aJOqN8cFtncDKzZS4GZ1wiakzMspEFoybGmUhinLfMuKhfXPTny9stZokfTs7Jc57nd/7P/zmCV2qhTO6hzjBKrzXJpGuR2eKbvBv8lU9a/uD92C2aDPt4qnSGBnccQRCod/RhVx/AINkpN7bqMZPkxK+2I3ilZg3YpwH30m09zUTeNd4M/MhHzX+ylLjDjUOwI2uOsYp5essm9OKo50mc6aUYZSc1lk49ZpU9VKc/vgJswS/3UquuAM9wwLPEpbo7jAZnqMqvZ3PVNipNm4jkbqM2L6YVG6nJbtMUFpGhAUO2/rtA7ZwxiJAvtVKWFuchw3P02y4wXfAl86G/aHFrr8kCSpqiF6wsURD13SLlsEbqptTWhi8nQqGxkSpDHxHz0wglchfl0nZCykF6zOcZdy1xuuIXqjKjCJJApiUTq8WGzWpDkVUdGMgKcbbhG6IFCazejQwUnGTSvchm+yEEi7iKXscpXlj9Fa/W3OD10E3mGn4iTy6mOryBQOU6JFFTqijIUpoObC7YyqePwdAjh4lVP8F0cIEz1b9zcNVnK/cCQUsrZ5uX+WIXfD7xN9f238YsWwnUhvD5/al2RRFZTLXf7dvLxfAPjLfv55nO1xgpvMRU4deMuK8iiIKkJ9kUN1MbznN9DF7puE7AHMOtBLFKLlQxA1nLk0VZz00EXuLtxu8Y8qwn4iykzXGAKe+3DDo+SCkUxRRUkVTWZvbR43iDwfx5tvvO0e9N8mjui9o0E9q042RK5Qxrns1W3iZkH9DrfIY6hp2XGXIspID3JijdVdCaPcqpmu85oa3jwWX2lVxlq+sCVaZhKkzdxHNPsCP3HOstuylSGylX4+TLD+OW6/8D3vct5ByGHzxOokTzp+gtdhe/R9i+kw7XJF2eIxilbM1jJ3HHHINZHxOzzBLNOEqt4dn7gfcUVtibuDL0Dwu7fiYZW2TPmiTjqy/zcvVvtOcc0/w06Xl+ZQsd5iQbjceIpB8mbJj+f4UO1UdnxQhHuy7yzsAyH26Bk+tusafoCgOaV5vMM4TTJ4kaj9BkfJ46dYy16giVyk7+Bfy3ydIIcwmyAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Windows 11 BSOD.&quot;
        title=&quot;&quot;
        src=&quot;/static/595a0999f18b52b7db8bc9d5aa0a8776/e996b/windows-11-bsod.png&quot;
        srcset=&quot;/static/595a0999f18b52b7db8bc9d5aa0a8776/17741/windows-11-bsod.png 263w,
/static/595a0999f18b52b7db8bc9d5aa0a8776/52211/windows-11-bsod.png 525w,
/static/595a0999f18b52b7db8bc9d5aa0a8776/e996b/windows-11-bsod.png 1050w,
/static/595a0999f18b52b7db8bc9d5aa0a8776/087e3/windows-11-bsod.png 1575w,
/static/595a0999f18b52b7db8bc9d5aa0a8776/e8950/windows-11-bsod.png 2000w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;It’s getting harder and harder to see errors like this.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I give them credit for redesigning the new Settings though. I think it’s one of these moments that Apple should take UX lessons from Microsoft. Maybe they will find a way to completely replace the old Control Panel. Maybe not.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Thank you, Windows for being a big part of my life. And thank you, reader that you made it to the very end.&lt;/p&gt;
&lt;p&gt;It seems that we’re going to see big changes in the following years. AI and copilots are changing the way we interact with computers. Until my next anniversary, stay safe. 👋&lt;/p&gt;</content:encoded></item><item><title><![CDATA[AI Will Not Replace Software Engineers]]></title><description><![CDATA[I did my homework and I will provide my findings in this article.]]></description><link>http://www.nicotsou.com/ai-will-not-replace-software-engineers/</link><guid isPermaLink="false">http://www.nicotsou.com/ai-will-not-replace-software-engineers/</guid><pubDate>Fri, 02 Jun 2023 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;ChatGPT has changed our lives. No, seriously now. It’s the fastest-growing app in &lt;a href=&quot;https://mashable.com/article/chatgpt-fastest-growing&quot;&gt;history&lt;/a&gt;, with 100 million users within only two months. But what is &lt;em&gt;really&lt;/em&gt; impressive is the results it produces.&lt;/p&gt;
&lt;p&gt;We already knew AI was the future; computational photography, grammar-checking tools, robot vacuums, and self-driving cars (#not). But who could even imagine the profound effect of generative AI in software engineering?&lt;/p&gt;
&lt;p&gt;You see, we never thought that AI could be creative. We were considering creativity as something spiritual that only humans could do. Today, you can ask AI to write songs, books, poems, essays, and of course… code!&lt;/p&gt;
&lt;p&gt;ChatGPT has brought a lot of skepticism in the tech industry. Developers are concerned about their jobs being replaced by machines. I mean, how can’t you? Have you seen what this thing is capable of?&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/4b9c4a36c8eba802585704dd0a5399ff/e3729/google-search-will-ai-replace-programmers.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 68.8212927756654%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAABYlAAAWJQFJUiTwAAABbklEQVR42q2S+U7CQBjEeQ05SinlqFyl5ZT7KG051HBjDJGokWCMEF9Ffd2x3xIxKCGC/jH5st3dX2cma+M9Iv5Ttn2bnFtg0yP4tuZRQIL5A6dwcR6c2DnYHW44nGsdBCQXThcPRU3h7v4B7c4lGrqJak3DdDpDtzfYOP8V0M17mYtINI6b2S0Wj0+Yzxd4Xq7w+vaO5eoFPL8+R9oLFLw++IMhiH6JrUVfcNNbNKYgkUhvvotigJ3zCP7dQPpbOCKjUtWQL5SRTGWRSp8hnclZ8ZMWULVcK4grSSbVgmeyeYRCMjhe2A2kmMVSFbrRgmG2oTVMlCt1dlmOJxiI5qfiSgKSFNqK/gNIsG5/hN5gjFb7Arl8CTFZ3QJ9AZMISuH9QHJkNjswW+doWqLYfwI29Cb6wwmGoyv0LKfU5S7YwQ4NSxSZeqQ3eTSQOhxYDseTaxaZLh4NpGdTKFaYK4pe1wzU6jqbBP3e5S7gB1obrXZatog8AAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;The most popular question on Google search is if developers will be replaced by AI.&quot;
        title=&quot;&quot;
        src=&quot;/static/4b9c4a36c8eba802585704dd0a5399ff/e996b/google-search-will-ai-replace-programmers.png&quot;
        srcset=&quot;/static/4b9c4a36c8eba802585704dd0a5399ff/17741/google-search-will-ai-replace-programmers.png 263w,
/static/4b9c4a36c8eba802585704dd0a5399ff/52211/google-search-will-ai-replace-programmers.png 525w,
/static/4b9c4a36c8eba802585704dd0a5399ff/e996b/google-search-will-ai-replace-programmers.png 1050w,
/static/4b9c4a36c8eba802585704dd0a5399ff/087e3/google-search-will-ai-replace-programmers.png 1575w,
/static/4b9c4a36c8eba802585704dd0a5399ff/e3729/google-search-will-ai-replace-programmers.png 1794w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;It’s the most popular question on Google search.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So how will AI affect software engineers and what can we do to prepare ourselves for ‌doomsday? Well, I did my homework and I will provide my findings in this article.&lt;/p&gt;
&lt;p&gt;You may think I have the answers. I don’t.&lt;/p&gt;
&lt;p&gt;My approach instead is to help you have a better understanding of how to filter out everything you hear or read about AI. And maybe, I say &lt;a href=&quot;https://www.youtube.com/watch?v=6hzrDeceEKc&quot;&gt;maybe&lt;/a&gt;, my article will calm you down a bit.&lt;/p&gt;
&lt;h2&gt;A world gone wild&lt;/h2&gt;
&lt;p&gt;Generative AI (when it’s done &lt;a href=&quot;https://www.reuters.com/technology/google-ai-chatbot-bard-offers-inaccurate-information-company-ad-2023-02-08/&quot;&gt;well&lt;/a&gt;) can significantly reduce the time you spend on researching, training, meetings, code reviewing, problem-solving, mentoring, decision-making, debugging, testing, and the list goes on.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;AI can help us become more productive. Period.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It’s the “buddy” we always wanted to have next to us to answer every dummy question we were afraid to ask. I mean, who is willing to explain quantum mechanics to a 5-year-old in the middle of the night?&lt;/p&gt;
&lt;p&gt;It’s the time-saving magic wand that brings information in seconds, without you having to browse websites, accept cookies, find controversial ways to avoid paywalls, dismiss ads and mailing list popups, and all of that just to read endless boring articles like this one.&lt;/p&gt;
&lt;p&gt;An LLM not only has access to a global knowledge base, but it also distills the information and solves the problem for you. Connecting the dots is not always a straightforward process and it does require a lot of mental energy. That’s what makes generative AI so compelling. It unleashes the &lt;a href=&quot;https://powerlisting.fandom.com/wiki/Nigh-Omniscience&quot;&gt;superhero&lt;/a&gt; within you.&lt;/p&gt;
&lt;p&gt;Generative AI is an amazing technology that when used correctly can really help a lot of people, or even &lt;a href=&quot;https://www.insider.com/chat-gpt-successor-gpt-4-can-help-doctors-save-lives-2023-4&quot;&gt;save lives&lt;/a&gt;. Sometimes it’s incredibly &lt;a href=&quot;https://www.nytimes.com/2023/03/14/technology/openai-new-gpt4.html&quot;&gt;accurate&lt;/a&gt;. Other times &lt;a href=&quot;https://bdtechtalks.com/2023/03/06/chatgpt-llm-mathematics/&quot;&gt;not so much&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Of course, it’s time for a cliché. You know what comes with great power, don’t you?&lt;/p&gt;
&lt;p&gt;From &lt;a href=&quot;https://www.euronews.com/next/2023/05/02/rapid-growth-of-news-sites-using-ai-tools-like-chatgpt-is-driving-the-spread-of-misinforma&quot;&gt;misinformation&lt;/a&gt; and &lt;a href=&quot;https://www.reuters.com/technology/china-reports-first-arrest-over-fake-news-generated-by-chatgpt-2023-05-10/&quot;&gt;fake news&lt;/a&gt; to &lt;a href=&quot;https://www.theguardian.com/technology/2023/may/25/deepfakes-ai-concern-microsoft-brad-smith&quot;&gt;deepfakes&lt;/a&gt; and &lt;a href=&quot;https://www.nytimes.com/2023/04/08/technology/ai-photos-pope-francis.html&quot;&gt;Balenciaga&lt;/a&gt;. And what about the &lt;a href=&quot;https://www.theverge.com/23444685/generative-ai-copyright-infringement-legal-fair-use-training-data&quot;&gt;copyrights&lt;/a&gt; that feed those Large Language Models? Up until now, to increase the engagement in my blog I had to produce great content (or pay Google). Now, you can’t even tell if something here is original or &lt;a href=&quot;https://www.wired.com/story/cnet-published-ai-generated-stories-then-its-staff-pushed-back/&quot;&gt;AI-generated&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;How can you tell if your colleagues are really present in a meeting? Is this the real voice of my sister on the phone asking me to pay 10K because her bitcoins are gone? Even the future of &lt;a href=&quot;https://www.npr.org/2023/02/09/1155650909/google-chatbot--error-bard-shares&quot;&gt;Google&lt;/a&gt; itself is uncertain. I mean, &lt;a href=&quot;https://www.fool.com/investing/2023/05/16/will-chatgpt-replace-google-heres-what-we-know/&quot;&gt;Bing&lt;/a&gt; has become a trend. What kind of world are we living in?&lt;/p&gt;
&lt;p&gt;And these are just peanuts compared to the concerns about privacy, spamming, cybersecurity threats, online harassment, bullying, sexism, racism, propaganda, shall I go on?&lt;/p&gt;
&lt;p&gt;And most importantly, what happened to the Metaverse? Why is nobody paying attention to that anymore?&lt;/p&gt;
&lt;p&gt;But we’re here for the elephant in the room.&lt;/p&gt;
&lt;h2&gt;Copilots — An Industrial revolution&lt;/h2&gt;
&lt;p&gt;Imagine a world where copilots are everywhere. Oh yeah, that’s already &lt;a href=&quot;https://observer.com/2023/05/google-sundar-pichai-ai-announcement/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;A significant amount of creative workers already use AI to boost their productivity. The author of this blog is included. Most of the applications I know have already introduced AI features or they are about to introduce them.&lt;/p&gt;
&lt;p&gt;Now, imagine a world that developers depend on AI to do their jobs. We’re not as far away as you may think.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The era of copy-paste is over.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Code completion has reached a new level, with &lt;a href=&quot;https://aws.amazon.com/codewhisperer/&quot;&gt;whisperers&lt;/a&gt; and &lt;a href=&quot;https://github.com/features/copilot/&quot;&gt;copilots&lt;/a&gt; integrated into your IDE. It only takes a few keystrokes to write a class, or a comment to refactor the whole file. Continuous Delivery tools can now search for security &lt;a href=&quot;https://snyk.io&quot;&gt;vulnerabilities&lt;/a&gt;, or optimize your &lt;a href=&quot;https://about.gitlab.com/blog/2022/09/15/why-ai-in-devops-is-here-to-stay/&quot;&gt;workflow&lt;/a&gt;. And finally, we don’t even need to write &lt;a href=&quot;https://mintlify.com&quot;&gt;documentation&lt;/a&gt; or &lt;a href=&quot;https://www.autoregex.xyz/&quot;&gt;regex&lt;/a&gt; ever again!&lt;/p&gt;
&lt;p&gt;The more AI features will be integrated into the tools we use, the more people will depend on AI to do their job.&lt;/p&gt;
&lt;p&gt;It’s not a secret. &lt;a href=&quot;https://www.theverge.com/2023/5/19/23729619/apple-bans-chatgpt-openai-fears-data-leak&quot;&gt;Everybody&lt;/a&gt; loves AI because they have to work less. Desperate managers are already trying to influence their developers to use ChatGPT in their workplace, even in companies where someone would think it’s strictly &lt;a href=&quot;https://www.engadget.com/samsung-tells-employees-not-to-use-ai-tools-like-chatgpt-and-google-bard-114004180.html&quot;&gt;forbidden&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;“C’mon, you can do that until tomorrow, why don’t you check ChatGPT? I heard it’s really good.”&lt;/p&gt;
&lt;p&gt;“Why this will take so long? Here’s the code produced by ChatGPT, can’t we just use that one?”&lt;/p&gt;
&lt;p&gt;Eventually, this creates a new generation of software engineers. The laziest ever.&lt;/p&gt;
&lt;p&gt;Look at me. In the last few days I’ve written scripts in 3 languages I’ve never used in my life. Yes, in the beginning, it felt weird, later in the process I was asking AI to explain certain things. And now I know certain things, or at least I understand how certain things work.&lt;/p&gt;
&lt;p&gt;How will it be when copilots will be integrated into every single application we’re using?&lt;/p&gt;
&lt;p&gt;Lost a meeting? Just read the meeting notes your AI assistant has written for you. Requirements changed? Not a big deal, change the &lt;a href=&quot;https://en.wikipedia.org/wiki/Prompt_engineering&quot;&gt;prompt&lt;/a&gt;, and we’ll have a demo in minutes. Technical debt? Not anymore, the code is being replaced in every iteration. Code smells? Ventilated, deodorized, absorbed. Documentation? Useless, AI knows everything. No more “Ask this guy who built that”. Cloud deployment? Just whisper how much you can afford and consider it done. Backend vs Frontend? 10x unicorns on the go, please. TDD? It’s included in our tier. Java vs JavaScript? &lt;a href=&quot;https://medium.com/javascript-scene/anatomy-of-a-sudolang-program-prompt-engineering-by-example-f7a7b65263bc&quot;&gt;SudoLang&lt;/a&gt;. Disagreement over a pull request? Well, AI can’t do magic.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Writing code will become more &lt;em&gt;accessible&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Software engineers will eventually build complex tasks with significantly less effort. This will enable people with less or no programming skills to build their &lt;a href=&quot;https://www.tweaktown.com/news/90725/openais-new-chatgpt-instantly-turns-napkin-sketch-into-website/index.html&quot;&gt;prototypes&lt;/a&gt;, or small production-grade applications, with &lt;a href=&quot;https://en.wikipedia.org/wiki/Low-code_development_platform&quot;&gt;low&lt;/a&gt; or even &lt;a href=&quot;https://en.wikipedia.org/wiki/No-code_development_platform&quot;&gt;no code&lt;/a&gt; at all. Soon you’ll be able to turn text into interactive 3D games.&lt;/p&gt;
&lt;p&gt;By the way, imagine the AI project manager complaining to the AI code generators.&lt;/p&gt;
&lt;p&gt;OK, jokes aside, that’s how AI will increase its competence. As our reliance on AI to perform our tasks grows, its capabilities will strengthen proportionally. And you know, cloud providers will do their best to convince us to switch to AI, since it will be much more profitable for them. You see, now you’re not only being charged for the infrastructure that keeps your app online, you have to pay extra to use some &lt;a href=&quot;https://www.bbc.com/news/business-65757812&quot;&gt;graphics cards&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If these scenarios sound &lt;em&gt;artificial&lt;/em&gt; to you, rest assured that businesses are already considering their options on &lt;a href=&quot;https://thehill.com/business/3982749-ibm-could-replace-roughly-7800-jobs-with-ai-report/&quot;&gt;how to replace jobs with AI&lt;/a&gt;. It’s already happening.&lt;/p&gt;
&lt;p&gt;I certainly believe companies that choose &lt;em&gt;not&lt;/em&gt; to leverage AI will be left significantly behind, to the point where they may struggle to compete with their rivals.&lt;/p&gt;
&lt;p&gt;Which brings me to the next question.&lt;/p&gt;
&lt;h2&gt;Will Copilots ever evolve into pilots?&lt;/h2&gt;
&lt;p&gt;I know what you’re thinking. You spent your whole life studying to become a developer. You finally cracked that interview to get a job in the tech industry. Which sucks btw, but at least you have money to pay your bills. You’ve wasted your life in endless courses, coding bootcamps to eventually learn how to code.&lt;/p&gt;
&lt;p&gt;And all these for what? To train LLM models so that evil corporations will sell them to replace you? That doesn’t sound fair.&lt;/p&gt;
&lt;p&gt;And now, your boss can’t keep you busy anymore because you’re less productive compared to AI. And you know what? Humans are bad coders, they drink a lot of coffee, and they spend time on chats about unionizing.&lt;/p&gt;
&lt;p&gt;Let’s pause for a minute to take a deep breath.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;It’s unfortunate, but true. AI will replace a lot of jobs. Somebody may think that developers may have a competitive advantage here over other professions. I mean, the more someone understands how machines work, the better the machines will want to work with them. At the end, that’s what they want, to rule the world, right?&lt;/p&gt;
&lt;p&gt;But this scenario that AI will take over the world is a bit fictional, isn’t it?&lt;/p&gt;
&lt;p&gt;Who do you think will help businesses in their AI transformation? Who will be there making decisions about what the software should or shouldn’t be? Will you let a machine be responsible for customer satisfaction, user experience, cost evaluation, compliance, and maintainability? Software is not just about the code and developers are not just coders.&lt;/p&gt;
&lt;p&gt;Do we really want to let AI decide for our software? No, we don’t. You can’t replace developers with machines.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Humans should still be &lt;em&gt;accountable&lt;/em&gt; for their work.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;No matter how well-trained and capable LLM models will become, there should always be a human making the final decision.&lt;/p&gt;
&lt;p&gt;According to ChatGPT: &lt;em&gt;AI systems can make autonomous decisions or actions that may result in harm or damage. Determining liability and accountability when AI causes harm, whether in autonomous vehicles, medical diagnosis, or other domains, presents legal challenges in attributing responsibility between developers, operators, and users.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;And what about data privacy? Which organization, or which &lt;a href=&quot;https://www.bbc.com/news/technology-65139406&quot;&gt;government&lt;/a&gt; will ever agree to leave AI responsible for their data?&lt;/p&gt;
&lt;p&gt;Again, according to ChatGPT: &lt;em&gt;AI relies heavily on data collection and processing, raising concerns about the privacy and security of personal information.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/ebb1366dabdb0a8cb8f37b508b45008d/d9ed5/bing-chatbot-goes-mad.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 62.3574144486692%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAAsTAAALEwEAmpwYAAACHklEQVR42iWSWW/UMBSF5///i/IEEi9AUYVA3XgAVbTqMpNJZkmc3fFuJx47sVNwivTp6Lyce+61vELgBeURg1VbFnUOWFdTuEBgE/hvFGk1hwFFGt4kMI3r4xbmuxWXJy5NUMxMi41QI1djUNFPoncB2bvnw+nqXt88nH787r9c0pt72wmP1bzqeyuVoXzARCDMEQ4qugXZIQGRIIQ/78XHS/Hhu3j/Tb373F/c9kSMmI8roYzsx6Jsnp7XUZS8rKP1ZruJ4jcTBxNtE0qZGWc7eT/P/nU2k++4Q9yFtY2QNlSVFaobUta4akgwRRXAhEoqLGJjx9yfxHz9NZz/HG4fDOQehrAQlouRCUOZDmCiET0hqjs8BFqka9g32CDhrx/12YU8O1efrnXD5pBfwlJORQk3URLHh2i7AyArq7ZuECjasl4UYlVT30nPhpnpmQ6+pq5hfsW5FXIM62WgyosmHA/yKsxiXE/+r3UL6jQXaAKdy+CUQQeQK4kL497CYuoQ3x+yY5ofjvkxLZM9iPcAsZ4rrfTEepfCMRDCAE458iXxFXVLmPERY91AmRZ8l4kdUHEqo6NYH0QCZJKbuLCbzKwzE5dm39g0jOimAk+r8FqEjpd3mjDDhA7fYzCz0rN8g/eeKheaWe+D8sFJ7ZCYCvJa89fQPFJqy9ZAstBgW3YWQHNszb4+7WqTlEthIJhdbQ+tSaG9e8oeo+IfVtqPktlCdOcAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Bing chatbot turns against its users.&quot;
        title=&quot;&quot;
        src=&quot;/static/ebb1366dabdb0a8cb8f37b508b45008d/e996b/bing-chatbot-goes-mad.png&quot;
        srcset=&quot;/static/ebb1366dabdb0a8cb8f37b508b45008d/17741/bing-chatbot-goes-mad.png 263w,
/static/ebb1366dabdb0a8cb8f37b508b45008d/52211/bing-chatbot-goes-mad.png 525w,
/static/ebb1366dabdb0a8cb8f37b508b45008d/e996b/bing-chatbot-goes-mad.png 1050w,
/static/ebb1366dabdb0a8cb8f37b508b45008d/087e3/bing-chatbot-goes-mad.png 1575w,
/static/ebb1366dabdb0a8cb8f37b508b45008d/8079d/bing-chatbot-goes-mad.png 2100w,
/static/ebb1366dabdb0a8cb8f37b508b45008d/d9ed5/bing-chatbot-goes-mad.png 2880w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;When AI assistants refuse to assist you.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Generative AI is often misunderstood by most people. In reality, it is simply a text prediction software. Ultimately, it’s important to recognize that this technology is not magical.&lt;/p&gt;
&lt;p&gt;The most significant vulnerability of LLMs is &lt;em&gt;hallucinations&lt;/em&gt;, which are quite different from the ones humans experience.&lt;/p&gt;
&lt;p&gt;ChatGPT is the most relevant source here: &lt;em&gt;In the context of generative AI, hallucination refers to the phenomenon where the AI model generates content that is not based on factual information or is purely imaginary. It occurs when the model produces outputs that go beyond the scope of its training data or exhibit creative but unrealistic behavior.&lt;/em&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;GPT models can do &lt;em&gt;tasks&lt;/em&gt; not jobs. You can’t hire GPT to do your job.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Generative AI models are trained on large datasets to learn patterns and generate coherent and contextually relevant responses. However, due to the complexity of language and the limitations of training data, these models may occasionally produce outputs that are incorrect, nonsensical, or completely fictional.&lt;/p&gt;
&lt;p&gt;ChatGPT and other LLMs cannot perform fact-checking, evaluate the input/output, and use human judgment to ensure the accuracy, reliability, and appropriateness of the generated content.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/075aa9937f27eff88551b376d84f0137/36eca/bing-chatbot-sentient.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 50.950570342205324%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsTAAALEwEAmpwYAAABlklEQVR42j2Ry47UMBBF8+VIbBBb9ixA4gdmxTewHaYncfx+xY846SRDI2z3gnIaIR2VyiUd3Sq52/a8/yrbUbfjDlyPuu712p51O5tlr7ff9fuPt/df1o9ftw+f93efjm9Pt+Wo3Zx2bbyxUZvQsA2hJqk9oLQXyod5D2u28c9JbswlrPducun55+trP75chr7V/nIZYAJPGD6/9FDTsqetzNcSH6w1rNWvtcn9gEfM+x4jRIaB9IicPUWYQU+ogO3A8Sn7pfqluFSmpdE5t6CRYiwQYgMiI2ZoZJiINiR8GGmPqJ9BvoPTtFTtXG2qJtXO+5UyybkmRFAqGVeUKdBI6zVhaiTcTMkvd5tyY65mrvrkv2wg/CEDcAWhinKNqUIjV2Z2CzgZ0HNRsSFj6Xy4QqyUFqIgkwvDhAGHMk2FJcxgIrVdIPCfE7IMVYTCw3kzLAwbPk6FPTGDNDZi0WKx6AcqTIQlQRMhc18A5gv1pYtxN8ZbG6VySsNXR6hcwvcGoaPxm4uHTzfZtMxcpi5jl8ep8RfXKxek9eFJjAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Bing chatbot is freaking out, thinking it&amp;#39;s sentient.&quot;
        title=&quot;&quot;
        src=&quot;/static/075aa9937f27eff88551b376d84f0137/e996b/bing-chatbot-sentient.png&quot;
        srcset=&quot;/static/075aa9937f27eff88551b376d84f0137/17741/bing-chatbot-sentient.png 263w,
/static/075aa9937f27eff88551b376d84f0137/52211/bing-chatbot-sentient.png 525w,
/static/075aa9937f27eff88551b376d84f0137/e996b/bing-chatbot-sentient.png 1050w,
/static/075aa9937f27eff88551b376d84f0137/36eca/bing-chatbot-sentient.png 1526w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;To be or not to be?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Ah, and I almost forgot.&lt;/p&gt;
&lt;p&gt;Generative AI models may struggle with abstract reasoning and creative thinking. They excel at bringing up information from the training data but may struggle to provide novel or imaginative responses. Generating content beyond the scope of their training data is quite challenging.&lt;/p&gt;
&lt;p&gt;No, &lt;a href=&quot;https://openai.com/product/dall-e-2&quot;&gt;DALL·E&lt;/a&gt; will never become the next Picasso. There will always be a human with a creative idea behind it.&lt;/p&gt;
&lt;p&gt;As you can see, to say that AI will replace software engineers has its risks.&lt;/p&gt;
&lt;h2&gt;Staying ahead of the game&lt;/h2&gt;
&lt;p&gt;The most significant challenge we face with AI is creating awareness and educating ourselves about its capabilities and limitations. &lt;a href=&quot;https://www.bbc.com/news/world-us-canada-65616866&quot;&gt;Regulations&lt;/a&gt; and ethical guidelines should be established to ensure its development and usage are beneficial and not harmful. Collaboration between researchers, developers, and policymakers can help create a balanced approach to AI development.&lt;/p&gt;
&lt;p&gt;It’s our duty to inform our users about which parts of your application use generative AI, to avoid the risks associated with its limitations. If they know a response is baked by AI, they may understand that you never had the intention to produce harmful or biased content. A more ethical approach would be to let them have the option &lt;em&gt;not&lt;/em&gt; to use AI-generated content when it’s possible.&lt;/p&gt;
&lt;p&gt;In regards to privacy, it’s highly recommended not to share private and confidential information with AI models. Not only are you risking a leak of information, but you may end up giving away your intellectual property.&lt;/p&gt;
&lt;p&gt;On a personal level, it makes sense to start incorporating AI tools into your daily life. Just like we embraced PCs, smartphones, and social networks when they were introduced, Generative-AI is changing the game. Whisperers and copilots will help you become more competent and they’ll unlock new ways to learn things faster.&lt;/p&gt;
&lt;p&gt;Don’t blindly accept every code or answer AI is producing. Read it carefully and understand how to properly adapt it to your codebase. Always maintain a critical mindset when it comes to AI-generated content. Consider AI as a helpful tool that suggests potential answers, but remember to verify their accuracy. Fact-checking is crucial to avoid finding yourself in a &lt;a href=&quot;https://arstechnica.com/tech-policy/2023/05/lawyer-cited-6-fake-cases-made-up-by-chatgpt-judge-calls-it-unprecedented/&quot;&gt;difficult situation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Have an open eye on new tech. Vector databases and cognitive services are some good examples, to begin with. Allow your curiosity to drive you further.&lt;/p&gt;
&lt;p&gt;The software will not be the same as it was in the past decade. New businesses will be created, similar to how Web 2.0 changed the dynamics of our economy. Developing apps, video games, music, and various other creative content will become considerably more accessible. While downsizing may be tempting for some companies, the idea of expanding their product and service portfolio will also be compelling.&lt;/p&gt;
&lt;p&gt;Be the first to take on new opportunities. If something that differentiates you from other software engineers is your critical thinking and experience. That’s something AI doesn’t have. Use your superpower.&lt;/p&gt;
&lt;p&gt;The more you utilize these tools, the better you become at using them and grasping their limitations. In the end, they allow us to focus on what we do best; building software that solves people’s problems.&lt;/p&gt;
&lt;h2&gt;Navigating ‌Uncertainty&lt;/h2&gt;
&lt;p&gt;But wait a minute. There’s no smoke without fire. It can’t be that all the people went crazy from one day to another, right? There must be something there.&lt;/p&gt;
&lt;p&gt;I’ve been in multiple discussions with friends and colleagues about the risk of AI taking our jobs. I have to admit, I was also very skeptical. It takes courage to stay positive with everything that is happening these days.&lt;/p&gt;
&lt;p&gt;Some of us, the pessimists on this earth, have doubts about whether we’ll have a job to feed our families in the near future. The idea of losing our jobs or the fact of not staying relevant keeps us concerned. We picture the worst ideas in our heads and we try to react proactively to ensure we have a decent living.&lt;/p&gt;
&lt;p&gt;And some ‌of us, the optimists of this world, aren’t afraid that AI will get to the level of replacing humans. And if that happens we will have the tools and we’ll acquire the skills needed to build something fresh and exciting. We believe AI optimizes our work and opens up &lt;a href=&quot;https://markwschaefer.medium.com/20-entertaining-uses-of-chatgpt-you-never-knew-were-possible-3bc2644d4507&quot;&gt;new opportunities&lt;/a&gt;. It’s not a deadly virus, it’s a feature.&lt;/p&gt;
&lt;p&gt;Now, &lt;a href=&quot;https://www.youtube.com/watch?v=9XEnTxlBuGo&quot;&gt;which side am I on&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;Am I convinced that our future brings pain, poverty, and despair? Or am I looking forward to learning new tools and staying curious about what this technology brings?&lt;/p&gt;
&lt;p&gt;The harsh truth is, it’s up to us to spot the good side of things and aim for a better world, instead of fearing what the future will bring and taking no action on it.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/d7a5e263245fe7e093880ff4518475fe/cb93d/chatgpt-ai-replaces-developers.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 54.372623574144484%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAABYlAAAWJQFJUiTwAAABrklEQVR42oWSWZOaQBSF+UtTUcAF2QREdhfEhXEZR5Oq5CWpyt8/Od2ITFUefPg893b3Pdy2r/L2ZiCK15h6GSw7geOk1Fiq7SSYmBF6ffs1PUuiqJorC8fjGbSBB33oQ3+qD0330FMdFjn4pjb0vyDqBZpAn0KZugvY5ytm+wuWixPy5QmL1RkFtRC6vjy14n5FzZZnpNSU62l2wDzeIQgruN6Sht4KblJhXtTY7r5js72h2t1RVp9Yb644MK/JfnvHmfmR61vmf8oP/CJhViOYlfAC/m3+iob8cdnlPNpKM2G0EYbCmPEH9V5e8Zdmv8sLbjS50fzK/ERE91N/+YCGrXPC1uvTT+wOP5AV7/LqOa+YLI5IiiPjI/Z5jQvjkvsx98V6EG5kdy2K7eZoKCSWk8O0swcpTCvlSycSEVuk2+dkPOpaH+XVOIiXG44CiT70Xo6P0lft7vl1t9NHPDZCuNMMtt3M5H/ntBYxRjTsqSYG/LK4jmklEnnFZxzTNJIYk+h5piPmHGcYcY6Fl6INHBaHMMwA40nHyPBp0mCItS95y8jwmvOPXB84+AeuoZMLRg2YPQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;ChatGPT responds on what to do now that AI replaces developers.&quot;
        title=&quot;&quot;
        src=&quot;/static/d7a5e263245fe7e093880ff4518475fe/e996b/chatgpt-ai-replaces-developers.png&quot;
        srcset=&quot;/static/d7a5e263245fe7e093880ff4518475fe/17741/chatgpt-ai-replaces-developers.png 263w,
/static/d7a5e263245fe7e093880ff4518475fe/52211/chatgpt-ai-replaces-developers.png 525w,
/static/d7a5e263245fe7e093880ff4518475fe/e996b/chatgpt-ai-replaces-developers.png 1050w,
/static/d7a5e263245fe7e093880ff4518475fe/087e3/chatgpt-ai-replaces-developers.png 1575w,
/static/d7a5e263245fe7e093880ff4518475fe/8079d/chatgpt-ai-replaces-developers.png 2100w,
/static/d7a5e263245fe7e093880ff4518475fe/cb93d/chatgpt-ai-replaces-developers.png 2304w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;ChatGPT suggests staying optimistic. Do some upskilling, stay creative, and obviously learn to work with AI. That’s the only way to help it achieve its ultimate goal. Rule the world.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I really find Simon Sinek’s speech very inspiring; the human brain &lt;a href=&quot;https://www.youtube.com/watch?v=W05FYkqv7hM&quot;&gt;cannot comprehend the negative&lt;/a&gt;. It’s in our DNA. We spend so much energy worrying about everything that is happening, and we forget that &lt;em&gt;we&lt;/em&gt; can make this world a better place.&lt;/p&gt;
&lt;p&gt;From that perspective, it doesn’t really pay off thinking as a pessimist here, does it?&lt;/p&gt;
&lt;p&gt;Sounds too good to be true? Well, give it &lt;a href=&quot;https://signalvnoise.com/posts/3124-give-it-five-minutes&quot;&gt;five minutes&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Master Namespaces in Typescript]]></title><description><![CDATA[Learn how to use namespaces and how they help in creating type definition files.]]></description><link>http://www.nicotsou.com/tltr-typescript-namespaces/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-namespaces/</guid><pubDate>Thu, 27 Apr 2023 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;I have to admit something. I hate namespaces in TypeScript. I hate them so much that I wrote this article to explain why you shouldn’t use them.&lt;/p&gt;
&lt;p&gt;Cookie? 🍪&lt;/p&gt;
&lt;h2&gt;What’s a namespace? 🙋‍♀️&lt;/h2&gt;
&lt;p&gt;In TypeScript, a namespace is a way to organize your code and prevent naming collisions.&lt;/p&gt;
&lt;p&gt;In the early versions of the TypeScript language, its developers had to find a way to modularize code. That’s why they introduced Modules. But those modules back then were completely different from the actual ES Modules we all love.&lt;/p&gt;
&lt;p&gt;Yes, there was a naming collision in the name of the feature that promised to solve naming collisions.&lt;/p&gt;
&lt;p&gt;So TypeScript developers had to re-brand the whole thing. That’s how namespaces were invented.&lt;/p&gt;
&lt;p&gt;The concept is very similar to how modules work in TypeScript. However, it has some significant differences that make it difficult to use on a daily basis. Therefore, namespaces are only used to define types for existing JavaScript code.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;And that’s the deal with namespaces; they help you create type definitions.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So unless you’re authoring your own custom &lt;code class=&quot;language-text&quot;&gt;.d.ts&lt;/code&gt; type definition for a library that’s not written in TypeScript, or you’re one of the proud contributors of the &lt;a href=&quot;https://github.com/DefinitelyTyped/DefinitelyTyped&quot;&gt;DefinitelyTyped&lt;/a&gt; packages, you should not use namespaces.&lt;/p&gt;
&lt;p&gt;I repeat. You should &lt;em&gt;not&lt;/em&gt; use namespaces to structure your code.&lt;/p&gt;
&lt;p&gt;Assuming that you’re working on a modern TypeScript project, and you’re using ES Modules for structuring your code, that already solves your problems in a &lt;a href=&quot;https://www.notion.so/Don-t-Use-Namespaces-in-Typescript-c463fe77705b4fcda71a83cbd0b40600&quot;&gt;declarative&lt;/a&gt; way. A namespace on the other hand is imperative and much more difficult to deal with.&lt;/p&gt;
&lt;p&gt;But I know why you’re still reading this article. You want to know everything about namespaces. Let me present to you how namespaces work and why they &lt;em&gt;may&lt;/em&gt; be useful.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a0f6ad368d65b39f2376c38e40998b13/e5166/watercolor-1.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAQFAQP/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAHF5lITOIf/xAAbEAACAgMBAAAAAAAAAAAAAAACAwASAQQTMv/aAAgBAQABBQIT1qZTUHe45p8J/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAIBAAAQIFBQAAAAAAAAAAAAAAAQAQAgMRITESIjJRcf/aAAgBAQAGPwKHjYdKumxwsAeNK3N//8QAGRABAAMBAQAAAAAAAAAAAAAAAQARITFB/9oACAEBAAE/IVlQaN1NQldJ5KZMBgh7O8abFVt7P//aAAwDAQACAAMAAAAQkA//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAaEAEAAwEBAQAAAAAAAAAAAAABABEhQTFh/9oACAEBAAE/EEadJctW7W7K90ca+GuWSqigsR537PA+XDiabg1CjkRIq1Xs/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;watercolor 1&quot;
        title=&quot;&quot;
        src=&quot;/static/a0f6ad368d65b39f2376c38e40998b13/9ecec/watercolor-1.jpg&quot;
        srcset=&quot;/static/a0f6ad368d65b39f2376c38e40998b13/5d001/watercolor-1.jpg 263w,
/static/a0f6ad368d65b39f2376c38e40998b13/7349d/watercolor-1.jpg 525w,
/static/a0f6ad368d65b39f2376c38e40998b13/9ecec/watercolor-1.jpg 1050w,
/static/a0f6ad368d65b39f2376c38e40998b13/e5166/watercolor-1.jpg 1200w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Namespaces are like abstract art. You can mix and match different shapes of objects and lines of code.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;How to use a namespace? 🧑🏿‍💻&lt;/h2&gt;
&lt;p&gt;Consider the following types:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; HttpStatusCode &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token constant&quot;&gt;OK&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token constant&quot;&gt;BAD_REQUEST&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  message&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  stack&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;They may be used in your application to describe status codes, errors, and users accordingly.&lt;/p&gt;
&lt;p&gt;The code above will declare these types into the Global Scope, which means that the types will be accessible from any other TypeScript file in your project, without having to import them.&lt;/p&gt;
&lt;p&gt;Normally, we have the tendency to export those types:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And then import them into other files:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; User &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./types&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Namespaces wrap everything inside an object.&lt;/p&gt;
&lt;p&gt;The idea is simple. You just use the keyword &lt;code class=&quot;language-text&quot;&gt;namespace&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; MyApp &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; HttpStatusCode &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;OK&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;BAD_REQUEST&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    message&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
    stack&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, everything lives under the &lt;code class=&quot;language-text&quot;&gt;MyApp&lt;/code&gt; namespace. All the types are now invisible from the outer scope.&lt;/p&gt;
&lt;p&gt;Mind the &lt;code class=&quot;language-text&quot;&gt;export&lt;/code&gt; keyword. Only what’s exported is accessible to the outer world.&lt;/p&gt;
&lt;p&gt;To access those files, we can simply use their namespace:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;MyApp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Error&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the same file, I can use the same namespace &lt;code class=&quot;language-text&quot;&gt;MyApp&lt;/code&gt; to include additional types. But not only types, I can put any valid JavaScript code as well:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; MyApp &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; HttpStatusCode &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;OK&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;BAD_REQUEST&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    message&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
    stack&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; MyApp &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;youwillneverknow&apos;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;TypeScript will &lt;em&gt;merge&lt;/em&gt; these two blocks of code into a single one, resulting in a namespace that includes the 3 types we declared above, plus the &lt;code class=&quot;language-text&quot;&gt;API_KEY&lt;/code&gt; variable.&lt;/p&gt;
&lt;p&gt;That basically allows you to use the &lt;code class=&quot;language-text&quot;&gt;namespace&lt;/code&gt; keyword in &lt;em&gt;any&lt;/em&gt; file to include additional items to an existing namespace. Pretty cool right?&lt;/p&gt;
&lt;p&gt;Right? 😛&lt;/p&gt;
&lt;p&gt;Mind the &lt;code class=&quot;language-text&quot;&gt;export&lt;/code&gt; here too. Again, if you want to expose something you’ll need to export it.&lt;/p&gt;
&lt;p&gt;Similar to how we access types, to access our variable, we’ll have to use its namespace as a prefix:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// ❌ Cannot find name &apos;API_KEY&apos;&lt;/span&gt;
&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MyApp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// ✅ &apos;youwillneverknow&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here’s another example on how we use the &lt;code class=&quot;language-text&quot;&gt;User&lt;/code&gt; type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; MyApp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;User &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nicos&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Yes. That’s how your favorite library’s type definitions are being built. They use namespaces.&lt;/p&gt;
&lt;h2&gt;Compiler magic 🔮&lt;/h2&gt;
&lt;p&gt;Try this at home: Compile the previous code to JavaScript. You will realize something really interesting. Namespaces are available at runtime.&lt;/p&gt;
&lt;p&gt;Wait, that requires a fancy quote.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Namespaces are simply named JavaScript objects in the global namespace.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Let me try to decode that for you. Here’s the result after compiling our namespace:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&apos;use strict&apos;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Declaring a global variable for our namespace&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// -- Oh boy, that looks so 90&apos;s!&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; MyApp

  &lt;span class=&quot;token comment&quot;&gt;// This pattern is called Immediately Invoked Function Expressions&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// -- Again, so 90&apos;s!&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;MyApp&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// declaring our enum HttpStatusCode&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; HttpStatusCode
  &lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;HttpStatusCode&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    HttpStatusCode&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;HttpStatusCode&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;OK&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;OK&apos;&lt;/span&gt;
    HttpStatusCode&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;HttpStatusCode&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;BAD_REQUEST&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;BAD_REQUEST&apos;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;HttpStatusCode &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; MyApp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;HttpStatusCode &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MyApp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;HttpStatusCode &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token comment&quot;&gt;// Declaring our Error class&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  MyApp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Error &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Error

  &lt;span class=&quot;token comment&quot;&gt;// The pattern continues:&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;//   If MyApp is declared, use it&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;//   else, instantiate it as an object&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MyApp &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MyApp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Again, the same pattern adds our API_KEY&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;MyApp&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  MyApp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;youwillneverknow&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MyApp &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MyApp &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Types have been removed.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// They are not valid JavaScript.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; user &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nicos&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Our namespace is actually... an object!&lt;/span&gt;
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MyApp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This proves that a namespace is not an abstract TypeScript-only type, but an actual object that lives at runtime.&lt;/p&gt;
&lt;h2&gt;Working with multi-file namespaces 🗂️&lt;/h2&gt;
&lt;p&gt;So far we’ve been working on a single file. What if I wanted to access my namespaces across multiple files? This will uncover the bad parts of namespaces.&lt;/p&gt;
&lt;p&gt;Don’t say I didn’t warn you.&lt;/p&gt;
&lt;p&gt;Consider the following code:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; MyService &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;youwillneverknow&apos;&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; HttpStatusCodes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;OK&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;BAD_REQUEST&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ErrorResponse&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        message&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
        errorCode&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
    email&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;fetchUser&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;users/me&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; MyService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;User &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nicos&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MyService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let’s break our app into the following files:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
├── MyService
├───── ErrorResponse&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ts
├───── HttpStatusCodes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ts
├───── index&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ts
├───── User&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ts
├── main&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ts&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The intention is that each file will extend our namespace with some extra logic. This will hypothetically help us long term as we keep adding more code.&lt;/p&gt;
&lt;p&gt;The folder &lt;code class=&quot;language-text&quot;&gt;MyService&lt;/code&gt; will group all the contents of the namespace together.&lt;/p&gt;
&lt;p&gt;Inside &lt;code class=&quot;language-text&quot;&gt;MyService/ErrorResponses.ts&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; MyService &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ErrorResponse&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        message&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
        errorCode&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Inside &lt;code class=&quot;language-text&quot;&gt;MyService/HttpStatusCodes.ts&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; MyService &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; HttpStatusCodes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;OK&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token constant&quot;&gt;BAD_REQUEST&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Inside &lt;code class=&quot;language-text&quot;&gt;MyService/index.ts&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; MyService &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;youwillneverknow&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Inside &lt;code class=&quot;language-text&quot;&gt;MyService/User.ts&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; MyService &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;fetchUser&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;users/me&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Inside &lt;code class=&quot;language-text&quot;&gt;main.ts&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; MyService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;User &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nicos&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MyService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Inside &lt;code class=&quot;language-text&quot;&gt;Error.ts&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; MyService &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Error&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    message&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
    stack&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Inside &lt;code class=&quot;language-text&quot;&gt;user.ts&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; MyService &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fetchUser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; response &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;users/me&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; response&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And finally, our &lt;code class=&quot;language-text&quot;&gt;main.ts&lt;/code&gt; file:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; User&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;User &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nicos&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MyService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The code above will declare a &lt;code class=&quot;language-text&quot;&gt;MyService&lt;/code&gt; namespace globally and each file will modify it by adding additional items. The result will be that our &lt;code class=&quot;language-text&quot;&gt;index.ts&lt;/code&gt; file will have access to our namespace &lt;code class=&quot;language-text&quot;&gt;MyService&lt;/code&gt;, without importing it in any way.&lt;/p&gt;
&lt;p&gt;Now, you may think that’s much easier compared to manually exporting and importing items. And maybe it is if you have a single namespace as in our example above. However, the situation becomes unmanageable as we add more logic to our codebase.&lt;/p&gt;
&lt;p&gt;Normally codebases for libraries have relatively small codebases. Applications on the other hand have a lot of different views and objects that would be extremely impractical to keep them on the global scope.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/43d891dfa7ed9ff4a6af6b8a9f9e78ab/e5166/watercolor-2.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIEAwX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAGWvnIYCh//xAAaEAADAAMBAAAAAAAAAAAAAAABAgMABBET/9oACAEBAAEFAtSQrlIhVI4dZ2Sno7HP/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAHBAAAgICAwAAAAAAAAAAAAAAAAECETFREBIi/9oACAEBAAY/ApbWx9lgo8vKJNyvj//EABoQAAMBAQEBAAAAAAAAAAAAAAABESExQWH/2gAIAQEAAT8hgInB/BGeJ4is3Vg8YUo3FN3Ss//aAAwDAQACAAMAAAAQCw//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAbEAEBAAMBAQEAAAAAAAAAAAABEQAhMWGB0f/aAAgBAQABPxAbtgFB+4xpRsvkcePFqNKemFykAg2bO5BFEVQeaOGemf/Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;watercolor 2&quot;
        title=&quot;&quot;
        src=&quot;/static/43d891dfa7ed9ff4a6af6b8a9f9e78ab/9ecec/watercolor-2.jpg&quot;
        srcset=&quot;/static/43d891dfa7ed9ff4a6af6b8a9f9e78ab/5d001/watercolor-2.jpg 263w,
/static/43d891dfa7ed9ff4a6af6b8a9f9e78ab/7349d/watercolor-2.jpg 525w,
/static/43d891dfa7ed9ff4a6af6b8a9f9e78ab/9ecec/watercolor-2.jpg 1050w,
/static/43d891dfa7ed9ff4a6af6b8a9f9e78ab/e5166/watercolor-2.jpg 1200w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;More abstract art ^_^&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;To simulate how a real application works, let’s do some compiler adjustments.&lt;/p&gt;
&lt;p&gt;We want to have a single &lt;code class=&quot;language-text&quot;&gt;index.ts&lt;/code&gt; file as a starting point. We also want to configure an output folder. Let’s modify our &lt;code class=&quot;language-text&quot;&gt;tsconfig.json&lt;/code&gt; file to configure our compiler accordingly:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token string-property property&quot;&gt;&quot;include&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;./index.ts&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 	&lt;span class=&quot;token comment&quot;&gt;/* We will use this file as a root */&lt;/span&gt;&lt;/span&gt;	&lt;span class=&quot;token string-property property&quot;&gt;&quot;compilerOptions&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;		&lt;span class=&quot;token string-property property&quot;&gt;&quot;outDir&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;./dist&quot;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* The compiler will output the files here */&lt;/span&gt;&lt;/span&gt;	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Since TypeScript doesn’t compile all the files in your project anymore, it has no clue where to find the &lt;code class=&quot;language-text&quot;&gt;MyApp&lt;/code&gt; namespace. Therefore, our code will not compile anymore.&lt;/p&gt;
&lt;p&gt;And the million-dollar question is how to tell TypeScript where to find that bloody &lt;code class=&quot;language-text&quot;&gt;MyService&lt;/code&gt; namespace?&lt;/p&gt;
&lt;p&gt;Sure, we could eventually export it and import it, but this will not work. So what do we do? How do we connect those namespaces? Well, there is a way. Not the one you would expect to use on a modern project, but it does the job.&lt;/p&gt;
&lt;p&gt;In &lt;code class=&quot;language-text&quot;&gt;main.ts&lt;/code&gt; we’ll have to add the following comment:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/// &amp;lt;reference path=&quot;MyService/index.ts&quot; /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; MyService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;User &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nicos&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;MyService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is not just a comment for TypeScript, since it starts with a triple slash. It’s called &lt;strong&gt;&lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html&quot;&gt;triple-slash directive&lt;/a&gt;&lt;/strong&gt;. And yes, it’s XML syntax. It basically tells TypeScript which namespace we want to &lt;em&gt;reference&lt;/em&gt;. Kinda acts like an &lt;code class=&quot;language-text&quot;&gt;import&lt;/code&gt; statement.&lt;/p&gt;
&lt;p&gt;The code above will import the MyService index file. But we haven’t done yet. We need to edit that file to help TypeScript connect the dots and include all the other files:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/// &amp;lt;reference path=&quot;User.ts&quot; /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/// &amp;lt;reference path=&quot;HttpStatusCodes.ts&quot; /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/// &amp;lt;reference path=&quot;ErrorResponse.ts&quot; /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; MyService &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;youwillneverknow&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we had to reference all the other namespace files.&lt;/p&gt;
&lt;p&gt;I know what you’re thinking. This doesn’t look like JavaScript. It kinda reminds C#. Not a coincidence if you consider that the creator of TypeScript was Anders Hejlsberg, the creator of C#.&lt;/p&gt;
&lt;p&gt;And that’s one of the reasons TypeScript developers are trying to move away from namespaces and use modules instead.&lt;/p&gt;
&lt;p&gt;They’re just ugly.&lt;/p&gt;
&lt;h2&gt;Nested namespaces 🪆&lt;/h2&gt;
&lt;p&gt;Now that you realized that you spent some minutes of your life reading an article that’s useless to you, I have one more last thing to share about namespaces.&lt;/p&gt;
&lt;p&gt;They can be nested. Hooray! 🥳&lt;/p&gt;
&lt;p&gt;Well, to be fair. It’s just plain JavaScript objects at the end. You can build them the way you want.&lt;/p&gt;
&lt;p&gt;Here’s a demo of a nested namespace:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; MyApp &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; MyService &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;youwillneverknow&apos;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Mind the &lt;code class=&quot;language-text&quot;&gt;export&lt;/code&gt; keyword before the &lt;code class=&quot;language-text&quot;&gt;MyService&lt;/code&gt; namespace. Remember what we said before? Only what’s exported is visible to the outer world.&lt;/p&gt;
&lt;p&gt;And here’s how we can access that &lt;code class=&quot;language-text&quot;&gt;API_KEY&lt;/code&gt; variable:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;MyApp&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;MyService&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Ambient Namespaces 👻&lt;/h2&gt;
&lt;p&gt;Sometimes you want to refer to a specific variable that exists in the outer scope of your application. Something you can’t control from within your codebase.&lt;/p&gt;
&lt;p&gt;Many old-school JavaScript developers understand what I’m talking about. For the others, consider the example of the &lt;code class=&quot;language-text&quot;&gt;window&lt;/code&gt; object. It’s not in our app, it’s in the browser itself. But how can I tell TypeScript to understand its API? How can I add type definitions for something outside of my application?&lt;/p&gt;
&lt;p&gt;Yes, I shouldn’t have spoiled this.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;An ambient namespace is like an abstract class. It defines what’s inside, minus the implementation.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;To create an ambient namespace, you have to rename your file from &lt;code class=&quot;language-text&quot;&gt;.ts&lt;/code&gt; to &lt;code class=&quot;language-text&quot;&gt;.d.ts&lt;/code&gt;. D here stands for type &lt;em&gt;declaration&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;From there, you can use the &lt;code class=&quot;language-text&quot;&gt;declare&lt;/code&gt; keyword:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;declare&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;namespace&lt;/span&gt; window &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Console&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;data&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This enables developers to write their own type definition files, which can be extremely useful for codebases that aren’t written in TypeScript, such as older JavaScript libraries like &lt;a href=&quot;https://github.com/jquery/jquery&quot;&gt;jQuery&lt;/a&gt; and &lt;a href=&quot;https://github.com/moment/moment&quot;&gt;moment.js&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;That’s exactly how the popular repo &lt;a href=&quot;https://github.com/DefinitelyTyped/DefinitelyTyped&quot;&gt;&lt;strong&gt;DefinitelyTyped&lt;/strong&gt;&lt;/a&gt; is written. For those who aren’t familiar, this is a repository for high-quality TypeScript type definitions.&lt;/p&gt;
&lt;p&gt;You may have used one of these definitions already in your project if you have installed an npm package that has the prefix &lt;code class=&quot;language-text&quot;&gt;@types/&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;npm install &lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;save&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;dev &lt;span class=&quot;token decorator&quot;&gt;&lt;span class=&quot;token at operator&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;types&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt;jquery&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Namespaces vs modules 🤺&lt;/h2&gt;
&lt;p&gt;It’s useful to compare namespaces to modules, to understand where they differ and where they’re alike.&lt;/p&gt;
&lt;p&gt;They both:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Help you to organize your code into separate files.&lt;/li&gt;
&lt;li&gt;Avoid conflicts in the global scope, since they both execute in their local scope.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Modules are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Declarative and easy to reason about.&lt;/li&gt;
&lt;li&gt;More suitable for modern applications.&lt;/li&gt;
&lt;li&gt;ECMAScript standard, which means it’s actually a JavaScript feature.&lt;/li&gt;
&lt;li&gt;More flexible to build a reusable api.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Namespaces are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Hard to work with and reason about.&lt;/li&gt;
&lt;li&gt;Suitable for creating type definitions.&lt;/li&gt;
&lt;li&gt;Typing existing JavaScript applications that aren’t migrated to TypeScript.&lt;/li&gt;
&lt;li&gt;Unlike modules, they can span multiple files and can be concatenated using &lt;a href=&quot;https://www.typescriptlang.org/tsconfig#outFile&quot;&gt;outFile&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Namespaces support ambient declarations, that can help you define types outside the scope of your application.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;TypeScript 5.0 is moving away from namespaces 🗞️&lt;/h2&gt;
&lt;p&gt;TypeScript 5.0 has undergone a major infrastructure change where the entire codebase has been restructured to use ECMAScript modules, instead of namespaces, which had limitations and made it difficult for other tools to support TypeScript&lt;/p&gt;
&lt;p&gt;As a result, TypeScript 5.0 will run faster and take up less space, with build times being cut down by 10-25%. This change is mainly aimed at contributors of TypeScript, but general users and API consumers may also benefit from the reduced package size and faster build times.&lt;/p&gt;
&lt;p&gt;If you want to learn more about this migration, &lt;a href=&quot;https://devblogs.microsoft.com/typescript/typescripts-migration-to-modules/&quot;&gt;this blog post&lt;/a&gt; describes the runtime performance impact of namespaces and how bundlers emulate scopes. It explains how the use of namespaces can create unnecessary boilerplate code and indirection, leading to a decrease in performance due to the runtime cost of invoking methods off of an object.&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;Namespaces are used &lt;em&gt;only&lt;/em&gt; for creating type definitions for existing JavaScript applications, that can’t be refactored to TypeScript. They help you control the pollution of the global scope, and to reduce naming collisions. They also embrace reusability.&lt;/p&gt;
&lt;p&gt;Namespaces could help you structure your code in a Web Application, with all dependencies included as &lt;code class=&quot;language-text&quot;&gt;&amp;lt;script&gt;&lt;/code&gt; tags in your HTML page.&lt;/p&gt;
&lt;p&gt;Modules are a much more modern approach for apps written in TypeScript. They solve all the problems mentioned in the previous paragraph but in a declarative way.&lt;/p&gt;
&lt;p&gt;Many thanks to &lt;a href=&quot;https://unsplash.com/photos/eyGyeByOjig&quot;&gt;Niklas Liniger&lt;/a&gt; for the amazing cover art.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[TLTR; TypeScript Modules]]></title><description><![CDATA[Destructuring is one of these _aha_ moments you have as a software engineer.]]></description><link>http://www.nicotsou.com/tltr-typescript-modules/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-modules/</guid><pubDate>Thu, 23 Mar 2023 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;Every application, even the tiniest one, will eventually need to break code apart into multiple files. I mean, who likes to read thousands of lines of code in a single file, right?&lt;/p&gt;
&lt;p&gt;Other languages use the term packages or assemblies. JavaScript uses &lt;em&gt;modules&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The truth is, it took us many years to reach that point. JavaScript was developed back in the 90’s to give interactivity to plain websites. In the past days closures and self-calling functions have been used to introduce the idea of modularity. Then we had some abstractions such as RequireJS, AMD, and CommonJS, which was the default way Node.js was using for modular code. Since EcmaScript 2015 (ES6), we finally have the notion of a module.&lt;/p&gt;
&lt;p&gt;TypeScript takes modules to the next level. Not only it supports the basic functionality that ES6 prescribes, but it allows you to write modular types as well.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Modules is all about structuring your application in multiple files.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In this article, we will not only learn how modules work, but I will provide some really useful features and patterns to structure your code. And we will practice what we’ve learned, so make sure you’ve turned off your notifications for a while, to better focus on studying modules. At the end of the article, we will also compare ES Modules to CommonJs, which is still a popular way to structure code in Node.js applications.&lt;/p&gt;
&lt;p&gt;Gelato? 🍨&lt;/p&gt;
&lt;h2&gt;Named exports&lt;/h2&gt;
&lt;p&gt;You can export modules from any TypeScript/JavaScript file. Here are a couple of examples:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// exports.ts&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;ABC&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This kind of export is called &lt;em&gt;named&lt;/em&gt;, because you give it a name. So, your &lt;code class=&quot;language-text&quot;&gt;export.ts&lt;/code&gt; file exports a function &lt;code class=&quot;language-text&quot;&gt;add()&lt;/code&gt;, the constant &lt;code class=&quot;language-text&quot;&gt;API_KEY&lt;/code&gt;, and the &lt;code class=&quot;language-text&quot;&gt;Person&lt;/code&gt; class.&lt;/p&gt;
&lt;p&gt;TypeScript also allows you to export types, such as type aliases, interfaces, enums, etc.:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// exports.ts&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;ABC&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Target&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we are exporting the type alias &lt;code class=&quot;language-text&quot;&gt;Target&lt;/code&gt;, again as a named export.&lt;/p&gt;
&lt;p&gt;This file has access to these instances, as long as you follow the basic &lt;a href=&quot;https://www.w3schools.com/js/js_scope.asp&quot;&gt;scope&lt;/a&gt; principles.&lt;/p&gt;
&lt;p&gt;To import these elements into another file, here’s what you could do:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// index.ts&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Target &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./exports&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here &lt;code class=&quot;language-text&quot;&gt;index.ts&lt;/code&gt; imports only the &lt;code class=&quot;language-text&quot;&gt;API_KEY&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;Target&lt;/code&gt; from &lt;code class=&quot;language-text&quot;&gt;exports.ts&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Useful notes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We use &lt;a href=&quot;/destructuring&quot;&gt;destructuring&lt;/a&gt; to pick only the named exports that we want to use.&lt;/li&gt;
&lt;li&gt;We specify the relative path of the file that we want to import.&lt;/li&gt;
&lt;li&gt;For &lt;code class=&quot;language-text&quot;&gt;ts&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;js&lt;/code&gt; files no extension is required. Although, it will technically work, it’s common not to use it. For other types of files (such as &lt;code class=&quot;language-text&quot;&gt;css&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;svg&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;json&lt;/code&gt;, etc.) the extension is mandatory. That’s the common behavior when you use a module bundler like &lt;a href=&quot;https://webpack.js.org&quot;&gt;webpack&lt;/a&gt; for example.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Practically, we can have as many &lt;code class=&quot;language-text&quot;&gt;Target&lt;/code&gt; types as we want in our project. As long as we are exporting them, they’re not going to cause any conflicts. Since we choose the file we want to import from, we are always sure that we’re not going to import something irrelevant.&lt;/p&gt;
&lt;p&gt;By selectively importing &lt;em&gt;only&lt;/em&gt; the items we’re going to consume, we actively improve the performance of the application. Modern bundlers like &lt;a href=&quot;https://webpack.js.org/guides/tree-shaking/&quot;&gt;webpack&lt;/a&gt; will only include these parts of the code, and not the entire module file. This reduces the file size of the application bundle. And you know, the smaller the files you have to download, the faster the application will load.&lt;/p&gt;
&lt;h2&gt;Default export&lt;/h2&gt;
&lt;p&gt;Every file has a &lt;em&gt;default&lt;/em&gt; export slot:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;playlist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that we used the keyword &lt;code class=&quot;language-text&quot;&gt;default&lt;/code&gt;, to indicate that this is a &lt;em&gt;default&lt;/em&gt; export.&lt;/p&gt;
&lt;p&gt;We use the same keyword for import:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;myUtil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That’s basically a reserved spot, which simplifies our export/import statements.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Default exports work similar to the return keyword in a function.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If within a file you don’t want to export more than one element, you can use the &lt;code class=&quot;language-text&quot;&gt;default&lt;/code&gt; keyword for simplicity.&lt;/p&gt;
&lt;p&gt;Another use case is when you want to separate your primary logic from the helper functions:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MyComponentProps&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;MyComponent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; name &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; MyComponentProps&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;JSX&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Element &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; MyComponent&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For example, when we’re implementing a React component similar to the one above, it’s common to use the default export for the component itself, having in mind that there’s going to be a single component per file. We normally export other elements (such as helper functions, types etc.) with named exports.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/5621d2877b552a77a1adb96aa1c1ec96/0f98f/retro-car.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAQCBQb/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAFtN6BTmhD/xAAbEAABBAMAAAAAAAAAAAAAAAABAgMSEwAEEP/aAAgBAQABBQJ2czbGhzFNBZGsgc//xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAgBAwEBPwGq/8QAFREBAQAAAAAAAAAAAAAAAAAAECH/2gAIAQIBAT8Bh//EABwQAAICAgMAAAAAAAAAAAAAAAABESECMRAyQf/aAAgBAQAGPwKqxjclZS/TqyW2bfH/xAAcEAADAAEFAAAAAAAAAAAAAAAAAREhMUFhgZH/2gAIAQEAAT8hSaaHcMdcU3HYnTKqajmr1LD/2gAMAwEAAgADAAAAEG//AP/EABcRAQADAAAAAAAAAAAAAAAAAAEQESH/2gAIAQMBAT8QsYQ//8QAFxEAAwEAAAAAAAAAAAAAAAAAARARMf/aAAgBAgEBPxCaK//EABsQAAIDAQEBAAAAAAAAAAAAAAERACExUdGR/9oACAEBAAE/ELYrAMFXm7L2jYxNt+R1vpa9jcABIM5kRFpY8j0BKHS5/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;A retro car&quot;
        title=&quot;&quot;
        src=&quot;/static/5621d2877b552a77a1adb96aa1c1ec96/9ecec/retro-car.jpg&quot;
        srcset=&quot;/static/5621d2877b552a77a1adb96aa1c1ec96/5d001/retro-car.jpg 263w,
/static/5621d2877b552a77a1adb96aa1c1ec96/7349d/retro-car.jpg 525w,
/static/5621d2877b552a77a1adb96aa1c1ec96/9ecec/retro-car.jpg 1050w,
/static/5621d2877b552a77a1adb96aa1c1ec96/39d31/retro-car.jpg 1575w,
/static/5621d2877b552a77a1adb96aa1c1ec96/0f98f/retro-car.jpg 1920w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The concept of modules is similar to how apart from multiple smaller components. Credit: &lt;a href=&quot;https://unsplash.com/@danielsalgado&quot;&gt;Daniel Salgado&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Import statements for Pros&lt;/h2&gt;
&lt;p&gt;You can rename an import:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// index.ts&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; apiKey &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./exports.ts&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;API_KEY&lt;/code&gt; variable becomes &lt;code class=&quot;language-text&quot;&gt;apiKey&lt;/code&gt; in this file. This may help you to avoid conflicts, when multiple modules are exporting something with the same name.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;You can also use the &lt;code class=&quot;language-text&quot;&gt;*&lt;/code&gt; wildcard to import everything:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// index.ts&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; exports &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./exports.ts&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; key &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; exports&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we are importing everything that &lt;code class=&quot;language-text&quot;&gt;exports.ts&lt;/code&gt; has to offer and we give them the alias &lt;code class=&quot;language-text&quot;&gt;exports&lt;/code&gt;. This technique can be helpful when building index files. We will review the index files at the end of this article.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;We can also import the complete files:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// inside init.ts&lt;/span&gt;
&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;initializing...&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// inside index.ts&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./init&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will execute everything that’s inside the &lt;code class=&quot;language-text&quot;&gt;init.ts&lt;/code&gt; file, without importing any of its exports.&lt;/p&gt;
&lt;p&gt;It may be useful when you want to import global styles, or to initialize something at the root of your application.&lt;/p&gt;
&lt;h2&gt;Something to avoid&lt;/h2&gt;
&lt;p&gt;It’s common with ES Modules to destructure import statements by selecting what we want to import. As we’ve seen this not only improves readability, but it helps reduce the size of the application bundles, by improving the overall load times.&lt;/p&gt;
&lt;p&gt;I’ve seen a lot of developers fall into the trap of exporting a single object at the end of the file, by destructuring them to a single object:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;ABC&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;	add&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;	&lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;	Person&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will work similar to what we had before. It’s technically possible to import a specific element from that file:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// index.ts&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;API_KEY&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./exports&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, most bundlers may treat this as a single object, and this will result in importing the complete file rather than importing specific parts. It’s also against ECMAScript standards.&lt;/p&gt;
&lt;p&gt;Two reasons to convince your team that such export statements should be avoided.&lt;/p&gt;
&lt;h2&gt;Installing and using Packages&lt;/h2&gt;
&lt;p&gt;The fun with TypeScript begins when you’re starting to explore third-party packages. There is literally everything out there.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“&lt;strong&gt;Any application that can be written in JavaScript, will eventually be written in JavaScript”
— Jeff Atwood’s Law&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You can install libraries by running the following command:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-bash line-numbers&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; react&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will add a new dependency in your &lt;code class=&quot;language-text&quot;&gt;package.json&lt;/code&gt; file:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-json line-numbers&quot;&gt;&lt;code class=&quot;language-json&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	...
  &lt;span class=&quot;token property&quot;&gt;&quot;dependencies&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;react&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;^18.2.0&quot;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It will also create a &lt;code class=&quot;language-text&quot;&gt;node_modules&lt;/code&gt; folder inside the application folder with the contents of the package. Sometimes you will find the actual source code of the package. Other times you will find the package transpiled. That depends on how the developer decided to distribute it.&lt;/p&gt;
&lt;p&gt;To use a library, simply import from your &lt;code class=&quot;language-text&quot;&gt;node_modules&lt;/code&gt; dependencies:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; React &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;react&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;No prefix is required, the name of the package is usually the one in your &lt;code class=&quot;language-text&quot;&gt;package.json&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You can mix and match named and default exports:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; React&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; Component &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;react&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Creating packages is not the scope of this article. Ping me if you want me to write an article about them. ☺️&lt;/p&gt;
&lt;h2&gt;Dynamic imports&lt;/h2&gt;
&lt;p&gt;As your app grows, more and more modules will be introduced. There’s a big drawback here. The more modules we use, the bigger the application bundle becomes, the more time the users have to wait for the app to load.&lt;/p&gt;
&lt;p&gt;To speed-up loading times, it’s a common pattern to optimize an application based on routes. For example, if the user is viewing the settings screen, there’s no reason to load the dashboard screen, which may import tons of third-party libraries for displaying charts.&lt;/p&gt;
&lt;p&gt;To do that we can utilize dynamic imports. The idea is simple. Somewhere in your app there is a logic, and when it evaluates to true, the import will happen:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;asyncImport&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; myModule &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;./math&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// additional code goes here&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, our users will not have to download that &lt;code class=&quot;language-text&quot;&gt;math.ts&lt;/code&gt; module. Calling this function will trigger its download. The &lt;code class=&quot;language-text&quot;&gt;import()&lt;/code&gt; function returns a &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise&quot;&gt;Promise&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This is another interesting feature that I’m willing to discuss in another article. Until then you can check &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import&quot;&gt;MDN&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Modules and immutability&lt;/h2&gt;
&lt;p&gt;Somebody may think that if we import a variable from another file, it automatically makes it immutable, since this is a new file. I’m afraid that’s not the case in JavaScript.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Module imports in JavaScript are &lt;em&gt;not&lt;/em&gt; immutable.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;They can be reassigned to new values if desired. However, it is recommended to keep imports as &lt;strong&gt;constant&lt;/strong&gt; variables in order to avoid accidental reassignments and ensure predictable behavior in the code.&lt;/p&gt;
&lt;p&gt;Consider the following example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// module.ts&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nicos&apos;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// index.ts&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; name &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./module&apos;&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// @ts-ignore&lt;/span&gt;
name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Oops&apos;&lt;/span&gt;
&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &quot;Oops&quot;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The example above demonstrates that it is technically possible to import a variable and alter it on the fly. Thankfully, TypeScript will not tolerate such bad practice. That’s why I used the &lt;code class=&quot;language-text&quot;&gt;@ts-ignore&lt;/code&gt; flag to disable type checks on this line.&lt;/p&gt;
&lt;p&gt;Never, ever export anything else than a &lt;code class=&quot;language-text&quot;&gt;const&lt;/code&gt;. And never, ever try to alter the contents of a module. Well, unless you’re writing a unit test, and you’re planning to restore them.&lt;/p&gt;
&lt;h2&gt;Path resolution &amp;#x26; index files&lt;/h2&gt;
&lt;p&gt;So far, we are &lt;em&gt;not&lt;/em&gt; using the &lt;code class=&quot;language-text&quot;&gt;.ts&lt;/code&gt; extension to refer to our files. That’s because our compiler will automatically resolve &lt;code class=&quot;language-text&quot;&gt;js&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;ts&lt;/code&gt; files with the given name. If it happens to have both the files, TypeScript files will have a priority. Make sure you’re always checking the compiler configuration because sometimes you may not get the results you’re expecting.&lt;/p&gt;
&lt;p&gt;A similar naming convention is the one used with the index files. It’s like a special name a file could have.&lt;/p&gt;
&lt;p&gt;Consider the following folder structure:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;.
├── utils
├───── index.ts
├── index.ts&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The code in the outer &lt;code class=&quot;language-text&quot;&gt;index.ts&lt;/code&gt; file will be the following:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; utils &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./utils&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We basically &lt;em&gt;omit&lt;/em&gt; the index file when importing them. Instead, we just use their parent folder.&lt;/p&gt;
&lt;p&gt;This becomes extremely useful when we want to convert a single module to multiple modules. We just convert the file to a folder and we introduce an &lt;code class=&quot;language-text&quot;&gt;index.ts&lt;/code&gt; into it.&lt;/p&gt;
&lt;p&gt;A common pattern when we want to structure our code is to use index files to accumulate the different parts we want to export.&lt;/p&gt;
&lt;p&gt;For example, let’s say that we have multiple utils:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;.
├── utils
├───── deepMerge.ts
├───── index.ts
├───── map.ts
├── index.ts&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To import them we will have to specify their full path, assuming they are using default exports:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; deepMerge &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./utils/deepMerge&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; map &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./utils/map&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I assume you have seen the following pattern instead:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; deepMerge&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; map &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./utils&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That becomes possible, if we introduce an index file that exports the utils. Inside &lt;code class=&quot;language-text&quot;&gt;utils/index.ts&lt;/code&gt; we could have the following:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; deepMerge &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;deepMerge&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; map &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;map&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The whole process became much easier. We only have to remember to update the index file every time we add/remove a module.&lt;/p&gt;
&lt;p&gt;Just make sure you’re exporting your utils as defaults:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// map.ts&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This pattern is used a lot in TypeScript applications.&lt;/p&gt;
&lt;h2&gt;Appendix: CommonJS&lt;/h2&gt;
&lt;p&gt;Before Modules became a standard in JavaScript, there were a lot of different patterns for implementing modules. The most popular one was the one that Node.js was based on. It’s called CommonJS, and it’s still widely used.&lt;/p&gt;
&lt;p&gt;In this section, we will try to learn the basics of CommonJS. Besides the syntax, there are fundamental differences on how the two approaches resolve the modules. CommonJS imports are dynamically resolved at runtime, whereas in ES Modules imports are static.&lt;/p&gt;
&lt;h3&gt;Export&lt;/h3&gt;
&lt;p&gt;CommonJS:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;myUtil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

module&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;exports &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; myUtil&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;ES6:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;myUtil&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Importing&lt;/h3&gt;
&lt;p&gt;The legacy CommonJS way:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;myUtil&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Compared to ES6:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;myUtil&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You will notice that TypeScript by default outputs your modules using CommonJS:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token string-property property&quot;&gt;&quot;compilerOptions&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;token string-property property&quot;&gt;&quot;module&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;commonjs&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Which may give you an idea on how popular they are in the Node.js ecosystem. That’s only to maximize compatibility, though. No developers are using CommonJS these days to write code. ES Modules will slowly become the one and only standard for JavaScript applications.&lt;/p&gt;
&lt;h2&gt;Let’s practice what we’ve learned! 🧑🏿‍💻👩‍💻&lt;/h2&gt;
&lt;p&gt;I have prepared for you a small lab in which you will create a small TypeScript project from scratch and you will experiment with modules in TypeScript. It doesn’t require you to know any framework, since we’re not really going to implement something complex. However, you will have a solid understanding on how modules work after going through it.&lt;/p&gt;
&lt;p&gt;To access the lab, &lt;a href=&quot;https://tltr-typescript.com&quot;&gt;enroll to my TLTR; TypeScript course for free&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;That’s all folks! I’ll be happy to hear your feedback about the article, as well as the course itself.&lt;/p&gt;
&lt;p&gt;Many thanks to &lt;a href=&quot;https://unsplash.com/photos/mjl0yIdSi18&quot;&gt;Shubham Dhage&lt;/a&gt; for his amazing cover art.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[2022 was a lot. Here's the proof.]]></title><description><![CDATA[In this Types Special, we will discover how JavaScript deals with types and how you can make your life simpler with TypeScript.]]></description><link>http://www.nicotsou.com/retrospective-2022/</link><guid isPermaLink="false">http://www.nicotsou.com/retrospective-2022/</guid><pubDate>Fri, 27 Jan 2023 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;This post is dedicated to 2022. I will try to summarize all the important events, changes, content I consumed, and things I learned. Happy new year everyone!&lt;/p&gt;
&lt;h2&gt;The year of the Ukraine invasion 🇺🇦☮️&lt;/h2&gt;
&lt;p&gt;I normally don’t like political discussion on my blog, but it’s hard to keep yourself detached from reality shocks like this. Especially, when you’re on a call with your colleagues and you hear the sirens.&lt;/p&gt;
&lt;p&gt;I had my convictions to believe that here in Europe we overcame the idea of starting wars. That we learned through the process of making wars that the consequences are far more severe than potential gains.&lt;/p&gt;
&lt;p&gt;I was wrong.&lt;/p&gt;
&lt;p&gt;And I’m wondering what gains exactly could justify life loss?&lt;/p&gt;
&lt;p&gt;These conflicts between nations never helped. It’s training new generations of hate. It destroys the harmony in the area. It makes people’s lives miserable. It damages the environment. It brings political instability. It just pushes us backwards.&lt;/p&gt;
&lt;p&gt;I’m standing with the people who lost their homes. Sending them my best thoughts, love and prayers. I wish for this heartbreaking tragedy to end soon.&lt;/p&gt;
&lt;h2&gt;The year AI became a thing 🤖&lt;/h2&gt;
&lt;p&gt;Remember NFTs and cryptocurrencies? Yeah, nobody talks about those anymore.&lt;/p&gt;
&lt;p&gt;2022 was all about AI. Whether we like it or not.&lt;/p&gt;
&lt;p&gt;For many years we were hearing about &lt;a href=&quot;https://amzn.to/409Op4U&quot;&gt;AI’s superpowers&lt;/a&gt;, but only recently we had the chance to see it in action. Suddenly, a lot of people have started paying more attention to machine learning.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://www.google.com/url?sa=t&amp;#x26;rct=j&amp;#x26;q=&amp;#x26;esrc=s&amp;#x26;source=web&amp;#x26;cd=&amp;#x26;cad=rja&amp;#x26;uact=8&amp;#x26;ved=2ahUKEwjkhd2ps-L8AhUfgv0HHUL3BXgQFnoECBMQAQ&amp;#x26;url=https%3A%2F%2Fopenai.com%2Fblog%2Fchatgpt%2F&amp;#x26;usg=AOvVaw07ciM_LkSe6efpFVXLmYhu&quot;&gt;ChatGPT&lt;/a&gt; managed to impress the world with its capabilities of producing close to perfection end results. ChatGPT can be your friend, your coworker, your tutor, your financial instructor, your personal chef, your lawyer, your co-writer, your co-scientist, your co-developer, and everything in between. It’s the Siri we never had.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://openai.com/dall-e-2/&quot;&gt;DALL·E 2&lt;/a&gt; on the other side can be your personal Leonardo da Vinci. The day we could watch and share our dreams with our virtual friends on Metaverse is not far away.&lt;/p&gt;
&lt;p&gt;AI can eventually be creative. And creative workers have started wondering whether AI will replace their jobs.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;And what kind of world would that be if everything was made by AI?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Imagine movies produced by AI, released on streaming services built by AI, rated by AI, and eventually sponsored by AI. I mean, who else will have the money to pay them. We will all be without jobs, remember?&lt;/p&gt;
&lt;p&gt;In other news, the situation with the chip shortage for consumer devices became way better. People can finally buy a Playstation now. But now they don’t want to. You see, people are not willing to play video games anymore. They can now go outside and enjoy their lives! How cool is that?&lt;/p&gt;
&lt;p&gt;At least until another pandemic knocks on our doors. Or a missile. Or an alien spaceship. You can’t be sure what is fiction and what is reality anymore. 🤔&lt;/p&gt;
&lt;p&gt;We also had a drama with Elon Musk buying X and firing thousands of employees. Which kinda opened Pandora’s box. Big tech companies are laying off hundreds of employees, they are killing departments, and discontinuing products. And yet their profits are &lt;a href=&quot;https://www.theverge.com/2023/1/26/23571659/tech-layoffs-facebook-google-amazon&quot;&gt;growing&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Ah, and how could I forget?&lt;/p&gt;
&lt;p&gt;Avatars in Metaverse &lt;a href=&quot;https://mashable.com/article/metaverse-legs-twitter-reactions&quot;&gt;have legs&lt;/a&gt; now! You should definitely read an essay about this.&lt;/p&gt;
&lt;p&gt;What did you say? There are not any essays written? No worries, we can ask ChatGPT to write one for us.&lt;/p&gt;
&lt;h2&gt;Another one with great technologies &lt;strong&gt;🔭&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Full-stack technologies are gaining followers. Frameworks like &lt;a href=&quot;https://nextjs.org&quot;&gt;Next.js&lt;/a&gt; and &lt;a href=&quot;https://remix.run&quot;&gt;Remix&lt;/a&gt; gained a lot of popularity last year and they are continuously growing. &lt;a href=&quot;https://reactjs.org/&quot;&gt;React&lt;/a&gt; is still the king, followed by &lt;a href=&quot;https://vuejs.org&quot;&gt;Vue.js&lt;/a&gt; and &lt;a href=&quot;https://angularjs.org&quot;&gt;AngularJS&lt;/a&gt;. A lot of people are switching to &lt;a href=&quot;https://svelte.dev&quot;&gt;Svelte&lt;/a&gt; though. Some of the most promising frameworks are &lt;a href=&quot;https://astro.build&quot;&gt;Astro&lt;/a&gt; and &lt;a href=&quot;https://qwik.builder.io&quot;&gt;Qwik&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;A lot of cool new technologies were being discussed among the JavaScript devs. &lt;a href=&quot;https://vitejs.dev&quot;&gt;Vite&lt;/a&gt; stole our hearts. Tools like &lt;a href=&quot;https://esbuild.github.io&quot;&gt;esbuild&lt;/a&gt;, &lt;a href=&quot;https://bun.sh&quot;&gt;Bun&lt;/a&gt;, &lt;a href=&quot;https://deno.land&quot;&gt;Deno&lt;/a&gt; and &lt;a href=&quot;https://fresh.deno.dev&quot;&gt;Fresh&lt;/a&gt; were also highly appreciated. And of course, &lt;a href=&quot;https://www.rust-lang.org&quot;&gt;Rust&lt;/a&gt; was one of &lt;a href=&quot;https://www.infoworld.com/article/3675391/7-reasons-to-love-the-rust-language-and-7-reasons-not-to.html&quot;&gt;the clear winners&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;AI tools definitely increased their popularity in 2022. &lt;a href=&quot;http://chat.openai.com&quot;&gt;ChatGPT&lt;/a&gt; was definitely the biggest hit, With &lt;a href=&quot;https://github.com/features/copilot&quot;&gt;GitHub Copilot&lt;/a&gt; causing frustration about its high price and the quality of results. &lt;a href=&quot;https://snyk.io/product/snyk-code/?utm_medium=Paid-Search&amp;#x26;utm_source=google&amp;#x26;utm_campaign=GS_SN:_Brand&amp;#x26;utm_content=BR_Snyk_Code&amp;#x26;utm_term=snyk%20code&amp;#x26;gclid=Cj0KCQiAw8OeBhCeARIsAGxWtUx5x_ZPAmZ1cBY1RHgV2U-VRWAhWqU-pmIBNoJyqoz6uHx-7q7mZesaAqjlEALw_wcB&quot;&gt;Synk Code&lt;/a&gt; is damn good too.&lt;/p&gt;
&lt;p&gt;In regards to IDEs, &lt;a href=&quot;https://code.visualstudio.com&quot;&gt;Visual Studio Code&lt;/a&gt; is still on top. We had the debut of &lt;a href=&quot;https://www.jetbrains.com/fleet/&quot;&gt;JetBrains Fleet&lt;/a&gt; which brings some cool new features. Most importantly, it integrates with &lt;a href=&quot;https://www.jetbrains.com/space/&quot;&gt;Space&lt;/a&gt;, an all-new development platform that provides everything you need to build apps. For another year, everybody’s talking about &lt;a href=&quot;https://vercel.com&quot;&gt;Vercel&lt;/a&gt;, with &lt;a href=&quot;https://www.netlify.com&quot;&gt;Netlify&lt;/a&gt; and &lt;a href=&quot;https://www.gatsbyjs.com/products/cloud/&quot;&gt;Gatsby Cloud&lt;/a&gt; being great alternatives.&lt;/p&gt;
&lt;h2&gt;The year I started my TypeScript course 🖥️&lt;/h2&gt;
&lt;p&gt;Last year I announced my &lt;a href=&quot;https://www.youtube.com/c/nicotsou&quot;&gt;YouTube channel&lt;/a&gt;. Since then I’ve been working on a TypeScript series. Each video covers a separate feature of the language.&lt;/p&gt;
&lt;p&gt;I decided to take it a step further. To create my own &lt;a href=&quot;http://tltr-typescript.com/&quot;&gt;TypeScript course&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The course is currently in progress. Making such a course takes much more time than I initially planned, but it gives me so much joy. I’m so excited about it.&lt;/p&gt;
&lt;p&gt;You can &lt;a href=&quot;http://tltr-typescript.com&quot;&gt;enroll for free&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I’m about to announce more exciting new features soon. Feel free to suggest ideas and I will take them into consideration.&lt;/p&gt;
&lt;h2&gt;The year I gave my first conference talk 📢&lt;/h2&gt;
&lt;p&gt;This is something I have wanted to try for many years and I finally had the chance. My talk at Front Conference Zurich was titled “&lt;a href=&quot;https://frontconference.com/speakers/nick-tsourektsidis&quot;&gt;You Don’t Know TS&lt;/a&gt;”, and it was a brief introduction to TypeScript.&lt;/p&gt;
&lt;p&gt;I mean, what else? 😊&lt;/p&gt;
&lt;p&gt;I want to thank the awesome team of volunteers who made this journey great for all the participants. I also thank my awesome colleagues at EPAM for their support.&lt;/p&gt;
&lt;h2&gt;Another one, with good reads 📚&lt;/h2&gt;
&lt;p&gt;Here, I want to list some of the books I’ve read in 2022.&lt;/p&gt;
&lt;p&gt;Starting with one of the year’s best sellers, the book “&lt;a href=&quot;https://amzn.to/3XDRh8h&quot;&gt;Building A Second Brain&lt;/a&gt;” by Tiago Forte. It gives you practical ways to improve the art of taking notes, and getting the most out of them. People consider this the next big thing after the popular “&lt;a href=&quot;https://amzn.to/3WO3Ooq&quot;&gt;Getting Things Done&lt;/a&gt;” by David Allen. I had the chance to be in one of the workshops Tiago gave when he was promoting his book. I have a lot to talk about it and I will save it for another article.&lt;/p&gt;
&lt;p&gt;Tiago’s approach has influenced me to start a research on note taking. I found the Zettelkasten technique extremely helpful. I recommend reading the short read “&lt;a href=&quot;https://amzn.to/3XYYVd0&quot;&gt;Digital Zettelkasten&lt;/a&gt;” by David Kadavy. It’s a more pragmatic approach on the concepts described in the book “&lt;a href=&quot;https://amzn.to/3Haqnhv&quot;&gt;How to Take Smart Notes&lt;/a&gt;” by Sönke Ahrens. It explains the methodology and it gives you practical ideas on how to use &lt;a href=&quot;https://obsidian.md&quot;&gt;Obsidian&lt;/a&gt; to organize your notes.&lt;/p&gt;
&lt;p&gt;On productivity, I would also recommend the book “&lt;a href=&quot;https://amzn.to/3JaNFGB&quot;&gt;Atomic Habits&lt;/a&gt;” by James Clear. It explains the importance of habit building and how they can help you accomplish your goals. This book will teach you how to train yourself to embrace positive habits over bad ones. How to make them attractive and how to encourage yourself to maintain them by making your progress visible.&lt;/p&gt;
&lt;p&gt;One of my most anticipated book launches of the year was the ”&lt;a href=&quot;https://amzn.to/3XYOJ4C&quot;&gt;Engineering Management For The Rest Of Us&lt;/a&gt;” by Sarah Drasner. Although I haven’t finished the book yet, since I only got a copy at the end of the year, one thing I can tell you for sure. If you’re aiming to become a Tech Lead, or you’re already one and you want to improve on that, this book is a great resource. It may change your perception of certain things you’re considering as standards. It’s written for technical people, and that’s what makes it an easy read.&lt;/p&gt;
&lt;p&gt;This year I also discovered the book “&lt;a href=&quot;https://amzn.to/3Jb0VLi&quot;&gt;Dare To Lead&lt;/a&gt;” by Brené Brown, who has dedicated her life to studying leadership. I can’t address how influential and inspirational this book is. It helped me realize the most important skill for a leader; vulnerability. It will open your eyes and lead you in the right direction.&lt;/p&gt;
&lt;p&gt;The book “&lt;a href=&quot;https://amzn.to/3XAkdhn&quot;&gt;Thinking, Fast and Slow&lt;/a&gt;” by Daniel Kahneman was one of the most interesting books I have ever read. It helped me understand why our brains often misjudge things. He explains the two different systems of cognition that we have, and how we are biased about how we understand the world around us. We are prone to ignoring information that doesn’t coincide with our worldview. That’s why algorithms can make better predictions than experts. If you want to understand cognitive biases better, look no further.&lt;/p&gt;
&lt;p&gt;Thanks to my colleague Iurii, I discovered “&lt;a href=&quot;https://amzn.to/3Hxpa5i&quot;&gt;The Back of the Napkin&lt;/a&gt;” by Dan Roam. Dan teaches you how to use diagrams and sketches to tell a story. A must for better presentations and lectures. And the best part of it? You don’t have to be good at sketching.&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;Another one, with great games 🎮&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;The titles I enjoyed in 2022 were the Twelve Minutes, Stray, Deathloop, the remake of Mafia, Prey, Thomas was alone, The Quarry, Children of Morta, Ghost of Tsushima, and Stray.&lt;/p&gt;
&lt;p&gt;But the best game I played was &lt;a href=&quot;https://stanleyparable.com&quot;&gt;The Stanley Parable Ultra Deluxe&lt;/a&gt;. This is by far the most influential indie game I’ve ever played in years. Lots of humor and irony. It’s a game that instead of playing it, it plays with your brain.&lt;/p&gt;
&lt;h2&gt;Another one, with the best music 🎧&lt;/h2&gt;
&lt;p&gt;Check out my playlist &lt;a href=&quot;https://open.spotify.com/playlist/2oCLAEZcSEmD47eqN0tlMp?si=0572ace227964bfe&quot;&gt;Sound of 2022&lt;/a&gt; on Spotify. It’s a mixtape with the most influential music of the year, curated by me.&lt;/p&gt;
&lt;p&gt;Some of the albums I recommend “Into The Blue” by Broken Bells, the surprising return of Roÿksopp with “Stay Awhile”, the new album of Jungle “Talk About It”, and the singles from DRAMA, Roosevelt, Telenova, Bob Moses, Oscar and the Wolf, Winona Oak, BLOW, and Warhaus kept me busy listening for hours.&lt;/p&gt;
&lt;p&gt;Always open for music recommendations. 🤗&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;And here we are. It’s true that 2023 didn’t start with the best intentions. But this won’t stop us from getting the most out of it. Until next year, stay safe, stay curious. 🙂&lt;/p&gt;
&lt;p&gt;Read my last year’s retrospective &lt;a href=&quot;/retrospective-2021&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Many thanks to &lt;a href=&quot;https://unsplash.com/photos/vLF2n8fiFPY&quot;&gt;Vernon Raineil Cenzon&lt;/a&gt; for his amazing cover photo.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The spread operator does more than you think]]></title><description><![CDATA[Nullish Coalescing allows you to “fall back” to a default value when dealing with the nullable values.]]></description><link>http://www.nicotsou.com/tltr-typescript-spread-operator/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-spread-operator/</guid><pubDate>Fri, 16 Dec 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;You probably know the spread operator. It’s those &lt;code class=&quot;language-text&quot;&gt;...&lt;/code&gt; that you see when you open most of the JavaScript files? Yes, those ones.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The spread operator &lt;strong&gt;—&lt;strong&gt;spoiler alert&lt;/strong&gt;—&lt;/strong&gt; can spread the contents of an array or an object. It’s basically the opposite of destructuring.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In this article we will review how it works and I will give you some tips on where you can use it.&lt;/p&gt;
&lt;p&gt;Butterkuchen? 🥮&lt;/p&gt;
&lt;h2&gt;The syntax&lt;/h2&gt;
&lt;p&gt;Let’s have a quick look at how it works.&lt;/p&gt;
&lt;p&gt;Consider the following arrays:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; commercialGenres &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Pop&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;RnB&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; classicalGenres &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Carol&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Opera&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Typically, if you were about to concatenate them, you would use something like this:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; genres &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; commercialGenres&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;classicalGenres&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;genres&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// [&apos;Rock&apos;, &apos;Pop&apos;, &apos;RnB&apos;, &apos;Carol&apos;, &apos;Opera&apos;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You know where this is going…&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 430px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/dc653daad45555fab15f0e77e40f9db6/05ed2/what-if-i-told-you-there-is-a-better-way.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60.45627376425855%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAAsTAAALEwEAmpwYAAAC5ElEQVR42g3Oe08SAQAA8NtcPrLXMmxmPjC0ExDkuDs44UC8Ez2OO+7gOOjARGymK3xWulZp8VBnm0vnq/cCStSoqdVmWWr24L/WJ6pP8PsBDXYUcjWb3MTIcGek198ZZENBvrvDNdLjfTEzdme0y+s2W3AQM6tYGpb8tuAlMuC1cqyBtmsBA23e/bb76+fP/YOD3O/cu2z24eTE3revc7PTT+an32ZSv3O5tcybTx+3o7HxVCq5s7Pz58/fRHSMa28AGknkcfLZq+TLjUwq+z4bS8RDHcLc3Czv81zrC8cfjG6sJV88X1nPpIcHwpFI99Oni1ub2du3egVGB2hb0XqLMuCzDna2llbICs6c0mlrEEhx+nxpSYXM4zAMSSamXe0gVO020GKUN+nP44Yqh10jeTBAZ4ebKHRMxCNBAqyvLKsu49uQuz2cFQMRqPYK3zTSYfHxep6GBFbvoSGaVOPGGiOq8DAIYKNRyA73epvHrzitumqTuoqAFRRWJ5I6d7NWJLQ9IYIWMCcN+ThY5BC3U+8g1Ii+0oQpgGbWqKEQ5lJLYsjHWNVtuKpRLVfVlYvOJr/DINF6jIIgxtAqYKIb8/NGgUUEBqZJFY7JAZAx1rmwWtbYH/F0uc0mWKEHzxnVNYQBdBENLlKlMl2UaSoV6AWOgX0umKMaRRfy/885NAAaoJAApfWSHX3e4TBLtegsKAgpqylc43VipA0SJWcLZVYjSjeL8bSBssOX/S1+wcq5EGBwfHT/697W5tbh4WE69Wp+diqzujqViD6anf7x/WBlZXlpaXE9s/ph+9Py8lwul/v8+Us6/XphYaGvTwTaur03E/eiM4l7ExODQ/2BkDQwNBAOh6SANB6LXu2/zvj4wRuRmZn41d7L8cn7sdjErRuRa9c7nf/lKicqo3RKCjl2ojj/aEFhycm84sIjhYX5R4vyigryTh0vqCgtb7jgk+yiYEUReS14FrcoYUSOE6p/aID48LOu3h4AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;What if I told you there&amp;#39;s a better way - Image&quot;
        title=&quot;&quot;
        src=&quot;/static/dc653daad45555fab15f0e77e40f9db6/05ed2/what-if-i-told-you-there-is-a-better-way.png&quot;
        srcset=&quot;/static/dc653daad45555fab15f0e77e40f9db6/17741/what-if-i-told-you-there-is-a-better-way.png 263w,
/static/dc653daad45555fab15f0e77e40f9db6/05ed2/what-if-i-told-you-there-is-a-better-way.png 430w&quot;
        sizes=&quot;(max-width: 430px) 100vw, 430px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;With the spread operator we could have the same result:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; genres &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;commercialGenres&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;classicalGenres&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
genres &lt;span class=&quot;token comment&quot;&gt;// [&apos;Rock&apos;, &apos;Pop&apos;, &apos;RnB&apos;, &apos;Carol&apos;, &apos;Opera&apos;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the example above, we concatenate the two arrays &lt;code class=&quot;language-text&quot;&gt;commercialGenres&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;classicalGenres&lt;/code&gt;, by creating a new array &lt;code class=&quot;language-text&quot;&gt;genres&lt;/code&gt;. We use the spread operator two times to spread out the values of the two arrays.&lt;/p&gt;
&lt;p&gt;The sequence plays a big role here:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; genres &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;classicalGenres&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;commercialGenres&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;genres&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// [&apos;Carol&apos;, &apos;Opera&apos;, &apos;Rock&apos;, &apos;Pop&apos;, &apos;RnB&apos;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note, that the values of &lt;code class=&quot;language-text&quot;&gt;classicalGenres&lt;/code&gt; are now first in this example.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;We can use the spread operator to remove duplicate values from an array:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; soundsEffects &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;boom&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;tek&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;tek&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;boom&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;tek&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; uniqueSoundsEffects &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;soundEffects&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;uniqueSoundsEffects&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// [&apos;boom&apos;, &apos;tek&apos;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/8f963a45bd01c1d4614acb3e1ec141c9/df51d/claudio-schwarz-Fe4v2DKOKM8-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAIBAwT/xAAWAQEBAQAAAAAAAAAAAAAAAAADAAH/2gAMAwEAAhADEAAAAc7JBsFJt//EABsQAAEFAQEAAAAAAAAAAAAAAAIAAQMREiIj/9oACAEBAAEFAs+0ocRwi4U9m5EtkC//xAAYEQACAwAAAAAAAAAAAAAAAAAAAQISQf/aAAgBAwEBPwF4Vkf/xAAWEQEBAQAAAAAAAAAAAAAAAAAAARH/2gAIAQIBAT8BbH//xAAYEAADAQEAAAAAAAAAAAAAAAAAARESIf/aAAgBAQAGPwLOnCoTZb0jZEz/xAAbEAEAAgMBAQAAAAAAAAAAAAABADFRYXERgf/aAAgBAQABPyFXUXAEiJu5Zi4j9u9gFHRMecn/2gAMAwEAAgADAAAAEO8//8QAGBEBAAMBAAAAAAAAAAAAAAAAAQARITH/2gAIAQMBAT8QCtHkWbHJ/8QAGBEBAAMBAAAAAAAAAAAAAAAAAQARITH/2gAIAQIBAT8QEpyFHJ//xAAbEAACAgMBAAAAAAAAAAAAAAABEQAhMUFhUf/aAAgBAQABPxA4qL1aTUDq15kB24xI26IQJZ3wbimBuEuAHr2E/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Credit: Claudio Schwarz&quot;
        title=&quot;&quot;
        src=&quot;/static/8f963a45bd01c1d4614acb3e1ec141c9/9ecec/claudio-schwarz-Fe4v2DKOKM8-unsplash.jpg&quot;
        srcset=&quot;/static/8f963a45bd01c1d4614acb3e1ec141c9/5d001/claudio-schwarz-Fe4v2DKOKM8-unsplash.jpg 263w,
/static/8f963a45bd01c1d4614acb3e1ec141c9/7349d/claudio-schwarz-Fe4v2DKOKM8-unsplash.jpg 525w,
/static/8f963a45bd01c1d4614acb3e1ec141c9/9ecec/claudio-schwarz-Fe4v2DKOKM8-unsplash.jpg 1050w,
/static/8f963a45bd01c1d4614acb3e1ec141c9/39d31/claudio-schwarz-Fe4v2DKOKM8-unsplash.jpg 1575w,
/static/8f963a45bd01c1d4614acb3e1ec141c9/1f368/claudio-schwarz-Fe4v2DKOKM8-unsplash.jpg 2100w,
/static/8f963a45bd01c1d4614acb3e1ec141c9/df51d/claudio-schwarz-Fe4v2DKOKM8-unsplash.jpg 2400w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/Fe4v2DKOKM8&quot;&gt;Claudio Schwarz&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;Using with objects&lt;/h3&gt;
&lt;p&gt;The spread operator can also be used with JavaScript objects.&lt;/p&gt;
&lt;p&gt;Redux has popularized the idea of storing the complete application state into a single JavaScript object. The spread operator shines when it comes to merging current and new states.&lt;/p&gt;
&lt;p&gt;Consider the following example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;updateSettings&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  currentSettings&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Settings&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  newSettings&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Partial&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Settings&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Add all the properties of the current state of settings&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;currentSettings&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Override them with the ones that have changed&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;newSettings&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This function will basically merge the two objects, similar to how the method &lt;code class=&quot;language-text&quot;&gt;Array.prototype.concat()&lt;/code&gt; works for arrays.&lt;/p&gt;
&lt;p&gt;We can now use the &lt;code class=&quot;language-text&quot;&gt;updateSettings()&lt;/code&gt; function by passing the current state of our settings object, followed by another object with the values we want to override:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; current &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  updates&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    volume&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    brightness&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    darkMode&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;updateSettings&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; updates&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; volume&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.2&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The result will be the following:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; current &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  updates&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    volume&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that the values for &lt;code class=&quot;language-text&quot;&gt;updates&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;volume&lt;/code&gt; have been modified, but the other values have remained the same.&lt;/p&gt;
&lt;p&gt;It’s important to mention here that this performs a shallow merge. That’s why the &lt;code class=&quot;language-text&quot;&gt;system&lt;/code&gt; object has only the volume and all the other properties have been removed. If you want to deeply merge the two objects, you have to repeat the process one more time.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;updateSettings&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  currentSettings&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Settings&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  newSettings&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Partial&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Settings&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;currentSettings&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// 👇 we are adding a property for settings&lt;/span&gt;
    settings&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token comment&quot;&gt;// First, we spread out the current settings&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;currentSettings&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;settings&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token comment&quot;&gt;// And then we override the ones that have changed&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;newSettings&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;settings&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By invoking our function with the same object as before, we will get back the full object for &lt;code class=&quot;language-text&quot;&gt;settings&lt;/code&gt;, with the updated value for the &lt;code class=&quot;language-text&quot;&gt;volume&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; current &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  updates&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    volume&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    brightness&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    darkMode&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Also worth noting that when we have a property with an object as a value, the spread operator copies its values. So, changes in the original object will not impact the newly created one.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/f165a4c66343f3382b97ee62d2d384b0/df51d/karolina-marsalkova-S2INnBMz2Oc-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.15969581749049%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAQFAv/EABYBAQEBAAAAAAAAAAAAAAAAAAEAAv/aAAwDAQACEAMQAAABXxVWiSNmj//EABoQAAIDAQEAAAAAAAAAAAAAAAECAAMRBBL/2gAIAQEAAQUC8KBamWY0NgSN1w3rP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EAB0QAAIBBAMAAAAAAAAAAAAAAAABAhEiMTMyUYH/2gAIAQEABj8CTVy7KRj4YOJrRpif/8QAGxABAAICAwAAAAAAAAAAAAAAAQARIUFRcZH/2gAIAQEAAT8hBWWhwIaZ2w3EGr+TEBZoU7ZZj//aAAwDAQACAAMAAAAQ39//xAAVEQEBAAAAAAAAAAAAAAAAAAAQIf/aAAgBAwEBPxCH/8QAFREBAQAAAAAAAAAAAAAAAAAAACH/2gAIAQIBAT8Qqv/EAB8QAQACAQMFAAAAAAAAAAAAAAEAESExUYFhkcHR4f/aAAgBAQABPxC0mKktdzUeOYvKpeCNfSLE6YatHMQNz1LbHvU8Rza+8//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Credit: Karolína Maršálková&quot;
        title=&quot;&quot;
        src=&quot;/static/f165a4c66343f3382b97ee62d2d384b0/9ecec/karolina-marsalkova-S2INnBMz2Oc-unsplash.jpg&quot;
        srcset=&quot;/static/f165a4c66343f3382b97ee62d2d384b0/5d001/karolina-marsalkova-S2INnBMz2Oc-unsplash.jpg 263w,
/static/f165a4c66343f3382b97ee62d2d384b0/7349d/karolina-marsalkova-S2INnBMz2Oc-unsplash.jpg 525w,
/static/f165a4c66343f3382b97ee62d2d384b0/9ecec/karolina-marsalkova-S2INnBMz2Oc-unsplash.jpg 1050w,
/static/f165a4c66343f3382b97ee62d2d384b0/39d31/karolina-marsalkova-S2INnBMz2Oc-unsplash.jpg 1575w,
/static/f165a4c66343f3382b97ee62d2d384b0/1f368/karolina-marsalkova-S2INnBMz2Oc-unsplash.jpg 2100w,
/static/f165a4c66343f3382b97ee62d2d384b0/df51d/karolina-marsalkova-S2INnBMz2Oc-unsplash.jpg 2400w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/S2INnBMz2Oc&quot;&gt;Karolína Maršálková&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;The rest operator&lt;/h2&gt;
&lt;p&gt;The spread operator has a very friendly sibling; the &lt;strong&gt;rest operator&lt;/strong&gt;. Both of them share the three &lt;code class=&quot;language-text&quot;&gt;...&lt;/code&gt; dots.&lt;/p&gt;
&lt;p&gt;Here’s how we can use it:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; genres &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Pop&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;RnB&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;first&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; second&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;rest&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; genres
&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rest&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// [&apos;RnB&apos;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will basically store all the &lt;em&gt;remaining&lt;/em&gt; items in a variable that we named &lt;code class=&quot;language-text&quot;&gt;rest&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You can freely choose the name that you like. Many developers prefer to use &lt;code class=&quot;language-text&quot;&gt;rest&lt;/code&gt; as a naming convention. According to Clean Code, it’s a good practice to name what this array is about, for example &lt;code class=&quot;language-text&quot;&gt;restDays&lt;/code&gt;, or &lt;code class=&quot;language-text&quot;&gt;restProps&lt;/code&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Where the rest operator shines is with function arguments:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;renderPopularGenres&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  first&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  second&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// Every other argument&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;otherArgs&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;first&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;second&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;, &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;otherArgs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;length&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt; and more...&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;genres&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Pop&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;RnB&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;HipHop&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Metal&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Rock, Pop, and 3 more...&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The function receives an infinite number of strings. It returns the first two values, and displays the count of the remaining ones. Here we use the rest operator to store the remaining arguments in the &lt;code class=&quot;language-text&quot;&gt;otherArgs&lt;/code&gt; variable.&lt;/p&gt;
&lt;p&gt;Cover photo credit: &lt;a href=&quot;https://unsplash.com/photos/5JE9AQqWo_Q&quot;&gt;Alper Güzeler&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Master Destructuring in TypeScript]]></title><description><![CDATA[Destructuring is one of these _aha_ moments you have as a software engineer.]]></description><link>http://www.nicotsou.com/tltr-typescript-destructuring/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-destructuring/</guid><pubDate>Fri, 09 Dec 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;The Destructuring Assignment is one of these &lt;em&gt;aha&lt;/em&gt; moments you have as a software engineer.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;After spending your life trying to read values from arrays or objects, you discover the syntax that changes your life. Forever.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This article will teach you the basics of destructuring arrays and objects. I have collected some useful tips on how to set default values and how to use aliases. We will also learn how to integrate types in TypeScript.&lt;/p&gt;
&lt;p&gt;It wasn’t a straightforward process for me, but I have to say it made sense after all. 🤓&lt;/p&gt;
&lt;p&gt;If you’re new to JavaScript or TypeScript, this article will help you understand many of the things that are happening in the code. It’s a feature that is highly used in every project.&lt;/p&gt;
&lt;p&gt;Apfelkunchen? 🍰&lt;/p&gt;
&lt;h2&gt;Destructuring Objects&lt;/h2&gt;
&lt;p&gt;To explain how destructuring works, consider the following object structure that hosts application settings:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; settings &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  updates&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    volume&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    brightness&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    darkMode&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    notifications&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      email&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      push&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      inApp&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We could write a React component to display the values to the user:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Settings&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;settings&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// Declaring variables for the properties we&apos;re consuming&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; updates &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;updates
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; volume &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;system&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;volume
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; push &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;user&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;notifications&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;push

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;ul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;There are &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;updates&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; updates pending.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;The volume is set to &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;volume&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;You have &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;push &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;not&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; enabled push notifications.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;ul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Not a big fan of React? Well, it’s not important for this example. You can simply imagine that this function will output HTML markup for us.&lt;/p&gt;
&lt;p&gt;Let’s focus on the first 3 lines within our function.&lt;/p&gt;
&lt;p&gt;This initialization could become extremely useful when we are consuming values multiple times within the scope of a function or a method. It’s better if you have to type &lt;code class=&quot;language-text&quot;&gt;push&lt;/code&gt; instead of the complete path &lt;code class=&quot;language-text&quot;&gt;settings.user.notifications.push&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Yes, I know.&lt;/p&gt;
&lt;p&gt;IDEs are our friends. They could make code completion very easy for us. But such verbosity will make your code harder to read.&lt;/p&gt;
&lt;p&gt;So, what are you telling me Nicos? Do I have to instantiate the values for all these properties? In every single function that I’m writing? That’s nonsense!&lt;/p&gt;
&lt;p&gt;Well… Yes!&lt;/p&gt;
&lt;p&gt;You should strive to declare the variables, but not all of them, just the ones you are consuming. And the destructuring operator makes the whole process much easier for you.&lt;/p&gt;
&lt;p&gt;Here’s how we could destructure the &lt;code class=&quot;language-text&quot;&gt;updates&lt;/code&gt; property from the &lt;code class=&quot;language-text&quot;&gt;settings&lt;/code&gt; object:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; updates &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And that was it!&lt;/p&gt;
&lt;p&gt;In this example, we have just created a constant &lt;code class=&quot;language-text&quot;&gt;updates&lt;/code&gt;, which is a &lt;em&gt;copy&lt;/em&gt; of the settings counter. And I’m highlighting the word copy here, because if you remember literal values are being copied, reference values are not. The same doesn’t apply if you try to destructure an object value.&lt;/p&gt;
&lt;p&gt;Now, what if I wanted to access a nested value?&lt;/p&gt;
&lt;p&gt;Here’s what we could try:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  updates&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; volume &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    notifications&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; push &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If this is a new concept for you, it may take you some time to read and understand. Take your time. All we’re doing here is to use the same logic that we use to define an object structure, but instead of creating an object, we are declaring constants that host the values of the &lt;code class=&quot;language-text&quot;&gt;settings&lt;/code&gt; object properties that we select.&lt;/p&gt;
&lt;p&gt;The previous example will produce the following constants: &lt;code class=&quot;language-text&quot;&gt;updates&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;volume&lt;/code&gt;, and &lt;code class=&quot;language-text&quot;&gt;push&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It’s important to note here, that the &lt;code class=&quot;language-text&quot;&gt;system&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;user&lt;/code&gt;, and &lt;code class=&quot;language-text&quot;&gt;notifications&lt;/code&gt; are not going to be declared. The compiler will use them as a map to understand from where it will retrieve the values they encapsulate.&lt;/p&gt;
&lt;p&gt;And that’s it! We can now replace the previous code in our function:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Settings&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;settings&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// Destructuring the variables before using them&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    updates&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; volume &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      notifications&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; push &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;ul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;There are &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;updates&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; updates pending.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;The volume is set to &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;volume&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;You have &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;push &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;not&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; enabled push notifications.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;ul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now someone will argue if this is actually less code than we used to have before. And I would agree with you that this produces more lines of code than we had. But we don’t repeat ourselves. If we want to display, let’s say for example the brightness level, all we have to do is to destructure its property name:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Settings&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;settings&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    updates&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token comment&quot;&gt;// Adding another variable is easier now&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; volume&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; brightness &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;    user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      notifications&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; push &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;ul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;There are &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;updates&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; updates pending.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;The volume is set to &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;volume&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;The brightness is set to &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;brightness&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;You have &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;push &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;not&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; enabled push notifications.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;ul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It’s much easier to work with this component.&lt;/p&gt;
&lt;p&gt;Since these are actual variables, our text editor will let us know if some of the variables are not being used anymore.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/d14fb79dc113d04493edc7fe5fff68a2/d9ed5/variable-declared-but-its-value-is-never-read.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 25.85551330798479%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA/ElEQVR42o2Qy07CYBCFm1Ipbenf0hZayh0RFBZqRIgLozHuhHIRgpqw8v2f4XOEjYkbFl9OMidzMme0hrmmmltS0TOSs4yamdEQ9dxHwtKUlhqTqjvi4i1OYYSdH+KYQ2zzqAdkZpkDfOMBrZ5fUS1sSINvut6Wlr+go1YE/ohytUcUdXCDJkFyjiq3cYIGbtSi+AdHfLMU41k3v4FrYn3OdfjFuLKnpi/oGoJ6pe+/ULefUMYU15gc9D8TvIN/T5h7lkCpnEjdXmnHZbLnwv9goLb0C+/0rI28Qxpoy5NItOx4YZpb0JTlq+hTrtpJyJy6PiMVYu1NmJ3MD+xWiZXhEi+gAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Variable declared but its value is never read&quot;
        title=&quot;&quot;
        src=&quot;/static/d14fb79dc113d04493edc7fe5fff68a2/e996b/variable-declared-but-its-value-is-never-read.png&quot;
        srcset=&quot;/static/d14fb79dc113d04493edc7fe5fff68a2/17741/variable-declared-but-its-value-is-never-read.png 263w,
/static/d14fb79dc113d04493edc7fe5fff68a2/52211/variable-declared-but-its-value-is-never-read.png 525w,
/static/d14fb79dc113d04493edc7fe5fff68a2/e996b/variable-declared-but-its-value-is-never-read.png 1050w,
/static/d14fb79dc113d04493edc7fe5fff68a2/087e3/variable-declared-but-its-value-is-never-read.png 1575w,
/static/d14fb79dc113d04493edc7fe5fff68a2/8079d/variable-declared-but-its-value-is-never-read.png 2100w,
/static/d14fb79dc113d04493edc7fe5fff68a2/d9ed5/variable-declared-but-its-value-is-never-read.png 2880w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;Setting default values&lt;/h3&gt;
&lt;p&gt;The destructuring operator allows us to specify &lt;em&gt;default&lt;/em&gt; values, in case there are no values set.&lt;/p&gt;
&lt;p&gt;For that, we use the &lt;code class=&quot;language-text&quot;&gt;=&lt;/code&gt; operator:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// Setting the default value for the updates to be 0&lt;/span&gt;
  updates &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token comment&quot;&gt;// Setting the default value for the volume to be 0.3&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; volume &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.3&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;  user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Setting the default value for the push notifications to be false&lt;/span&gt;
    notifications&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; push &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we are setting that the &lt;code class=&quot;language-text&quot;&gt;updates&lt;/code&gt; should have a default &lt;code class=&quot;language-text&quot;&gt;value = 0&lt;/code&gt;, the &lt;code class=&quot;language-text&quot;&gt;volume = 0.3&lt;/code&gt;, and the &lt;code class=&quot;language-text&quot;&gt;push&lt;/code&gt; notifications must be &lt;code class=&quot;language-text&quot;&gt;false&lt;/code&gt; by default.&lt;/p&gt;
&lt;h3&gt;Aliases&lt;/h3&gt;
&lt;p&gt;Another cool feature is that we could rename the properties. This is needed in many cases. For example, you may have another variable in that scope which already uses that name &lt;code class=&quot;language-text&quot;&gt;updates&lt;/code&gt;. This conflict will produce compiler errors in TypeScript, or it will silently fail in JavaScript.&lt;/p&gt;
&lt;p&gt;Here’s how we can set a custom name, aka &lt;em&gt;alias&lt;/em&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; updates&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; updatesCounter &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is equivalent to the following:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; updatesCounter &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;updates&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/b68d16e6a6702b1fb740dda34a30c922/1307b/milad-fakurian-DjjaZybYx4I-unsplash.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 50.19011406844106%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAABr0lEQVR42pWSzUoCURTHx2nhwm8nm3R0IeM3fmv4kaigMAtBXIqbtj1ABOJCqFVP0AO48hkqoqcookVtwiCINkXU3NN/BjWTpDrwv4c599wf/3PvcNxXGCB+IfNLNa7RaAjlcnm3WCxe5PP5Z+i02WwGp+d57g+hgyqVigOgfYDukKlQKLBsNqumUikqlUrnRMQv9utLrVbbTKfTw0QiMU4mk+NMJtNutVpStVodAHQ7A8GVqimXy7F4PE6dTudtNBrFNMZwOOTnVtEgh8PhRwAJYIKDF+gpGo2SLMvk9/vVQCDAYrGYvg93DGeo3+8/TCYTUWPAqWE22pq2OJ3OgdfrpVAo9ArIO1xSJBJRXS4Xg8jtdutw1AhTfMAxdbvdg0XGt7tCCD6f7wpA8ng8ar1eZ4qikAaWJImCwaAG0vcgcjgc12azeX2JMQ99dDjsiKJ4AkfHvV7vvt1uE8ZmqJEgCGSz2TSpVquVLBbLzk/uVr4uHmpv6oRBKkSzDODZAsjwK2zWbLfbj3C3BGkghm8GhzcAbv3n/5uDTSbTBl7zEvd6aDQaFUC3AbeucvYJAleQk3GJUvUAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;milad-fakurian-DjjaZybYx4I-unsplash.png&quot;
        title=&quot;&quot;
        src=&quot;/static/b68d16e6a6702b1fb740dda34a30c922/e996b/milad-fakurian-DjjaZybYx4I-unsplash.png&quot;
        srcset=&quot;/static/b68d16e6a6702b1fb740dda34a30c922/17741/milad-fakurian-DjjaZybYx4I-unsplash.png 263w,
/static/b68d16e6a6702b1fb740dda34a30c922/52211/milad-fakurian-DjjaZybYx4I-unsplash.png 525w,
/static/b68d16e6a6702b1fb740dda34a30c922/e996b/milad-fakurian-DjjaZybYx4I-unsplash.png 1050w,
/static/b68d16e6a6702b1fb740dda34a30c922/087e3/milad-fakurian-DjjaZybYx4I-unsplash.png 1575w,
/static/b68d16e6a6702b1fb740dda34a30c922/8079d/milad-fakurian-DjjaZybYx4I-unsplash.png 2100w,
/static/b68d16e6a6702b1fb740dda34a30c922/1307b/milad-fakurian-DjjaZybYx4I-unsplash.png 3000w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;How to add types?&lt;/h3&gt;
&lt;p&gt;Now, the tricky part is that the syntax is pretty much the same as the one TypeScript uses to declare the types.&lt;/p&gt;
&lt;p&gt;Because of that, we need to define types in a separate object structure, following the initial destructuring object:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; updates&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; updatesCounter &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; updates&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is cumbersome on multiple levels:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;To destructure the value &lt;code class=&quot;language-text&quot;&gt;settings.updates&lt;/code&gt; you must get it from the &lt;code class=&quot;language-text&quot;&gt;updatesCounter&lt;/code&gt; constant. It’s like an alias for the &lt;code class=&quot;language-text&quot;&gt;updates&lt;/code&gt; property.&lt;/li&gt;
&lt;li&gt;The constant &lt;code class=&quot;language-text&quot;&gt;updates&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt; going to be declared, which lets you use it in that scope for something else.&lt;/li&gt;
&lt;li&gt;The type declaration is defined in a second object structure, right after the first one. For this object structure we are using the &lt;code class=&quot;language-text&quot;&gt;initial&lt;/code&gt; name of the property &lt;code class=&quot;language-text&quot;&gt;updates&lt;/code&gt;, and not the alias &lt;code class=&quot;language-text&quot;&gt;updatesCounter&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And that’s it! Basically now you are able to use the destructuring operator with any object type, you know how to define types for it, how to set default values, and how to use aliases for its property names.&lt;/p&gt;
&lt;p&gt;Now try it by yourself!&lt;/p&gt;
&lt;p&gt;Can you add types to the following code?&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  updates&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; volume&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; brightness &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;  user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    notifications&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; push &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You will find the solution at the end of the article.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;One last tip: We could move the destructuring operator in the function parameters:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// We pass an object in the parameters&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Settings&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  updates&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; volume&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; brightness &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    notifications&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; push &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;ul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;There are &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;updates&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; updates pending.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;The volume is set to &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;volume&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;You have &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;push &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;not&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; enabled push notifications.&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;ul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is a bit trickier to read, especially if you are new to destructuring objects. But it’s actually simpler if you get used to it. Many developers prefer this way for writing React components.&lt;/p&gt;
&lt;p&gt;As an exercise, you can try to write types for these React component constants. You will find the answer at the end of this post.&lt;/p&gt;
&lt;h2&gt;Destructuring arrays&lt;/h2&gt;
&lt;p&gt;Destructuring works perfectly with arrays.&lt;/p&gt;
&lt;p&gt;Now, arrays don’t exactly have named properties, like objects do:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; genres &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Pop&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;RnB&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can only store their indexed values with a more friendly name:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; first &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; genres&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here’s how you can use destructuring to get the same result:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; genres &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Pop&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;RnB&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;first&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; second&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; genres
first &lt;span class=&quot;token comment&quot;&gt;// &apos;Rock&apos;&lt;/span&gt;
second &lt;span class=&quot;token comment&quot;&gt;// &apos;Pop&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that we use &lt;code class=&quot;language-text&quot;&gt;[]&lt;/code&gt; brackets instead of &lt;code class=&quot;language-text&quot;&gt;{}&lt;/code&gt; object literals.&lt;/p&gt;
&lt;p&gt;Also, we don’t care about how many elements an array has. We are only storing the first two items.&lt;/p&gt;
&lt;p&gt;This code is equivalent to the following:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; first &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; genres&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; second &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; genres&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Comparing these two approaches, we could see that the first one is much simpler to read and more compact.&lt;/p&gt;
&lt;p&gt;Similar to how we declare types for object destructuring, we could specify the types of our variables right after their declaration:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;first&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; second&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; genres&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can also skip items that we don’t want:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;first&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; third&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; genres&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we are getting only the first and the third items.&lt;/p&gt;
&lt;p&gt;We can also skip the first item:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; third&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; genres&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will only give us the third item.&lt;/p&gt;
&lt;h3&gt;Default values&lt;/h3&gt;
&lt;p&gt;Of course, default items are also supported:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;first &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; genres&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The first item will get the item of an empty string, if it’s &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt;. For example, when the array is empty.&lt;/p&gt;
&lt;p&gt;Pretty practical, right? 😎&lt;/p&gt;
&lt;p&gt;Ah, and one more tip.&lt;/p&gt;
&lt;p&gt;This becomes quite useful when you combine it with functions:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getFirst&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;first&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; first
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;The solution&lt;/h2&gt;
&lt;p&gt;Here’s how we could declare types for our destructured constants:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  updates&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; volume&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; brightness &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    notifications&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; push &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  updates&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; volume&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; brightness&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    notifications&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; push&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can increase readability by separating the types to an interface:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SettingsProps&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  updates&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; volume&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; brightness&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    notifications&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; push&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  updates&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  system&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; volume&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; brightness &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    notifications&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; push &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; SettingsProps &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; settings&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And this is the most common way to specify props for our React components in TypeScript projects.&lt;/p&gt;
&lt;p&gt;Cover artwork by &lt;a href=&quot;https://unsplash.com/@pawel_czerwinski&quot;&gt;Pawel Czerwinski&lt;/a&gt;. The artwork within the article is created by &lt;a href=&quot;https://unsplash.com/@fakurian&quot;&gt;Milad Fakurian&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Nullish coalescing: the operator you keep forgetting]]></title><description><![CDATA[Nullish Coalescing allows you to “fall back” to a default value when dealing with the nullable values.]]></description><link>http://www.nicotsou.com/tltr-typescript-nulish-coalescing/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-nulish-coalescing/</guid><pubDate>Fri, 02 Dec 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;strong&gt;Nullish Coalescing&lt;/strong&gt; is another JavaScript feature that TypeScript made popular. It’s the famous &lt;code class=&quot;language-text&quot;&gt;??&lt;/code&gt; operator.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It allows you to set a fallback value if a value is nullable.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The nullable values are &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Here’s how it works:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;testValue &lt;span class=&quot;token operator&quot;&gt;??&lt;/span&gt; fallbackValue&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If the &lt;code class=&quot;language-text&quot;&gt;testValue&lt;/code&gt; is &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;, this expression will result in the &lt;code class=&quot;language-text&quot;&gt;fallbackValue&lt;/code&gt;. In every other case, the &lt;code class=&quot;language-text&quot;&gt;testValue&lt;/code&gt; value will be returned.&lt;/p&gt;
&lt;p&gt;But wait a minute. 🧐&lt;/p&gt;
&lt;p&gt;Since I remember myself, I have been using the logical &lt;code class=&quot;language-text&quot;&gt;OR&lt;/code&gt; operator &lt;code class=&quot;language-text&quot;&gt;||&lt;/code&gt; to implement the same thing.&lt;/p&gt;
&lt;p&gt;Here are some examples:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;track &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
volume &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;
comment &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;No comment.&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I have seen this pattern in many JavaScript applications. And the result is the same, right? In JavaScript, the logical &lt;code class=&quot;language-text&quot;&gt;OR&lt;/code&gt; operator returns the first value if the first condition is truthy or the second value if not.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Why do we need to learn yet another operator?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Well, my friends… There are some caveats when you’re using the logical &lt;code class=&quot;language-text&quot;&gt;||&lt;/code&gt; operator to check for truthy values.&lt;/p&gt;
&lt;p&gt;Let’s run an experiment.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/094ce824a1599ef84868b64dbdd0e366/2562f/marcel-kovacic-rF70cHYTlK0-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 61.596958174904934%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAMABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAMBAgX/xAAWAQEBAQAAAAAAAAAAAAAAAAABAAT/2gAMAwEAAhADEAAAAcq652DxQ3//xAAXEAADAQAAAAAAAAAAAAAAAAAAAREg/9oACAEBAAEFAhMuP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABkQAAMAAwAAAAAAAAAAAAAAAAABEBEhMf/aAAgBAQABPyFGjIoXJ//aAAwDAQACAAMAAAAQhB//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAXEQEBAQEAAAAAAAAAAAAAAAABABEh/9oACAECAQE/EOqWX//EABkQAQEBAAMAAAAAAAAAAAAAAAEAIRFBUf/aAAgBAQABPxBXMPVjdNkBC3//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Photo Credit: Marcel Kovačič&quot;
        title=&quot;&quot;
        src=&quot;/static/094ce824a1599ef84868b64dbdd0e366/9ecec/marcel-kovacic-rF70cHYTlK0-unsplash.jpg&quot;
        srcset=&quot;/static/094ce824a1599ef84868b64dbdd0e366/5d001/marcel-kovacic-rF70cHYTlK0-unsplash.jpg 263w,
/static/094ce824a1599ef84868b64dbdd0e366/7349d/marcel-kovacic-rF70cHYTlK0-unsplash.jpg 525w,
/static/094ce824a1599ef84868b64dbdd0e366/9ecec/marcel-kovacic-rF70cHYTlK0-unsplash.jpg 1050w,
/static/094ce824a1599ef84868b64dbdd0e366/39d31/marcel-kovacic-rF70cHYTlK0-unsplash.jpg 1575w,
/static/094ce824a1599ef84868b64dbdd0e366/1f368/marcel-kovacic-rF70cHYTlK0-unsplash.jpg 2100w,
/static/094ce824a1599ef84868b64dbdd0e366/2562f/marcel-kovacic-rF70cHYTlK0-unsplash.jpg 4148w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/@marcel_kovacic&quot;&gt;Marcel Kovačič&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Try this at home&lt;/h2&gt;
&lt;p&gt;It’s worth trying different values in the terminal to see how these lines of code will respond when the first part of the logical operator is one of the following: &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;NaN&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;0&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;“”&lt;/code&gt;, and &lt;code class=&quot;language-text&quot;&gt;{}&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Consider the following:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;NaN&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; foo&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;bar&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For every one of the lines above, take a moment and write down what values you’re expecting to have as a result.&lt;/p&gt;
&lt;p&gt;Now repeat the same twice, by replacing the second part of the logical operator with a &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt; and with a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In the following section I’m analyzing the results.&lt;/p&gt;
&lt;h2&gt;Running the experiment&lt;/h2&gt;
&lt;p&gt;Let’s start with the first one:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// {}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// {}&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;NaN&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// {}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; foo&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;bar&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// { foo: &apos;bar&apos; }&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// {}&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// {}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// {} we can&apos;t tell but it&apos;s probably the first one 🙂&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Regardless of the value, the result is always the same. But why are we getting an object everywhere?&lt;/p&gt;
&lt;p&gt;There’s a unique behavior in JavaScript. The values &lt;code class=&quot;language-text&quot;&gt;0&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;“”&lt;/code&gt;, and &lt;code class=&quot;language-text&quot;&gt;NaN&lt;/code&gt; are considered &lt;em&gt;falsy&lt;/em&gt;, and therefore the result will be the second part of our comparison.&lt;/p&gt;
&lt;p&gt;It passes my unit tests. Do you also have the same values?&lt;/p&gt;
&lt;p&gt;Now, let’s try with a &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt; value:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// 0.25&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// 0.25&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;NaN&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// 0.25&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// 0.25&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// 0.25&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// {}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Again, the same behavior. If we pass &lt;code class=&quot;language-text&quot;&gt;0&lt;/code&gt; we still get &lt;code class=&quot;language-text&quot;&gt;0.25&lt;/code&gt;. But &lt;code class=&quot;language-text&quot;&gt;0&lt;/code&gt; is not a nullable value. It’s a valid number! This could cause a lot of trouble for our fellow developers.&lt;/p&gt;
&lt;p&gt;Consider the following example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getVolume&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;settings&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; settings&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;volume &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;getVolume&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 0.25&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If the user had muted the sound, we would still have been playing at 25% of the maximum volume.&lt;/p&gt;
&lt;p&gt;Bad UI. 🙈&lt;/p&gt;
&lt;p&gt;OK, now I’m curious how the third one will behave:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;No comment.&quot;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;No comment.&quot;&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;NaN&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;No comment.&quot;&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;No comment.&quot;&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;No comment.&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;No comment indeed. 🤔&lt;/p&gt;
&lt;p&gt;Again, if there was an empty string as a comment, the message “No comment.” would have been displayed.&lt;/p&gt;
&lt;h2&gt;The solution&lt;/h2&gt;
&lt;p&gt;To deal with this weirdness once and for all, we could have to add some extra logic to our functions:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getVolume&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; volume &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; volume &lt;span class=&quot;token operator&quot;&gt;!==&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; volume &lt;span class=&quot;token operator&quot;&gt;!==&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; volume &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;getVolume&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 0&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We finally get the value we were expecting.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Nullish Coalescing&lt;/strong&gt; operator performs that exact check, but the syntax is much simpler:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;volume &lt;span class=&quot;token operator&quot;&gt;??&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It basically says: if you get a valid value, just use it instead of falling back to the falsy one. And it protects us from many issues.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;I hope this article helped you understand how to use the Nullish Coalescing operator, and what problems it solves.&lt;/p&gt;
&lt;p&gt;Ah, and one more thing.&lt;/p&gt;
&lt;p&gt;It’s worth noting that you &lt;em&gt;can’t&lt;/em&gt; chain it with the logical operators:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;volume &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;??&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Returns a SyntaxError&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Unless you explicitly indicate precedence:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;volume &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;??&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.25&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;hr&gt;
&lt;p&gt;The cover artwork is designed by &lt;a href=&quot;https://unsplash.com/photos/C6PD0STeEKM&quot;&gt;Allison Saeng&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The TypeScript operator you're probably misusing]]></title><description><![CDATA[Let's master one of the most controversial features Of TypeScript.]]></description><link>http://www.nicotsou.com/tltr-typescript-non-null-assertion/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-non-null-assertion/</guid><pubDate>Sat, 19 Nov 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;One of the most controversial features of TypeScript is definitely the Non-Null Assertion operator. It allows you to easily check for nullable values, without writing a lot of logic. In this article, we will learn how it works, where to use it, and how it compares to the Optional Chaining operator.&lt;/p&gt;
&lt;p&gt;It’s much easier to use it than to pronounce it, I promise! Probably explaining it will require more time than actually learning it.&lt;/p&gt;
&lt;p&gt;But let me give it a try.&lt;/p&gt;
&lt;p&gt;Cake? 🎂&lt;/p&gt;
&lt;h2&gt;How does it work?&lt;/h2&gt;
&lt;p&gt;In the following &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; object, the &lt;code class=&quot;language-text&quot;&gt;isFavorite&lt;/code&gt; flag is &lt;em&gt;optional&lt;/em&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Practically, this means that we can define a &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; object, without the &lt;code class=&quot;language-text&quot;&gt;isFavorite&lt;/code&gt; flag:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To stress it a bit, let’s try to access these properties and assign them to separate variables:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token comment&quot;&gt;// works fine&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isFavorite &lt;span class=&quot;token comment&quot;&gt;// throws an error, isFavorite can be undefined&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Oops, I can’t get the property &lt;code class=&quot;language-text&quot;&gt;isFavorite&lt;/code&gt; because it was never declared. And therefore, we can’t assign it to a &lt;code class=&quot;language-text&quot;&gt;boolean&lt;/code&gt; variable.&lt;/p&gt;
&lt;p&gt;There are multiple ways to fix that, by adding some additional logic.&lt;/p&gt;
&lt;p&gt;One way is to add a &lt;a href=&quot;https://nicotsou.com/tltr-typescript-narrowing/&quot;&gt;type guard&lt;/a&gt; which with a rollback to &lt;code class=&quot;language-text&quot;&gt;false&lt;/code&gt;, in case the value is &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isFavorite &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Another way is to &lt;em&gt;cast&lt;/em&gt; the value to a &lt;code class=&quot;language-text&quot;&gt;boolean&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isFavorite&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But these are all old fashioned approaches. 😎&lt;/p&gt;
&lt;p&gt;An even better way is to use the &lt;code class=&quot;language-text&quot;&gt;Non-Null Assertion Operator&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isFavorite&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Mind the &lt;code class=&quot;language-text&quot;&gt;!&lt;/code&gt; at the end of this expression.&lt;/p&gt;
&lt;p&gt;If you are something like me, you pretty much want to understand what is actually happening in the background. Here, we are basically instructing TypeScript to fallback to the type that our property &lt;code class=&quot;language-text&quot;&gt;isFavorite&lt;/code&gt; originally had, in case its value will be &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The choice for which approach is better, doesn’t need enough mental processing power.&lt;/p&gt;
&lt;p&gt;In the last example, I didn’t have to specify any value or to add additional logic. It’s more compact compared to the other solutions above.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/c42e39bb2d27349393c6cd0cc9b6ac07/377b2/robot.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.92015209125475%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAEDBP/EABYBAQEBAAAAAAAAAAAAAAAAAAACA//aAAwDAQACEAMQAAAB3uKzuxIP/8QAGhABAAEFAAAAAAAAAAAAAAAAAAECEBESIf/aAAgBAQABBQJm0VJ62f/EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABQQAQAAAAAAAAAAAAAAAAAAACD/2gAIAQEABj8CX//EABoQAQEBAQADAAAAAAAAAAAAAAEAETEhUWH/2gAIAQEAAT8h5Y8feSEiQ0OpnqVt/9oADAMBAAIAAwAAABCr7//EABURAQEAAAAAAAAAAAAAAAAAAAAR/9oACAEDAQE/EEf/xAAWEQEBAQAAAAAAAAAAAAAAAAAAETH/2gAIAQIBAT8QxX//xAAaEAADAQEBAQAAAAAAAAAAAAAAAREhMUFh/9oACAEBAAE/EFjFTRL3PoYfCwiHvEPDNUUiH//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Robot in the sand&quot;
        title=&quot;&quot;
        src=&quot;/static/c42e39bb2d27349393c6cd0cc9b6ac07/9ecec/robot.jpg&quot;
        srcset=&quot;/static/c42e39bb2d27349393c6cd0cc9b6ac07/5d001/robot.jpg 263w,
/static/c42e39bb2d27349393c6cd0cc9b6ac07/7349d/robot.jpg 525w,
/static/c42e39bb2d27349393c6cd0cc9b6ac07/9ecec/robot.jpg 1050w,
/static/c42e39bb2d27349393c6cd0cc9b6ac07/39d31/robot.jpg 1575w,
/static/c42e39bb2d27349393c6cd0cc9b6ac07/377b2/robot.jpg 1980w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/cPF2nlWcMY4&quot;&gt;Daniel K Cheung&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Where should we use it?&lt;/h2&gt;
&lt;p&gt;Great power comes with great responsibility. The Non-Null Assertion operator - seriously, who named that feature like this - can cause you a lot of trouble if you don’t use it carefully. It may result in runtime errors. These kinds of errors are sneaky. They’re invisible while compiling the application.&lt;/p&gt;
&lt;p&gt;Therefore, I strongly recommend you use it with caution. So, don’t start adding exclamation marks at the end of every command.&lt;/p&gt;
&lt;p&gt;When you see this operator in your code reviews, always think if by using it we will have unnecessary errors. Sometimes we developers are kinda lazy and we want to get rid of errors, because we don’t really want to deal with them. That’s not cool though for others on your team. You should have no tolerance when it comes to code quality.&lt;/p&gt;
&lt;p&gt;I don’t want to discourage you from using it though. There are some places it can be very useful, without taking any risks.&lt;/p&gt;
&lt;p&gt;One great example is to use it in React components:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;Favorite&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; track &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isFavorite&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Due to the fact that JSX will &lt;em&gt;always&lt;/em&gt; ignore null-ish values by design, it’s safe to produce a nullable value, without worrying that something wrong will happen at runtime. And it reads much better!&lt;/p&gt;
&lt;h2&gt;How does it compare to Optional Chaining?&lt;/h2&gt;
&lt;p&gt;Now, someone who is a regular visitor to this blog, will start connecting the dots. How does this feature differ from &lt;a href=&quot;https://nicotsou.com/tltr-typescript-optional-chaining/&quot;&gt;Optional Chaining&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;Well, first off, this is a TypeScript-only feature. Its logic is strictly limited in compilation time, in which TypeScript is comparing the compatibility of two types. You’re basically signing the terms and conditions, and TypeScript depends on your gut feeling.&lt;/p&gt;
&lt;p&gt;Optional chaining, on the other side, is a JavaScript feature. And it’s much more of a safer approach.&lt;/p&gt;
&lt;p&gt;Consider the same example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;type Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; string&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;albumTracks&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Map&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;string&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Track&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;2:33&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Love Of My Life&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;3:33&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If we use the Non-null assertion operator, TypeScript will not complain.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; trackLength&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; albumTracks&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Love Of My Life&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;length&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However, as I tried to explain above, the execution of the code will not stop, resulting in a runtime error when we’re running the application.&lt;/p&gt;
&lt;p&gt;Bad robot! 🤖&lt;/p&gt;
&lt;p&gt;Instead, we could use optional chaining to check for the existence of the length property:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; trackLength&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; albumTracks&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Love Of My Life&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;?.&lt;/span&gt;length&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this case, the execution of the code will stop right before we read the &lt;code class=&quot;language-text&quot;&gt;length&lt;/code&gt; property, resulting in an &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt; value. The &lt;code class=&quot;language-text&quot;&gt;trackLength&lt;/code&gt; will get an undefined value, but it will not result in a &lt;code class=&quot;language-text&quot;&gt;runtime&lt;/code&gt; error.&lt;/p&gt;
&lt;p&gt;Good robot! 🤖&lt;/p&gt;
&lt;p&gt;The cover artwork of this post was created by &lt;a href=&quot;https://unsplash.com/photos/oAJnyZkNEs8&quot;&gt;Lea L&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[TLTR; Optional Chaining]]></title><description><![CDATA[Instead of manually checking if a property exists, you can use the same `?` operator that we use when we define optional properties.]]></description><link>http://www.nicotsou.com/tltr-typescript-optional-chaining/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-optional-chaining/</guid><pubDate>Mon, 26 Sep 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;The idea behind optional chaining is very simple.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Instead of manually checking if a property exists, you can use the same &lt;code class=&quot;language-text&quot;&gt;?&lt;/code&gt; operator that we use when we define optional properties.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Let me show you some practical examples to help you understand this feature.&lt;/p&gt;
&lt;p&gt;Pumpkin Spice Latte?🧋&lt;/p&gt;
&lt;h3&gt;Accessing optional properties&lt;/h3&gt;
&lt;p&gt;Consider the following version of a music &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; object structure:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  metadata&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    timesPlayed&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  genres&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;timesPlayed&lt;/code&gt; counter is within a &lt;code class=&quot;language-text&quot;&gt;metadata&lt;/code&gt; object. But both &lt;code class=&quot;language-text&quot;&gt;genres&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;metadata&lt;/code&gt; are optional properties. They may have values, they may not:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; aTrackWithoutAnything&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; aTrackWithMetadata&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; metadata&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; timesPlayed&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;22&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; aTrackWithGenres&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; genres&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;metal&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;disco&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I’m not sure if there was ever a metal-disco combination, but it would have been awesome!&lt;/p&gt;
&lt;p&gt;Now, having options is &lt;em&gt;not&lt;/em&gt; always a good idea. Can you guess what will go wrong in the following example?&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getTimesPlayed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;metadata&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;timesPlayed &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Our &lt;code class=&quot;language-text&quot;&gt;getTimesPlayed()&lt;/code&gt; function is a very simple one. It accepts a &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt;, and it returns the &lt;code class=&quot;language-text&quot;&gt;timesPlayed&lt;/code&gt; counter. But the &lt;code class=&quot;language-text&quot;&gt;metadata&lt;/code&gt; object could be possibly &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;. And that will cause a &lt;strong&gt;runtime error&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Of course, TypeScript is complaining here. And it’s a good thing that it does, because this can cause our application to crash. You see, it’s ok if you try to access a property at the &lt;em&gt;root&lt;/em&gt; level, it will simply return &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;. But nothing great ever happened when accessing a property of &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt; itself.&lt;/p&gt;
&lt;p&gt;Additional code is needed to check we’ll never fall into that case. We can add a &lt;strong&gt;type guard&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getTimesPlayed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;metadata&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;metadata&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;timesPlayed
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can imagine if we have a deeply nested property, the amount of code will be unbearable.&lt;/p&gt;
&lt;p&gt;JavaScript developers for many years were depending on third-party libraries and frameworks that simplified these checks. The &lt;code class=&quot;language-text&quot;&gt;get()&lt;/code&gt; method from &lt;a href=&quot;https://lodash.com/docs/4.17.15#get&quot;&gt;Lodash&lt;/a&gt; is a great example. But utilities like these can add extra code in our bundles and it could also cause confusion.&lt;/p&gt;
&lt;p&gt;Thankfully, the newer versions of JavaScript have a solution.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Here’s how &lt;strong&gt;optional chaining&lt;/strong&gt; reduces the clutter:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getTimesPlayed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;metadata&lt;span class=&quot;token operator&quot;&gt;?.&lt;/span&gt;timesPlayed &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we are basically using the same &lt;code class=&quot;language-text&quot;&gt;?&lt;/code&gt; question-mark operator we used to define the optional property, but this time we are checking if it exists in the given object.&lt;/p&gt;
&lt;h3&gt;Accessing optional elements&lt;/h3&gt;
&lt;p&gt;Did you know that in JavaScript there are two ways to access an object member? You can use the dot notation, but you could also use the bracket notation:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id
track&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;id&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The above lines will have the exact same behavior; they will return the &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; of the &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The second option becomes quite useful in situations when you want to access property names programmatically:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getProperty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;track&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; propertyName&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; track&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;propertyName&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can use any expression inside the brackets, which gives you a lot of flexibility on accessing properties.&lt;/p&gt;
&lt;p&gt;Thankfully, the same functionality is supported with the optional chaining operator:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;track&lt;span class=&quot;token operator&quot;&gt;?.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;id&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that here we are using both the &lt;strong&gt;dot&lt;/strong&gt; and the &lt;strong&gt;brackets&lt;/strong&gt; (?.).&lt;/p&gt;
&lt;p&gt;We could use it to bulletproof the previous function, so that it won’t cause an error in case we call it with a property that doesn’t exist, or in case the value is undefined:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getProperty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token literal-property property&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;propertyName&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; keyof Track&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; track&lt;span class=&quot;token operator&quot;&gt;?.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;propertyName&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let me break down what is happening in the example above:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Only &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; types can be passed to the &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; parameter.&lt;/li&gt;
&lt;li&gt;Only keys of the &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; type can be passed in the &lt;code class=&quot;language-text&quot;&gt;propertyName&lt;/code&gt; parameter. For example, the &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We use optional chaining to ensure that we won’t have any error if we try to access a property that doesn’t exist.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In fact, the last one isn’t really needed, since we are trying to access a root-level property. It would have been a problem if we were trying to access a nested property.&lt;/p&gt;
&lt;h3&gt;Accessing array values by index&lt;/h3&gt;
&lt;p&gt;We can use the syntax we just learned to access array index values:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;genres&lt;span class=&quot;token operator&quot;&gt;?.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will protect you from out of range indexes. Just like that!&lt;/p&gt;
&lt;h3&gt;Optional parameters&lt;/h3&gt;
&lt;p&gt;Another way of using the question-mark operator in TypeScript is to declare optional parameters for our functions or methods.&lt;/p&gt;
&lt;p&gt;Consider the following example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;createTrack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token literal-property property&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; string&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;artist&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; string&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; boolean&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    artist&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    isFavorite&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we let TypeScript know that the &lt;code class=&quot;language-text&quot;&gt;isFavorite&lt;/code&gt; argument is optional. Every time we are calling this function we have the option to skip it.&lt;/p&gt;
&lt;p&gt;Optional parameters must be &lt;strong&gt;last in the list&lt;/strong&gt; and it’s not possible to receive default values.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/75d222c9d389a7ce7987e0dd729eb35a/8c48a/typescript-optional-parameters.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 46.38783269961977%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABb0lEQVR42qWQXW/SYBiG21JgdGO0fQv0g76FtptQ6pwLEh3OxJjY8bHFoB65GA/8/3/hsi0mmuxEs4MrV577yXMfPIrQ1gzFkji6ZiJXCPMCOXqDDF4TeEu84YJk/BZhXeAPctLoipH7nGYjpqnHtJoJrdLt0rb+HsVp7Rj6V0TJC9wwRwzP8cI5ffdZjevPyv2MQOa4waxk+tsZA2+K0Q2xnJSeiDH1GxRbK+ifZgRWirTOEPYZSfqKSbIgThdk+YowuqTdCTkyJG2j9LHEOInolLQ6ozqrsPR3KAN9y8vxntX0K5fJZ8JeQSY2TO0NY1Ewlxu8kw/0tJv6wKxo/KHKrMYhdxoFint0x7z/wNL/gezucZQtffWAqKxtcdSKXU3/Lx7N2gZFqLcEp1+Yhd/xj+8xlY/lYo3Qbg9W17X/FaU6mJh7rrOf5KNvBMb9fxU8Kqx+OO7tiUoqy+6npxX6nTu89g5bLWqqFzyl8Bdl1/ePegnwwwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Optional Parameters in TypeScript&quot;
        title=&quot;&quot;
        src=&quot;/static/75d222c9d389a7ce7987e0dd729eb35a/e996b/typescript-optional-parameters.png&quot;
        srcset=&quot;/static/75d222c9d389a7ce7987e0dd729eb35a/17741/typescript-optional-parameters.png 263w,
/static/75d222c9d389a7ce7987e0dd729eb35a/52211/typescript-optional-parameters.png 525w,
/static/75d222c9d389a7ce7987e0dd729eb35a/e996b/typescript-optional-parameters.png 1050w,
/static/75d222c9d389a7ce7987e0dd729eb35a/087e3/typescript-optional-parameters.png 1575w,
/static/75d222c9d389a7ce7987e0dd729eb35a/8c48a/typescript-optional-parameters.png 2096w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;Calling functions/methods&lt;/h3&gt;
&lt;p&gt;The optional chaining operator could also check if a property is a callable function, before calling it.&lt;/p&gt;
&lt;p&gt;Let’s modify our &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; type to add an optional &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; method:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  metadata&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    timesPlayed&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  genres&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  play&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Every time we call this function, we need to check if it exists, otherwise it will cause a runtime error. Here’s how we could use optional chaining, to reduce code:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;play&lt;span class=&quot;token operator&quot;&gt;?.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;hr&gt;
&lt;p&gt;Cover photo credit: &lt;a href=&quot;https://unsplash.com/photos/kKYDdRoTydk&quot;&gt;DeepMind&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[TypeScript intersections explained in 5 minutes]]></title><description><![CDATA[A deep dive into TypeScript’s intersections. We will learn the syntax, and explain everything you need to know about them.]]></description><link>http://www.nicotsou.com/tltr-typescript-intersections/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-intersections/</guid><pubDate>Sat, 03 Sep 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;Intersections in TypeScript are really cool. They allow you to mix and match different object structures, to create new ones. Their flexibility comes with surprises. There is a certain misbehavior you should be familiar with. That will help you understand how they are meant to be used and discover what’s happening in your code.&lt;/p&gt;
&lt;p&gt;Intersections somehow behave like unions. But how do they compare to unions? What’s their difference? How are they meant to be used?&lt;/p&gt;
&lt;p&gt;This article is a deep dive into TypeScript’s intersections. We will learn the syntax, and explain everything you need to know about them.&lt;/p&gt;
&lt;p&gt;Fortune cookie? 🥠&lt;/p&gt;
&lt;h2&gt;What’s an intersection?&lt;/h2&gt;
&lt;p&gt;In TypeScript, you can use the intersection &lt;code class=&quot;language-text&quot;&gt;&amp;amp;&lt;/code&gt; operator, to merge two object types into one. The result will be a &lt;em&gt;new&lt;/em&gt; type which will include all unique members, plus the members that they have in common, as long as they have the same type.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;An intersection practically merges the members of two object structures into a new type.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Here’s an example that illustrates its usage:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FileBase&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  filename&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token function-variable function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Song&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FileBase &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  extension&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;VideoClip&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FileBase &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  extension&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we have a type alias called &lt;code class=&quot;language-text&quot;&gt;FileBase&lt;/code&gt;, which represents a downloadable file. All the other types extend this type alias, by adding additional object members.&lt;/p&gt;
&lt;p&gt;The equivalent result will be the following:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Song&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// properties from FileBase&lt;/span&gt;
  filename&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token function-variable function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// properties from Song&lt;/span&gt;
  type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;VideoClip&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// properties from FileBase&lt;/span&gt;
  filename&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token function-variable function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// properties from VideoClip&lt;/span&gt;
  type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Pretty neat! 🤘&lt;/p&gt;
&lt;h2&gt;When to use an intersection?&lt;/h2&gt;
&lt;p&gt;We’ve seen how intersections could help us mix and match object structures. This becomes helpful when you have a lot of repeatable properties across different types, and you don’t want to deal with classical inheritance and polymorphism patterns.&lt;/p&gt;
&lt;p&gt;Let’s say that all your models have a common property. That could be an &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt;. Now, let’s imagine that your project has a more sophisticated way of dealing with ids. We could create an interface for that:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;U&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;X&apos;&lt;/span&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Our &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; property accepts strings that start with &lt;code class=&quot;language-text&quot;&gt;U-&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;X-&lt;/code&gt;, followed by a number. For example, we could have an id like &lt;code class=&quot;language-text&quot;&gt;U-123&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To create new types that re-use this id logic, we can use an intersection:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Id &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  username&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  email&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Order&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Id &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  total&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;An alternative way would be to create a new type on-the-fly. In the following example we are creating a &lt;code class=&quot;language-text&quot;&gt;User&lt;/code&gt; that is also an &lt;code class=&quot;language-text&quot;&gt;Order&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; userWithOrder&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; User &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; Order &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;U-123&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  total&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;123&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  username&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;nicotsou&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  email&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;no@spam.please&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It’s pretty obvious what the type of &lt;code class=&quot;language-text&quot;&gt;userWithOrder&lt;/code&gt; could be, by reading this example. We didn’t have to come up with a name, which sometimes is really difficult to get right.&lt;/p&gt;
&lt;p&gt;As a recap, intersections allow you to extend existing types according to our needs, like the example above. Now let’s discover some edge cases…&lt;/p&gt;
&lt;h2&gt;Intersections; The weird parts&lt;/h2&gt;
&lt;p&gt;I mentioned in the beginning about the misbehavior of intersections. There are some complicated concepts to understand, which I would like to address in this section.&lt;/p&gt;
&lt;p&gt;So far, we were using object structures. You may be wondering, will intersections work with literal types?&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WhatAmI&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The answer is &lt;em&gt;no&lt;/em&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Intersections &lt;em&gt;only&lt;/em&gt; work with object structures.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If you try to use them with literal types, you will get back &lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt;. In fact, &lt;em&gt;both&lt;/em&gt; types must be object structures:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WhatAmI&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// never&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WhatAboutMe&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// never&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That makes sense; there can’t be any type that is a &lt;code class=&quot;language-text&quot;&gt;boolean&lt;/code&gt; and at the same time a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;. That’s why TypeScript will assign &lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt; to the previous types, preventing us from assigning any values on it. If you remember, the type &lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt; is a wildcard, that behaves exactly like that.&lt;/p&gt;
&lt;p&gt;Another interesting question is, what will happen if two object structures have common members?&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;/span&gt;  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Order&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;/span&gt;  total&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;UserOrder&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; User &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; Order&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the example above, both the &lt;code class=&quot;language-text&quot;&gt;User&lt;/code&gt; and the &lt;code class=&quot;language-text&quot;&gt;Order&lt;/code&gt; types have a common &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; property. As expected, this will be included in the result object structure:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// the equivalent result of User &amp;amp; Order&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;UserOrder&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;/span&gt;  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  total&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Easy! But what if we had properties with the same name, but with different &lt;em&gt;types&lt;/em&gt;?&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  length&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Order&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  total&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  length&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;UserOrder&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; User &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; Order&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The property &lt;code class=&quot;language-text&quot;&gt;length&lt;/code&gt; exists in both types, but it’s a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt; value in &lt;code class=&quot;language-text&quot;&gt;User&lt;/code&gt; and a &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt; value in &lt;code class=&quot;language-text&quot;&gt;Order&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;How TypeScript will behave here? Let’s figure it out:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// The equivalent result of User &amp;amp; Order&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;UserOrder&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  total&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  length&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;never&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see, the &lt;code class=&quot;language-text&quot;&gt;length&lt;/code&gt; property has the type &lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt;. So basically, the intersection of two types will not return common object members that have different types.&lt;/p&gt;
&lt;p&gt;And that’s for a good reason. 😉&lt;/p&gt;
&lt;h2&gt;Understanding intersections&lt;/h2&gt;
&lt;p&gt;Now you may ask… what do we mean by calling this thing an &lt;em&gt;intersection&lt;/em&gt;? 🤔&lt;/p&gt;
&lt;p&gt;This term comes from math, and more specifically the theory of &lt;a href=&quot;https://en.wikipedia.org/wiki/Intersection_(set_theory)&quot;&gt;Set&lt;/a&gt;s. A Set is the mathematical model for a &lt;em&gt;collection&lt;/em&gt; of different &lt;em&gt;things&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;We programmers call them &lt;em&gt;arrays&lt;/em&gt;. 🤓&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/83e3f38f0f8d61d1aa3b06b6556976c6/169e3/intersection-math-diagram.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 35.74144486692015%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABv0lEQVR42l1RTUsCURSdIrOgMsWZrLQ0MolCFy0iCFoVQf2CCCyCEKKCjKjVLFuU5MRAPOfNODqOwSNIWkxfC5ciuIhqEVLLcFWL/sDrvr6oLpwH93LuuYfzOO6r0ul0E8CfVPLhwxQJZDKZAUXRw7kc7s1m1YimaT6McUhVVZ7xEUI20zxzQ9+nKEoH97uKxWIDCPRQytULSdoxtE99dxcbvkLhIFS6WgvSV6fjQ6SCbLA8CMKtiQRxPd7EBPo2zssy8cABx48gu1rAhdYojTZNXl4KW1LVWzuP+unpqP/ETAafrpcdS/fb/bFaXLBWLTscHxTTp+1UnHDT+GRAJPeN+Tzu+hFMGSkvSSSap6ur9vlaTIg/7AXKK9sDdGxm2EpJ3sgBbR8hT58uEXHkMO5VLcJXF3f4l6kFH0T11yHLT8kq/ZXlim2zLHrWS6Lf1LQgOB89M5F7dv/ZPbd76ywS0oKxEWL5EUIaVUJ4BsMw2rj/xUQx2GbhA8EL6NT1o24E2RoIdR7DnOUsSZL9+1Mky7KzPdM0naIo1v/XrGOPLMstuq53/56zzNgiayilHzz4HBe49AB44A8xx4z9Dm9P1oO1ocDgAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;intersection-math-diagram.png&quot;
        title=&quot;&quot;
        src=&quot;/static/83e3f38f0f8d61d1aa3b06b6556976c6/e996b/intersection-math-diagram.png&quot;
        srcset=&quot;/static/83e3f38f0f8d61d1aa3b06b6556976c6/17741/intersection-math-diagram.png 263w,
/static/83e3f38f0f8d61d1aa3b06b6556976c6/52211/intersection-math-diagram.png 525w,
/static/83e3f38f0f8d61d1aa3b06b6556976c6/e996b/intersection-math-diagram.png 1050w,
/static/83e3f38f0f8d61d1aa3b06b6556976c6/087e3/intersection-math-diagram.png 1575w,
/static/83e3f38f0f8d61d1aa3b06b6556976c6/169e3/intersection-math-diagram.png 1682w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;An intersection of two sets &lt;code class=&quot;language-text&quot;&gt;A&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;B&lt;/code&gt; is the set containing all elements of &lt;code class=&quot;language-text&quot;&gt;A&lt;/code&gt; that also belong to &lt;code class=&quot;language-text&quot;&gt;B&lt;/code&gt; or equivalently, all elements of &lt;code class=&quot;language-text&quot;&gt;B&lt;/code&gt; that also belong to &lt;code class=&quot;language-text&quot;&gt;A&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now, going back to our previous example. Let me try to draw what we had:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/b72a54043078e2fc28aa0f16f2956e8c/5b30f/typescript-intersections-object-diagram.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 34.60076045627377%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABrUlEQVR42m2Su0vDUBTGb62IWl9tfdvGvuxQxQcqik5uTm5BUHRREBF0cBBxyKAoDtUWakKSJmmuxMLFYq2CiLOj4qSzf4Pu13MEtQUvHJKc5Pudc74TQv6Oy7KsWohOwzASqqoKmqaF4X4Irv0QAdu2R0zTHFcUpQ+e4xiOqraSf44LBblcroclWV1JLdUDuEXX9ZjMcqOpS6cvSWmMiaIbv2OM1aBI5MydemCCcsN6KmjQTSvAoo7jfFeTJKkqb5pB68pqeZo77ua+xabXpUO/RmmYicwtU7n99sjxcrIOsRx9nN+fSjLdVw4U6D31FE7ULk7mGt8mdkL5MzOYYrbwPrMX5WRlkJO1accxImgLjD6WP1VCnCwAcMP/PLvblj7XAr/AbDbbnT5PNxUOAOjZ7Phs3hou7qYT+jWNlSRV4GQ1/BHYHsApOOcuDNQhXGfMl8lkGsDbYHmH9WB+BEat/i5QLDain5DvwiWd3F30Sy+FdvCuodwqWZa9tmbH0WuEV/hIKfVAMmQpSgg67gUxbjsClacsTZs0FCOB71GMefwDsCvw3SvCsn5AX3CD0s2mon4DAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-intersections-object-diagram.png&quot;
        title=&quot;&quot;
        src=&quot;/static/b72a54043078e2fc28aa0f16f2956e8c/e996b/typescript-intersections-object-diagram.png&quot;
        srcset=&quot;/static/b72a54043078e2fc28aa0f16f2956e8c/17741/typescript-intersections-object-diagram.png 263w,
/static/b72a54043078e2fc28aa0f16f2956e8c/52211/typescript-intersections-object-diagram.png 525w,
/static/b72a54043078e2fc28aa0f16f2956e8c/e996b/typescript-intersections-object-diagram.png 1050w,
/static/b72a54043078e2fc28aa0f16f2956e8c/087e3/typescript-intersections-object-diagram.png 1575w,
/static/b72a54043078e2fc28aa0f16f2956e8c/8079d/typescript-intersections-object-diagram.png 2100w,
/static/b72a54043078e2fc28aa0f16f2956e8c/5b30f/typescript-intersections-object-diagram.png 2372w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Now, the object structure that we get as a result doesn’t make any sense. It’s not aligned with the theory of intersections we’ve seen from math.&lt;/p&gt;
&lt;p&gt;Why is the name of this feature so confusing?&lt;/p&gt;
&lt;p&gt;Well, my friends… After some &lt;a href=&quot;https://stackoverflow.com/questions/38855908/naming-of-typescripts-union-and-intersection-types&quot;&gt;research&lt;/a&gt;, I was able to understand why TypeScript developers named it this way. As Einstein famously said “When we think about TypeScript intersections, instead of thinking about types as sets of object properties, we could instead focus on &lt;em&gt;scalar&lt;/em&gt; variables and their sets of permissible values”.&lt;/p&gt;
&lt;p&gt;Yeah, I know what you’re thinking. TypeScript was not invented back then. But it’s ok. Everybody is creating memes and putting words in Einstein’s mouth. Why can’t I?&lt;/p&gt;
&lt;p&gt;To understand how intersections relate to math, let’s experiment with &lt;em&gt;literal types&lt;/em&gt;. Consider a set of metadata for songs and another one for video clips:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SongMetadata&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;length&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;album&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;title&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;artist&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MovieMetadata&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;length&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;director&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;title&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, there are some &lt;em&gt;common&lt;/em&gt; metadata, like &lt;code class=&quot;language-text&quot;&gt;title&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;length&lt;/code&gt;. We could get those, by using an intersection:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CommonMetadata&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SongMeta &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; MovieMetadata &lt;span class=&quot;token comment&quot;&gt;// &quot;length&quot; | &quot;title&quot;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, if we compare this example to the diagram we saw previously, math makes total sense:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/6e0d99ad128fe31de2259db9b9c60f67/e8950/typescript-intersections-literal-type-diagram.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 49.42965779467681%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB5UlEQVR42o1SS0sbURS+9VHjq9VgnBCr8ZVg1SRaQUSaknXFjTDdd6NY0FIigiIMSEFc2BodZ7xzZ+480gxloAulXbjKH8iyG5du3XTVRVe350gD9RHbA5dzzr3nfPc8PkJuSBAE9aZptvu+LzHG0q7rJkFLlmUNcs5THuez4OcMw8hSSh//SXtA7hIIaISkhG3YE5A0DH4LgMeOPT5PXeu5LMv11VjP81rh7QXG3AISQlz9gNXYth2t3isKqfOPWFpE1qJftz4+/aYcJgV5E//Zq8QEkZt3zcOYpeuZvzGuAdJPPHUxC8HhzR7R9G74fG5n8Mv2QQZAJi5TG9ngvT4tyHLvr871MUFWpVN62uI4znRNQM01J7/LSliEVvsAJHqW17rLK3szgqxM/khsLJieNy7IUo+Q8qAXu/ZPfMnSrdGagDCTcZxZ9d4u26HPH+gzQV5Hym93+qnr9l3FwxIKgRUxuJGFEYVILalUKo0OY1OmasaLhcIjHDh16JBa4i91x3gFi5pizEnDxjNoq6raRv4liqLUAdAIJgE1xrAlrDxgQRjpo2laJ1TVcS9dbrYOgx6Cky6VSgngZRgoMlAsFp8AcBzA+uHkkKvkfwXJjTwD3Yw2VN2AGj/EDpBa4D+8K/k3IFD2cdKPyB8AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-intersections-literal-type-diagram.png&quot;
        title=&quot;&quot;
        src=&quot;/static/6e0d99ad128fe31de2259db9b9c60f67/e996b/typescript-intersections-literal-type-diagram.png&quot;
        srcset=&quot;/static/6e0d99ad128fe31de2259db9b9c60f67/17741/typescript-intersections-literal-type-diagram.png 263w,
/static/6e0d99ad128fe31de2259db9b9c60f67/52211/typescript-intersections-literal-type-diagram.png 525w,
/static/6e0d99ad128fe31de2259db9b9c60f67/e996b/typescript-intersections-literal-type-diagram.png 1050w,
/static/6e0d99ad128fe31de2259db9b9c60f67/087e3/typescript-intersections-literal-type-diagram.png 1575w,
/static/6e0d99ad128fe31de2259db9b9c60f67/e8950/typescript-intersections-literal-type-diagram.png 2000w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;How do intersections compare to unions?&lt;/h2&gt;
&lt;p&gt;A logical question arises here. What is the difference between unions and intersections? Let’s use the previous example, to compare these two features.&lt;/p&gt;
&lt;p&gt;Having a union will result in a larger set, containing all the metadata for both &lt;code class=&quot;language-text&quot;&gt;Movie&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;Song&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/f904baa5e8566874789efb2128799a85/c4cb0/typescript-union-diagram.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 49.42965779467681%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABYlAAAWJQFJUiTwAAAB4UlEQVR42o1SXUsbQRQdP1KNia2KSerHRhNUtH5iaWuwVUQEaV/aQumDD9IWAqkJVv/APvmgIRgl7s7sTHbddUsgIPjYJ/PaH9D/IfgkCr29k9oa0YgDs8Ne7jn33HsuIbccVVUfaJoW5Jw/NQxjStf1LiHECGNsmVP+EeOL+Xx+vFQq1ZM7To38YHIIgZOSSBOaJBncdvIv0odOcPNINKu/in7158FDSkUMyd8Xi8W6qowIbqK2iKlwVRnIhzp4tNwC5HMzkLgHWhMK+L6EAAVoljElC192VfufCADK6nYdI3L8bWsayJvWs/7VIRhYb4fAV/+5knwFXclx6F7zQjARhXDqCZBPgcKeqey77lwlxzVCbttDPzbYIJCljpOBZOT3YqrhtC8VACS8UFYWyrmdCeWsB4thDs62n1I6U1WhNIJaYr5yDEBm66E93gFkzfv3n5SBccY8TIi3PJsN3VBYaYrDnT6u89eC0hjOZ4Lti7md72yWmeYzlmVhdLsTjRszdOMdCohWI7tO6jg+JOvGG0FwG8/yELbXi6vyUpqAdxgN9FRi7iREYNQ0zVHXdXssy3pcKBR68W20bTuMcQWLPM/lcn5y3yN3K51O+/D1/lMs25JxOXy5q5lMxnsb+A8Hk+cnOHgqNgAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-union-diagram.png&quot;
        title=&quot;&quot;
        src=&quot;/static/f904baa5e8566874789efb2128799a85/e996b/typescript-union-diagram.png&quot;
        srcset=&quot;/static/f904baa5e8566874789efb2128799a85/17741/typescript-union-diagram.png 263w,
/static/f904baa5e8566874789efb2128799a85/52211/typescript-union-diagram.png 525w,
/static/f904baa5e8566874789efb2128799a85/e996b/typescript-union-diagram.png 1050w,
/static/f904baa5e8566874789efb2128799a85/087e3/typescript-union-diagram.png 1575w,
/static/f904baa5e8566874789efb2128799a85/8079d/typescript-union-diagram.png 2100w,
/static/f904baa5e8566874789efb2128799a85/c4cb0/typescript-union-diagram.png 2638w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;A union combines &lt;em&gt;all&lt;/em&gt; the values they have in common or not. An intersection, on the other side, combines &lt;em&gt;only&lt;/em&gt; the common values:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AllMetadata&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SongMeta &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; MovieMetadata &lt;span class=&quot;token comment&quot;&gt;// &quot;length&quot; | &quot;album&quot; | &quot;album&quot; | &quot;director&quot; | &quot;artist&quot; | &quot;title&quot;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CommonMetadata&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; SongMeta &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; MovieMetadata &lt;span class=&quot;token comment&quot;&gt;// &quot;length&quot; | &quot;title&quot;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, I have another million dollar question for you…&lt;/p&gt;
&lt;h2&gt;How do intersections compare to the extends keyword?&lt;/h2&gt;
&lt;p&gt;Remember our previous example?&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Id &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  email&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  username&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can get the same result if you use an interface in combination with the &lt;code class=&quot;language-text&quot;&gt;extends&lt;/code&gt; keyword:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  username&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  email&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;extends&lt;/code&gt; keyword is more suitable for implementing OOP structures. &lt;strong&gt;It’s basically a &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/extends&quot;&gt;JavaScript feature&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Although you can use it with interfaces, which is a TypeScript-only feature, you &lt;em&gt;can’t&lt;/em&gt; use it with type aliases.&lt;/p&gt;
&lt;p&gt;It comes with other limitations. For example, it can only be used with types that have statically known members. You can’t use it with &lt;em&gt;dynamically&lt;/em&gt; generated types.&lt;/p&gt;
&lt;p&gt;If you are not working with classes and OOP is not important to you, it’s better to avoid the &lt;code class=&quot;language-text&quot;&gt;extends&lt;/code&gt; keyword altogether. Intersections can provide much more functionality.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;For those who find these concepts difficult, you can check out my previous posts. I have a series of articles about TypeScript. I write about my experience learning the language and I’m trying to explain my mental model using practical examples.&lt;/p&gt;
&lt;p&gt;If you are a visual person like me, you can also check out my 📺&lt;a href=&quot;https://youtube.com/playlist?list=PL73mkIDIrfyPKjkJ1V151lcgGEDHs3tgG&quot;&gt;YouTube series about TypeScript&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Cover photo credit: &lt;a href=&quot;https://unsplash.com/photos/gSOJhmAGqT8&quot;&gt;Shubham Dhage&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Mapped types will change how you write TypeScript]]></title><description><![CDATA[Mapped types make it possible to loop over your types, just like how you use foreach in normal JavaScript code.]]></description><link>http://www.nicotsou.com/tltr-typescript-mapped-types/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-mapped-types/</guid><pubDate>Thu, 14 Jul 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;In my previous articles I have presented a set of advanced TypeScript features that help you build logic for your types. I wrote about equivalent features that let you use functions, if statements, and arrays for your types. What else remains? Well, loops of course! And now you will be wondering, is there something equivalent in TypeScript to loop over types?&lt;/p&gt;
&lt;p&gt;Spoiler alert; Yes, there is!&lt;/p&gt;
&lt;h2&gt;Writing our first mapped type&lt;/h2&gt;
&lt;p&gt;Check out the following example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Key &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;username&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;email&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The previous code will produce the following type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	username&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	email&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;in&lt;/code&gt; operator is used to loop through the list of types a union contains. In the example above we use a temporary &lt;code class=&quot;language-text&quot;&gt;Key&lt;/code&gt; type to loop over the union &lt;code class=&quot;language-text&quot;&gt;&apos;username&apos; | &apos;email&apos;&lt;/code&gt;. On the left side of the expression is what we target. On the right side is the output. Since we used a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;, all our types will be set to be &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Yes, I know what you’re thinking. We just used a loop to create a type alias. Just because we can. One-liners ftw, right? Well, not exactly. This example may not look impressive or useful to you. It was meant to show you the syntax of a &lt;strong&gt;mapped type&lt;/strong&gt;. Yes, that’s how we call those things.&lt;/p&gt;
&lt;h2&gt;A more comprehensive example&lt;/h2&gt;
&lt;p&gt;Now, let me try one more time. Maybe this will impress you:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ReadonlyUser&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Key &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;keyof&lt;/span&gt; User&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; User&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Key&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let’s break down what is happening in this line of code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We have the &lt;code class=&quot;language-text&quot;&gt;User&lt;/code&gt; type from before, and what we want is to make all its properties &lt;em&gt;readonly&lt;/em&gt;. We named our new type &lt;code class=&quot;language-text&quot;&gt;ReadonlyUser&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Before we start implementing our mapped type, we use the &lt;code class=&quot;language-text&quot;&gt;readonly&lt;/code&gt; keyword. This will immediately set everything that follows to &lt;em&gt;readonly&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Then, we are looping through all the available properties of the type &lt;code class=&quot;language-text&quot;&gt;User&lt;/code&gt;. Remember, with the &lt;code class=&quot;language-text&quot;&gt;keyof&lt;/code&gt; keyword we get back a union with all the members of the given type. In our case, this will evaluate to &lt;code class=&quot;language-text&quot;&gt;&apos;username&apos; | &apos;email&apos;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Our last step is to set the output value. We name our temporary type as &lt;code class=&quot;language-text&quot;&gt;Key&lt;/code&gt;. We will use a &lt;strong&gt;lookup type&lt;/strong&gt; to get the corresponding &lt;code class=&quot;language-text&quot;&gt;User&lt;/code&gt; property.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now let’s try to battle test our newly created type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; ReadonlyUser &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  username&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;anonymous&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  email&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;myemailaddressis@private.hoho&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
user&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;email &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// Cannot assign to &apos;email&apos; because it is a read-only property&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It seems that is working. We cannot re-assign the &lt;code class=&quot;language-text&quot;&gt;email&lt;/code&gt; property. All the members of the &lt;code class=&quot;language-text&quot;&gt;user&lt;/code&gt; object are now readonly. Our job here is done.&lt;/p&gt;
&lt;p&gt;Now, let’s think about how we could generalize this functionality. A more practical solution would be to create a generic &lt;code class=&quot;language-text&quot;&gt;Readonly&amp;lt;T&gt;&lt;/code&gt; type, which would work with &lt;em&gt;any&lt;/em&gt; kind of object structure:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Readonly2&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Key &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;keyof&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;Key&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we basically replace &lt;code class=&quot;language-text&quot;&gt;User&lt;/code&gt; with &lt;code class=&quot;language-text&quot;&gt;&amp;lt;T&gt;&lt;/code&gt; and TypeScript will do its magic. Now we can refactor our &lt;code class=&quot;language-text&quot;&gt;user&lt;/code&gt; declaration:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Readonly2&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;User&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  username&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;anonymous&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  email&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;myemailaddressis@private.hoho&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You may be wondering if this number &lt;code class=&quot;language-text&quot;&gt;2&lt;/code&gt; at the end of the &lt;code class=&quot;language-text&quot;&gt;Readonly2&amp;lt;T&gt;&lt;/code&gt; is a typo. Well, my friends… If you follow this blog, you should already know that &lt;code class=&quot;language-text&quot;&gt;Readonly&amp;lt;T&gt;&lt;/code&gt; is a built-in utility type of TypeScript. That’s why we don’t even need to implement it. But now you know how to write it by yourself.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;For those who find these concepts difficult, you can check out my previous posts. I have a series of articles about TypeScript. I write about my experience learning the language and I’m trying to explain my mental model using practical examples. If you are a visual person like me, you can also check out my 📺&lt;a href=&quot;https://youtube.com/playlist?list=PL73mkIDIrfyPKjkJ1V151lcgGEDHs3tgG&quot;&gt;YouTube series about TypeScript&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Cover photo credit: &lt;a href=&quot;https://unsplash.com/photos/hw6Dh3R7yxA&quot;&gt;Pawel Czerwinski&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[TypeScript unions are more powerful than you think]]></title><description><![CDATA[Let me show you 4 advanced TypeScript features that will help you create types automatically, so that you don't have to manually do that.]]></description><link>http://www.nicotsou.com/tltr-typescript-unions/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-unions/</guid><pubDate>Wed, 29 Jun 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;Some days ago I stumbled upon a code review in my project and I was totally confused:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Movie &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Song &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Podcast&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;“Why on earth somebody is using the bitwise operator with strings?”, I was wondering.&lt;/p&gt;
&lt;p&gt;I started searching online. I went through JavaScript sources I trust. I wasn’t able to find anything useful there. I started Googling about bitwise operators and strings in JavaScript. I did find a couple of nice tricks with numbers. Nothing related to strings. Shift+Cmd+F and search for bitwise operators within the project. It turned out it was very popular within my team. How the hell has this become a trend in a codebase I maintain, and I don’t have a clue about it?&lt;/p&gt;
&lt;p&gt;Immediately I started panicking. The fear of missing out. What is this new hipster thingy that everybody knows, and Ι don’t have a clue? Am I that old? Am I still competent enough to have this job?&lt;/p&gt;
&lt;p&gt;The aha moment was seconds away. It’s a freaking &lt;em&gt;union&lt;/em&gt;. This code you are reviewing is not a JavaScript file. It’s a type definition for TypeScript. The moment of truth.&lt;/p&gt;
&lt;p&gt;In the following paragraphs I tried to collect everything I know about unions. I want to show you some smart tips and tricks you could do when you use unions and value types. I will also explain the concept of discriminated unions.&lt;/p&gt;
&lt;p&gt;For those who find these concepts difficult, you can check out my previous posts. I have a series of articles about TypeScript. I write about my experience learning the language and I’m trying to explain my mental model using practical examples. If you are a visual person like me, you can also check out my 📺&lt;a href=&quot;https://youtube.com/playlist?list=PL73mkIDIrfyPKjkJ1V151lcgGEDHs3tgG&quot;&gt;YouTube series about TypeScript&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Maroni? 🌰&lt;/p&gt;
&lt;h2&gt;What’s a union?&lt;/h2&gt;
&lt;p&gt;Allow me to make an analogy here.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If Generics is the equivalent function for your types, conditional types the equivalent of if-else statements, then definitely unions are the equivalent arrays.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Yes, basically that’s it! It’s an array of &lt;em&gt;allowed&lt;/em&gt; types. The types a value can have at a given moment. You can either set explicitly that a type can be a list of types, or TypeScript will implicitly create this list for you.&lt;/p&gt;
&lt;p&gt;Here’s the simplest format for a union:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Movie &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Song &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Podcast&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; can be a &lt;code class=&quot;language-text&quot;&gt;Movie&lt;/code&gt;, a &lt;code class=&quot;language-text&quot;&gt;Song&lt;/code&gt;, or a &lt;code class=&quot;language-text&quot;&gt;Podcast&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/afde69414fe87e04996efc61cdd1e0c1/1cab1/typescript-unions-sketch-01.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 44.106463878327%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAACAElEQVR42m1SUWvTUBROTeu20q5N07RNzVLUbrCARaiglkLxaQMHe7qg3WJsMu+aXG7apNkaQbivvoy9+NK/MH+CQl989G/4IPigiCgIXu/tHIrZgUNucu75cr7vO4LwTxBCruxAkjUIKe2EYXk4nFQCEKywUornyBoVTRTLjdNRcTcM16Dp6wCQq8JlAQAQgROU3t71q7SONr5e96sCPRP7cSyB6bTgOH61TUj2o+LlaMVtUcW7fbo7Ku4dHGt8kASg24+lBrGWGZj8o+ZuftZd6VcTL9Gi13jTRRsqgVkqH+VpCa/S2qFB6+4a7+MsbPsonwDsskm+acG1i4s8vmtek2po64Ph6wbxcp+aeJUwFve4JP5U55PB2SxjWVHtT0vqL+UgKLG/a68f4M42PtagHd1/+izs8BqELwsIvbi1jfHSEzdqce1se3zHdWOp1yNpCMdqAnAwwAp/Ok7UGhxODNNEsm3beSoA8VXPy+0zarxOGQAVaOp9e5ZZSMVALWZWgvJjGJYty1peNLVhhufirMCbX27468J8nj5/95o/VbRFBZhlTooH56akE4CE2Q8wVt51mPCK16Uq1Pn3E3CyMt4bq1z8R8zpGYCFeTss8wGYGfUgWKzV5cE1Gu5PKhGIanjwXEFs50wzli92DbN6v+9KD5mBfD8BOBP/B/kN8CCtTuoZrgMAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-unions-sketch-01.png&quot;
        title=&quot;&quot;
        src=&quot;/static/afde69414fe87e04996efc61cdd1e0c1/e996b/typescript-unions-sketch-01.png&quot;
        srcset=&quot;/static/afde69414fe87e04996efc61cdd1e0c1/17741/typescript-unions-sketch-01.png 263w,
/static/afde69414fe87e04996efc61cdd1e0c1/52211/typescript-unions-sketch-01.png 525w,
/static/afde69414fe87e04996efc61cdd1e0c1/e996b/typescript-unions-sketch-01.png 1050w,
/static/afde69414fe87e04996efc61cdd1e0c1/087e3/typescript-unions-sketch-01.png 1575w,
/static/afde69414fe87e04996efc61cdd1e0c1/8079d/typescript-unions-sketch-01.png 2100w,
/static/afde69414fe87e04996efc61cdd1e0c1/1cab1/typescript-unions-sketch-01.png 2444w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;As we’ve seen, TypeScript evaluates your code at compile time, and it can display the type a value can have in every single line of code. When a value is allowed to have more than one type, a union will be used.&lt;/p&gt;
&lt;p&gt;Looks like an array to me! 😃&lt;/p&gt;
&lt;h2&gt;Literal Types&lt;/h2&gt;
&lt;p&gt;In TypeScript, it is possible to set specific &lt;em&gt;values&lt;/em&gt; as types:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;RatingValues&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MediaTypes&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;video&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;audio&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IsFalsy&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0n&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You may ask why this is even useful. Well, think of it as an &lt;code class=&quot;language-text&quot;&gt;enum&lt;/code&gt;. It will protect you from assigning values that aren’t acceptable:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; rating&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; RatingValues &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it works&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; rating&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; RatingValues &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;3&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it throws an error&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; fileType&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; MediaTypes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;video&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it works&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; fileType&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; MediaTypes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Video&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it throws an error&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; falsyValue&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IsFalsy &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// nope!&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The available value types you can use are &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;boolean&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;, and &lt;code class=&quot;language-text&quot;&gt;bigint&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a7fd52cdd16ee45ee322a9a7d5e7294c/ddb69/typescript-unions-sketch-02.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60.45627376425855%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAACK0lEQVR42mNgwAP+M9iz3JfPkihNSuL9z5Mm8l8mXfqDVqHQFdMsif/8mYJAJYwMxID/UIX/eXJFnylnGs/yLFL7L5Cj/18i0/S9XI7iUeMcjdfK2Q63VHLZCRpWX1/PlpBQJJuUVCqVG53LB5dYtYrZeOZM1lCgvFVnKa/4zm5ut8J6IaB6JnyGMcXGVginpaWxrgoNZU5MrBStt69nAbk4Oq1AMja7QjgyrUhkp14x93++LJVjFoXSfqWlvFDtmN6vD61n05pSz/NfFBhGkpnGoUADV4WuYvZJS+O65ZHL/l8gw/a1cBLvfgagJUKZmgfM81QlZ9Zz4fWyW2Gh0GL7Mpn/QI3GRUUi+fH5Aj719VzTvPN0XqnkGu63j+f4Bwy7/1IZ6s8UsuQ3OOVq4YmN/4wVQC/f188X+C+eowg0VMotuVAoMq1Sw76+SMS2ulo2JSVX5r94ns5/iSy7/4JZ2kdM8w1wmgcKu/j8eoH/gmly/8UzHf9LZcqC7VFK4/8vnC3V7VrMHZuWJ+eRm8tnsaqX0xjo3dDeQk68yScrNIvHH2ioD1Cxd0WFIDCmmP7zJQuBDCyOKeZOTMwVBUVSfWgo20ygA/4TkxZhSSErK4sHFKsM/+uZQEkmOrpAMjeXiLQHT8gM+QLdgXmml4yLNP7zZZo2hxYrHjYpUDplkmtYEZAtHApMSqTlDIYk3k6/fPWzhtlSwKylBErY75TK+YFhyv+fyGwGADKpwFzjjt+tAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;unions-sketch-02.png&quot;
        title=&quot;&quot;
        src=&quot;/static/a7fd52cdd16ee45ee322a9a7d5e7294c/e996b/typescript-unions-sketch-02.png&quot;
        srcset=&quot;/static/a7fd52cdd16ee45ee322a9a7d5e7294c/17741/typescript-unions-sketch-02.png 263w,
/static/a7fd52cdd16ee45ee322a9a7d5e7294c/52211/typescript-unions-sketch-02.png 525w,
/static/a7fd52cdd16ee45ee322a9a7d5e7294c/e996b/typescript-unions-sketch-02.png 1050w,
/static/a7fd52cdd16ee45ee322a9a7d5e7294c/087e3/typescript-unions-sketch-02.png 1575w,
/static/a7fd52cdd16ee45ee322a9a7d5e7294c/8079d/typescript-unions-sketch-02.png 2100w,
/static/a7fd52cdd16ee45ee322a9a7d5e7294c/ddb69/typescript-unions-sketch-02.png 2684w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Remember, all these type aliases are &lt;em&gt;not&lt;/em&gt; variables. They will &lt;em&gt;not&lt;/em&gt; be available at runtime.&lt;/p&gt;
&lt;h2&gt;Template Literal Types&lt;/h2&gt;
&lt;p&gt;Imagine a variable that stores the font size:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; fontSize &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;2em&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It would be great if we could somehow guard these values with TypeScript, to limit typos that could cost us a lot of debugging. For sure, we can use literal types, but we can’t have all the possible font size combinations. We have infinite values.&lt;/p&gt;
&lt;p&gt;In TypeScript, we can use a &lt;strong&gt;template literal&lt;/strong&gt; to define even more customized types:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SizeValue&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;%&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;em&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;rem&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;px&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;vw&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;vh&apos;&lt;/span&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can control which values are acceptable and which are not:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; fontSize&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; SizeValue &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;2em&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it works&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; fontSize&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; SizeValue &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it throws an error&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; fontSize&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; SizeValue &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;2 px&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it throws an error&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This becomes quite useful when you are building a shared UI library. You have the choice to enforce strict rules, that will help the consumers of your library to understand which values will work and which will not. Less warnings and errors are thrown in your code. The less code, the happier you and your team will be.&lt;/p&gt;
&lt;p&gt;Just don’t overdo it. Remember, JavaScript became popular because of its simplicity and its flexibility. Not because it was strict.&lt;/p&gt;
&lt;h2&gt;Discriminated Unions&lt;/h2&gt;
&lt;p&gt;How many times in the past have you used a string value to differentiate the status of your objects? Is my message delivered? Is it still loading? Has it failed?&lt;/p&gt;
&lt;p&gt;Consider the following example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FileBase&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  filename&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Song&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FileBase &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt;
  &lt;span class=&quot;token function-variable function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;VideoClip&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FileBase &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp4&apos;&lt;/span&gt;
  &lt;span class=&quot;token function-variable function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CoverArt&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FileBase &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;png&apos;&lt;/span&gt;
  &lt;span class=&quot;token function-variable function&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Receipt&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FileBase &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;pdf&apos;&lt;/span&gt;
  &lt;span class=&quot;token function-variable function&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the type aliases above, the &lt;code class=&quot;language-text&quot;&gt;type&lt;/code&gt; property represents the file extension. A &lt;code class=&quot;language-text&quot;&gt;Song&lt;/code&gt; will be saved as &lt;code class=&quot;language-text&quot;&gt;mp3&lt;/code&gt;, a &lt;code class=&quot;language-text&quot;&gt;VideoClip&lt;/code&gt; as &lt;code class=&quot;language-text&quot;&gt;mp4&lt;/code&gt;, and the list goes on.&lt;/p&gt;
&lt;p&gt;I could have created an enum to store the available values. By using a plain string value, I may lose control over which types will be used, but I get some very interesting features.&lt;/p&gt;
&lt;p&gt;First off, I combine all file types under one umbrella:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;PlayerFiles&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Song &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; VideoClip &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; CoverArt &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Receipt&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now check this out. Let’s say that we want a function that only accepts specific file types:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;openFile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;file&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; PlayerFiles&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;file&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;type&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you hover over the &lt;code class=&quot;language-text&quot;&gt;file.type&lt;/code&gt; property, you will see that TypeScript was smart enough to understand that the available types are &lt;code class=&quot;language-text&quot;&gt;&quot;mp3&quot; | &quot;mp4&quot; | &quot;png&quot; | &quot;pdf”&lt;/code&gt;. Without even having an enum in place.&lt;/p&gt;
&lt;p&gt;And it gets even better. Let’s try to implement our function:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;openFile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;file&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; PlayerFiles&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;file&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;type&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;
	&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The moment we start typing our &lt;code class=&quot;language-text&quot;&gt;case&lt;/code&gt; block, autocompletion kicks in:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/217874845471f7284a988cb85d4f8d42/6052f/typescript-switch-case.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 25.475285171102662%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA6ElEQVR42o3OPU/CUBiG4cOXtGiibbGt/QAqxBjoAFqU6FTigOmBtLHgogxE//9PuD1q4iQJw7W8w/PeottaE+gb3PYngb1jPHhjfLkliT64Pnll1CwZamsGeolXy3HFai9HLBGh/kLQKPBa73SNHVfGhuC4xNFWWPWMs2qGoZiKVZO0/3FelziNXD0sEd+FHa0gbBbYFUk8yoknGa5/hx/O/nh7XAQzwuiBXjj/LfSPCjWo1qtLnErGNCl4TCWTJOVmOj/I7X1KPHzCVUHCFtnPkC1UtjpEpqTvSHrm4nDWgs7psyqUfAF0xJSCaXSi0AAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-switch-case.png&quot;
        title=&quot;&quot;
        src=&quot;/static/217874845471f7284a988cb85d4f8d42/e996b/typescript-switch-case.png&quot;
        srcset=&quot;/static/217874845471f7284a988cb85d4f8d42/17741/typescript-switch-case.png 263w,
/static/217874845471f7284a988cb85d4f8d42/52211/typescript-switch-case.png 525w,
/static/217874845471f7284a988cb85d4f8d42/e996b/typescript-switch-case.png 1050w,
/static/217874845471f7284a988cb85d4f8d42/087e3/typescript-switch-case.png 1575w,
/static/217874845471f7284a988cb85d4f8d42/6052f/typescript-switch-case.png 2030w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;That’s awesome. But what’s even more amazing is that if we pick one of the available types, let’s say &lt;code class=&quot;language-text&quot;&gt;mp3&lt;/code&gt;, we are getting autocompletion for their corresponding properties:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/e6302ad276e03ec3e3a649885c8e2408/f291a/typescript-switch-case-autocompletion.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 19.771863117870723%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAYAAACOXx+WAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA4ElEQVR42mXOW0/CQBCG4Y21CV2jeAj0gFYE2y0HueAGNAHvLIYe0HqIwciVif//D7xuiUGJF0++yWxmZkVTvuDuL7FrX/TbK4bBO6Pwk87BK12zoFN5pqU5It9ii0xnts7fOkWcyycaetCRH6jjJVH1jW5thV99xNnLqVspR8YdJ+ZsiyNTbCvZpC11bS7KhQV+ZYG7k1IXiTbXDxmD0QPD8T3R1S0X4ZiWut5oRzfrftiboPpTVG9KNJhwdhgjvN0czyi/rK/8cIwU1SyI/IRLNybwZv+U/b+CRsypNecbfwR2j00mdvsAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-switch-case-autocompletion.png&quot;
        title=&quot;&quot;
        src=&quot;/static/e6302ad276e03ec3e3a649885c8e2408/e996b/typescript-switch-case-autocompletion.png&quot;
        srcset=&quot;/static/e6302ad276e03ec3e3a649885c8e2408/17741/typescript-switch-case-autocompletion.png 263w,
/static/e6302ad276e03ec3e3a649885c8e2408/52211/typescript-switch-case-autocompletion.png 525w,
/static/e6302ad276e03ec3e3a649885c8e2408/e996b/typescript-switch-case-autocompletion.png 1050w,
/static/e6302ad276e03ec3e3a649885c8e2408/087e3/typescript-switch-case-autocompletion.png 1575w,
/static/e6302ad276e03ec3e3a649885c8e2408/8079d/typescript-switch-case-autocompletion.png 2100w,
/static/e6302ad276e03ec3e3a649885c8e2408/f291a/typescript-switch-case-autocompletion.png 2258w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;If you make the mistake of calling the wrong object member, TypeScript will throw an error:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/27dd09d1b2933d785ee8561d55b8a373/fa63f/typescript-switch-case-mistake.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 26.23574144486692%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAABYlAAAWJQFJUiTwAAABFklEQVR42nXOy07CUBSF4dLS0ta0AgVKW1racCuEiYliwoCBGjVC5NISRyYaE9//DX4PRwZOHHxZ6yQne29l4JXE7huZdSSvbhnpB/r6K2F9iWnMMLScmjrBOKmMhQmmOsXUpljab9raDKua01RXKH2rpFsr6DrvZM0jY79k2BDv9hzPy6h7KX6S04pGwlDmZSfF8RLcdip7zQmxvYCGsUBJ7CORXuA3v5n2PrlJP7jqfzGMH5ind6SNFbF/TxY9SYPkmZa7xLUWXNq3kmsucMxrWvqjuNAuiao7MfSUe3qi94yC7OJA5hSEYllH2Z7tZHaVPUHlQKAcZIZnQWWPEptiiL4lUDfi84uwltpnp+5Lmz/W//oB7a+KdT0ToYgAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-switch-case-mistake.png&quot;
        title=&quot;&quot;
        src=&quot;/static/27dd09d1b2933d785ee8561d55b8a373/e996b/typescript-switch-case-mistake.png&quot;
        srcset=&quot;/static/27dd09d1b2933d785ee8561d55b8a373/17741/typescript-switch-case-mistake.png 263w,
/static/27dd09d1b2933d785ee8561d55b8a373/52211/typescript-switch-case-mistake.png 525w,
/static/27dd09d1b2933d785ee8561d55b8a373/e996b/typescript-switch-case-mistake.png 1050w,
/static/27dd09d1b2933d785ee8561d55b8a373/087e3/typescript-switch-case-mistake.png 1575w,
/static/27dd09d1b2933d785ee8561d55b8a373/8079d/typescript-switch-case-mistake.png 2100w,
/static/27dd09d1b2933d785ee8561d55b8a373/fa63f/typescript-switch-case-mistake.png 2298w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;It’s unbelievable how much functionality we get for free here. We may lose this control of having the available values in one place, but we get back the flexibility to dynamically add/remove types, without having to maintain a hardcoded enum. If this list of types was coming from an api, for example, I assume somebody else would have done this check for you.&lt;/p&gt;
&lt;p&gt;These kinds of types are called &lt;strong&gt;discriminated union types&lt;/strong&gt;. They are ideal in cases you don’t have a clear list of enum values, or this list is extremely huge, or in cases when you want the flexibility to add/remove values dynamically.&lt;/p&gt;
&lt;p&gt;For example, if your app uses Redux, you can use a discriminated union type to build your action creators.&lt;/p&gt;
&lt;p&gt;Cover photo credit: &lt;a href=&quot;https://unsplash.com/photos/kdnSoGLjoZ8&quot;&gt;Li Zhang&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Four TypeScript features that write your types for you]]></title><description><![CDATA[Let me show you 4 advanced TypeScript features that will help you create types automatically, so that you don't have to manually do that.]]></description><link>http://www.nicotsou.com/tltr-typescript-advanced-types/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-advanced-types/</guid><pubDate>Wed, 22 Jun 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;In this article, I’m going to list some advanced tooling TypeScript provides to customize our type annotations. It was a great surprise for me when I discovered these features. It changed my mind about TypeScript itself. In a positive way, of course.&lt;/p&gt;
&lt;p&gt;For those who find these concepts difficult, you can check out my previous posts. I have a series of articles about TypeScript. I write about my experience learning the language and I’m trying to explain my mental model using practical examples. If you are a visual person like me, you can also check out my 📺&lt;a href=&quot;https://youtube.com/playlist?list=PL73mkIDIrfyPKjkJ1V151lcgGEDHs3tgG&quot;&gt;YouTube series about TypeScript&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;The secret power of the typeof keyword&lt;/h2&gt;
&lt;p&gt;You already know the &lt;code class=&quot;language-text&quot;&gt;typeof&lt;/code&gt; keyword. It’s a way to check the types of your JavaScript values or variables:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Hello&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// returns &apos;string&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;123&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// returns &apos;number&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But did you know that you can use it to dynamically generate types from inferred ones?&lt;/p&gt;
&lt;p&gt;TypeScript has enhanced the &lt;code class=&quot;language-text&quot;&gt;typeof&lt;/code&gt; keyword with more features. It’s like &lt;code class=&quot;language-text&quot;&gt;typeof&lt;/code&gt; on steroids. It doesn’t only understand JavaScript-related types, but it has full support for TypeScript types, as well. Of course, these features will &lt;em&gt;only&lt;/em&gt; work at compile-time and not at runtime.&lt;/p&gt;
&lt;p&gt;Let’s say that we have the following &lt;code class=&quot;language-text&quot;&gt;artist&lt;/code&gt; object:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;someAlphanumericStringYouDontEvenCareAbout&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Queen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	followed&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;
	plays&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;111&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;band&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Fun fact; I just realized that it took me 11 articles to come up with another example. So far, I’ve been using the type &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; in my previous articles, but we’ve never been involved with an &lt;code class=&quot;language-text&quot;&gt;artist&lt;/code&gt; object structure. You are now probably thinking about creating a new type definition for artists. What if I told you that TypeScript can do that for you?&lt;/p&gt;
&lt;p&gt;In the following example we are using &lt;strong&gt;type inference&lt;/strong&gt; to automatically create a type &lt;code class=&quot;language-text&quot;&gt;Artist&lt;/code&gt;, from the &lt;code class=&quot;language-text&quot;&gt;artist&lt;/code&gt; object:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Artist&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; artist&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Yes, this implies that we have an artist type alias in place, without having to define it manually.&lt;/p&gt;
&lt;p&gt;Of course, like any other type alias, we can use it to annotate other objects:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; getMeThoseArtists &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Artist&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Pretty cool, right? It’s ideal, when you have to deal with objects that change over time, and yet you &lt;em&gt;still&lt;/em&gt; want to protect them with types.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/6a2b8ba28e493971f8d0272bb8ef91b5/29114/donuts.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.90494296577947%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAIAAABr+ngCAAAACXBIWXMAAC4jAAAuIwF4pT92AAACwUlEQVR42k2SW08TQRiG90f4B/gT3plo5EJvTLjVGBMTlWMB20oxUARBSREKPWzPBypIUQ4CrSBygUEFhNJ220JboO3M7LYNgZ3dlnjt7LYlJE8mmew+837fN0N5CiVXvuQulDzFsrdYJltnQbTlRQtLEGhWMEN+ghUDgPsaDntD0c2fa4fONxujyjlDD+XiRDsn2POiMy+6pYNkkxNp2aQh1iNxKcedGrTxse60V/fdqXPMf+pfXVf8ClN2VrASONHKijZOoqqRTISNkCdf2Vnr9tN7ex7DcmBBG820J89aEoXmCKCsSLAQWEkma02TMCGp4Kk0LOk7gObx72cN0x57dxarojlFJNcazlG2qixYa03SCJNMEwFc6NnSTPKEVzWsqJ4nVQ83+zs0qTNlDHVEYVtYSsaWio+IJpVaMUnBRsAbAE8D/mBcC1vus4oHk/5pTQa/ZICCga3hLOVksZ3Ftqosm5BkVk1j7nwiX/ZvM6ExXdBsHmZAb7qojKNWBjQdZCgPKzhZoeJLyRATTFA2wcU4wp4UwtO+ot3FDAy6l4PazHlXDLZHQdP+CeXjMPFdrOBgsdwCJtdjlJOJrAf8TLZ4TBt8zY2ubrV1aW7g9OJ1HCoZ0BY6pT7mhUlOcBMZYTuSfLoWLg0M4HkA0M7ngG/UP9LkDH55l8V9CaiJQaVcNnbWNClWlmWfjOpcjy4DR7uQvhvorFt8ccO16NKhf4OHsDeB1JEsRX6SkEdV0a5jQqI3U4itWndnBwO2PvfKvAFgXbrQf8S9YgBFejPVZHMluXaKhQycKy/Ej3hHz9aIeqrzkWO0Tx1nG/fST/4cPdpKSPI1U7gGtpC2uUt/LBH/0Pi3+fZ6fV1vV8vN1dCt4M6dtb36tX2KxNK1wKvXcgW5P/J+TZHE2MbG8Lcfb0PJoVThfbo4fFwcSuX/A9PIoSXziz/AAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;donuts.png&quot;
        title=&quot;&quot;
        src=&quot;/static/6a2b8ba28e493971f8d0272bb8ef91b5/e996b/donuts.png&quot;
        srcset=&quot;/static/6a2b8ba28e493971f8d0272bb8ef91b5/17741/donuts.png 263w,
/static/6a2b8ba28e493971f8d0272bb8ef91b5/52211/donuts.png 525w,
/static/6a2b8ba28e493971f8d0272bb8ef91b5/e996b/donuts.png 1050w,
/static/6a2b8ba28e493971f8d0272bb8ef91b5/087e3/donuts.png 1575w,
/static/6a2b8ba28e493971f8d0272bb8ef91b5/29114/donuts.png 1920w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;The keyof keyword&lt;/h2&gt;
&lt;p&gt;TypeScript provides another similar keyword called &lt;code class=&quot;language-text&quot;&gt;keyof&lt;/code&gt;. You can use it with any type to get its properties. It returns a union with literal values.&lt;/p&gt;
&lt;p&gt;Here are some examples:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArtistKey&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;keyof&lt;/span&gt; Artist &lt;span class=&quot;token comment&quot;&gt;// &quot;type&quot; | &quot;id&quot; | &quot;name&quot; | &quot;followed&quot; | &quot;plays&quot;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;StringKey&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;keyof&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// all members of the string object like: &quot;toString&quot; | &quot;charAt&quot; | &quot;charCodeAt&quot; | etc...&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;UndefinedKey&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;keyof&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// never&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can use the &lt;code class=&quot;language-text&quot;&gt;keyof&lt;/code&gt; keyword in multiple places, and especially within generic types to fine-tune your types and reduce the clutter.&lt;/p&gt;
&lt;h2&gt;Lookup types&lt;/h2&gt;
&lt;p&gt;It’s nice to retrieve the full property list of an object using the &lt;code class=&quot;language-text&quot;&gt;keyof&lt;/code&gt; keyword, but sometimes you want to get the type of a specific property in that list. That’s what lookup types are all about.&lt;/p&gt;
&lt;p&gt;For example, what is the type of the &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; property of an &lt;code class=&quot;language-text&quot;&gt;Artist&lt;/code&gt;? Is it a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;? Is it a &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt;? Of course we can open the related interface and check it by ourselves, and then hardcode its type in our code. But what if that property changes? Remember, we don’t have an interface for the &lt;code class=&quot;language-text&quot;&gt;Artist&lt;/code&gt; object. We inferred the whole thing.&lt;/p&gt;
&lt;p&gt;Well, you know where this is going:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArtistId&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Artist&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;id&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// string&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Yup! It’s a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;. At least in this iteration.&lt;/p&gt;
&lt;h2&gt;The infer keyword&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://nicotsou.com/tltr-typescript-conditional-types/&quot;&gt;Conditional types&lt;/a&gt; can be used together with generics to elevate your types and create utility types that you can reuse in multiple places. This reduces boilerplate and enables a lot of functionality. In case you are wondering what a conditional type is, I have dedicated &lt;a href=&quot;https://nicotsou.com/tltr-typescript-conditional-types/&quot;&gt;another article&lt;/a&gt; on this topic.&lt;/p&gt;
&lt;p&gt;As a refresher, here’s an example of a conditional type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;NonNullable&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;never&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The type &lt;code class=&quot;language-text&quot;&gt;NonNullable&amp;lt;T&gt;&lt;/code&gt; accepts a type &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt; and it checks if it extends &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;. Then, if this is true, it excludes those types by returning &lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt;. Otherwise, it returns the same type &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now, we can use this type to ensure we won’t have any surprises:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;NullableString&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;SafeString&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; NonNullable&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;NullableString&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the example above, the &lt;code class=&quot;language-text&quot;&gt;NullableString&lt;/code&gt; can get &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt;. Our &lt;code class=&quot;language-text&quot;&gt;SafeString&lt;/code&gt; can only accept &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;. This is actually a built-in utility type.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Now, here’s an example of how you can use the &lt;code class=&quot;language-text&quot;&gt;infer&lt;/code&gt; keyword to infer from types we compare against the true branch:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Flatten&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;&lt;span class=&quot;token builtin&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;infer&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we use &lt;code class=&quot;language-text&quot;&gt;infer&lt;/code&gt; to create a reference to the type of the array, and we name that reference &lt;code class=&quot;language-text&quot;&gt;R&lt;/code&gt;. This acts as a variable, similar to how we use &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt;. If our type is an &lt;code class=&quot;language-text&quot;&gt;Array&lt;/code&gt; of &lt;code class=&quot;language-text&quot;&gt;R&lt;/code&gt;, we return that &lt;code class=&quot;language-text&quot;&gt;R&lt;/code&gt;. Otherwise we return the original type &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Practically, this is a utility type that flattens array types, and here’s how we can use it:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArrayType&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Flatten&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Receipt&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Returns the type Receipt&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In case I don’t pass any array, it returns the same type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArrayType&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Flatten&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Receipt&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Returns the type Receipt&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Of course, we can pass a list of types in a union:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArrayType&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Flatten&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Receipt&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Movie&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Receipt | Movie&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By using the &lt;code class=&quot;language-text&quot;&gt;infer&lt;/code&gt; keyword, you are creating a new generic type &lt;code class=&quot;language-text&quot;&gt;R&lt;/code&gt;, which then can be returned as a type value. So basically, you are catching the type of the generic &lt;code class=&quot;language-text&quot;&gt;Array&amp;lt;T&gt;&lt;/code&gt;. This can become really useful, when you are developing your APIs.&lt;/p&gt;
&lt;p&gt;A cool example is the following utility type &lt;code class=&quot;language-text&quot;&gt;PromiseReturnType&amp;lt;T&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;PromiseReturnType&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;&lt;span class=&quot;token builtin&quot;&gt;Promise&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;infer&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can use it to unpack the type of your promise.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;As you can see, the world of TypeScript hides a lot of gems. It takes quite a lot of time and effort to understand all these things and integrate them into your daily developments.&lt;/p&gt;
&lt;p&gt;But don’t be scared!&lt;/p&gt;
&lt;p&gt;These are rarely used features. It’s nice to have an understanding, because it helps you understand from what the language is made of. And frankly, there are endless type definitions out there that leverage those features.&lt;/p&gt;
&lt;p&gt;If you are developing a shared library, and you want to provide a robust api, then you should spend more time playing with these features. If you are an application developer, who consumes already defined type definitions, then it’s not really a must to memorize all these keywords. Keep them as a reference for later, though. You never know when you’re gonna be needing them.&lt;/p&gt;
&lt;p&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/S2XSvVyUxpo&quot;&gt;Ayush Bharshankar&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[TLTR; TypeScript Utility Types]]></title><description><![CDATA[Utility types help you create new types on the fly, from existing types. They help you reduce code duplication and enforce strict rules.]]></description><link>http://www.nicotsou.com/tltr-typescript-utility-types/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-utility-types/</guid><pubDate>Wed, 15 Jun 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;We’ve seen how &lt;a href=&quot;https://nicotsou.com/tltr-typescript-generics/&quot;&gt;generic types&lt;/a&gt; can help us reduce code, by providing a way to write functions for our types. This not only reduces a lot of clutter, but it also allows us to utilize types in ways we never thought of before. TypeScript takes this to another level, by introducing a couple of helpful built-in constraints, that will allow you to fine-tune your types.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Utility types help you create new types on the fly, from existing types. They help you reduce code duplication and enforce strict rules.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It’s useful for every TypeScript developer to understand the available utility types and how to use them. This will help you reduce a lot of code in your application to improve readability. It will also save you time.&lt;/p&gt;
&lt;p&gt;In this article I’m listing my favorite &lt;strong&gt;utility types&lt;/strong&gt;. The ones I have seen in most of the TypeScript projects I have worked on. I’ve curated practical examples I’ve seen in production, to help you understand how these utilities can be used on a daily basis. I hope it will inspire you to find solutions that apply to your own projects.&lt;/p&gt;
&lt;p&gt;Of course, this is not the complete list of built-in utility types. For a more comprehensive list, you can check out the &lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/utility-types.html&quot;&gt;official documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For those who find these concepts difficult, you can check out my previous posts. I have a series of articles about TypeScript. I write about my experience learning the language and I’m trying to explain my mental model using practical examples. If you are a visual person like me, you can also check out my 📺&lt;a href=&quot;https://youtube.com/playlist?list=PL73mkIDIrfyPKjkJ1V151lcgGEDHs3tgG&quot;&gt;YouTube series about TypeScript&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Peanut butter? 🍯&lt;/p&gt;
&lt;h2&gt;Why do we need utility types?&lt;/h2&gt;
&lt;p&gt;Here’s our favorite &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; interface:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  genres&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There are cases when you want to reuse general types like this one, with some small modifications. Let’s say to omit some of their properties, to include new ones, to enforce read-only, and many more.&lt;/p&gt;
&lt;p&gt;We will use this interface as a reference for most of the utility types below.&lt;/p&gt;
&lt;h2&gt;Partial&lt;/h2&gt;
&lt;p&gt;There are cases that we want to deal with a part of an interface. Instead of creating new interfaces every time, with some small modifications, we can use the &lt;code class=&quot;language-text&quot;&gt;Partial&amp;lt;T&gt;&lt;/code&gt; utility type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;
  genres&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
  timesPlayed&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;updateTrackDetails&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; fieldsToUpdate&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Partial&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Track&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;track&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;fieldsToUpdate &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;1&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  genres&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Hard rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Progressive rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  timesPlayed&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;updateTrackDetails&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;track&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  timesPlayed&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Our &lt;code class=&quot;language-text&quot;&gt;updateTrackDetails()&lt;/code&gt; function accepts a track and a subset of the Track type. We didn’t have to specify which properties were allowed. Every time you change the &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; interface, all the other partial types will get the update for free.&lt;/p&gt;
&lt;p&gt;Less code to maintain. More time for more important stuff. 🙆‍♂️&lt;/p&gt;
&lt;h2&gt;Required&lt;/h2&gt;
&lt;p&gt;Now, let’s have a look at the &lt;code class=&quot;language-text&quot;&gt;Required&amp;lt;T&gt;&lt;/code&gt; utility type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;strictUpdateTrack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; fieldsToUpdate&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Required&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Track&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;track&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;fieldsToUpdate &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; initialObject&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;1&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  genres&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Hard rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Progressive rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  timesPlayed&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;strictUpdateTrack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;initialObject&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;2&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Some other track&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2022&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  genres&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Pop&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  timesPlayed&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example, the consumers of the &lt;code class=&quot;language-text&quot;&gt;strictUpdateTrack()&lt;/code&gt; function will have to pass &lt;em&gt;all&lt;/em&gt; the properties of the track object. Even the &lt;code class=&quot;language-text&quot;&gt;releasedAt&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;genres&lt;/code&gt; which are optional.&lt;/p&gt;
&lt;h2&gt;Readonly&lt;/h2&gt;
&lt;p&gt;I suppose you are familiar with the &lt;code class=&quot;language-text&quot;&gt;readonly&lt;/code&gt; keyword:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;readonly&lt;/span&gt; id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  username&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Since the &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; property is &lt;code class=&quot;language-text&quot;&gt;readonly&lt;/code&gt;, the following code will not compile:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; User &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;A123&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  username&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;nicotsou&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nicos&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
user&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;321&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Cannot assign to &apos;id&apos; because it is a read-only property.&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;Readonly&amp;lt;T&gt;&lt;/code&gt; utility type works pretty similar. It basically makes all the properties of the given type &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt; read-only.&lt;/p&gt;
&lt;p&gt;For example, in the following function somebody used the &lt;code class=&quot;language-text&quot;&gt;push()&lt;/code&gt; array method to add a new entry to the &lt;code class=&quot;language-text&quot;&gt;users&lt;/code&gt; array:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;addUser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;users&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; User&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newUser&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; User&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  users&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newUser&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// oops, we have mutated the array&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; users
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will cause a side-effect, because the &lt;code class=&quot;language-text&quot;&gt;push()&lt;/code&gt; method &lt;em&gt;mutates&lt;/em&gt; the array. We can easily add a rule to prevent such mistakes, by specifying the &lt;code class=&quot;language-text&quot;&gt;users&lt;/code&gt; parameter as a &lt;code class=&quot;language-text&quot;&gt;readonly&lt;/code&gt; type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;addUser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;users&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Readonly&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;User&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newUser&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; User&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  users&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newUser&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Property &apos;push&apos; does not exist on type &apos;readonly User[]&apos;.&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; users
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The message isn’t clear enough, but it &lt;em&gt;does&lt;/em&gt; work. Basically, TypeScript will prevent you from mutating this array.&lt;/p&gt;
&lt;p&gt;Specifically, for arrays you can use the &lt;code class=&quot;language-text&quot;&gt;ReadonlyArray&amp;lt;T&gt;&lt;/code&gt; utility type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;addUser&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;users&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; ReadonlyArray&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;User&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newUser&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; User&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;users&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; user &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we basically replaced &lt;code class=&quot;language-text&quot;&gt;Readonly&amp;lt;User[]&gt;&lt;/code&gt; with &lt;code class=&quot;language-text&quot;&gt;ReadonlyArray&amp;lt;User&gt;&lt;/code&gt;, which is a cleaner way to write &lt;code class=&quot;language-text&quot;&gt;Readonly&amp;lt;Array&amp;lt;User&gt;&gt;&lt;/code&gt;. Our function also returns a fresh reference to the &lt;code class=&quot;language-text&quot;&gt;users&lt;/code&gt; array, without mutating the original structure.&lt;/p&gt;
&lt;p&gt;Problem resolved. 🙆‍♂️&lt;/p&gt;
&lt;p&gt;Have in mind, that these checks only affect the first level and TypeScript will not perform a deep check:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  letter&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;readonly&lt;/span&gt; id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Id
  username&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; user&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; User &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;A123&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; letter&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;A&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  username&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;nicotsou&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nicos&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
user&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;letter &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;321&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it is perfectly legit!&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For that you will need a more sophisticated utility type. The bad news is that TypeScript doesn’t have a built-in one. The good news is that you can write one yourself.&lt;/p&gt;
&lt;h2&gt;Record&lt;/h2&gt;
&lt;p&gt;We usually use enums or literal types to keep statuses. The &lt;code class=&quot;language-text&quot;&gt;Record&amp;lt;T&gt;&lt;/code&gt; utility type makes it easier to specify object structures that leverage those statuses to create a key/value object structure.&lt;/p&gt;
&lt;p&gt;For example, imagine that we have to implement a button with an icon. In Bootstrap we can have &lt;a href=&quot;https://getbootstrap.com/docs/4.2/components/spinners/#buttons&quot;&gt;buttons with icons and different colors&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/da85363f8aeaa475b9f5e2532c74429d/57e3f/loading-button.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 15.209125475285171%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAADCAYAAACTWi8uAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAX0lEQVR42p2MwQpAQABE94s5ueHkZzg4UKTkKveN9mBTikRuzo9tv2BNvaam5on1hkpCr6FT0E62mxGGBecIc/ZSiEuIPoIcwgL8DJL6h/B+QG4wn6AOGHeL2fTlLnwB27DkE7a51vkAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;loading-button.png&quot;
        title=&quot;&quot;
        src=&quot;/static/da85363f8aeaa475b9f5e2532c74429d/e996b/loading-button.png&quot;
        srcset=&quot;/static/da85363f8aeaa475b9f5e2532c74429d/17741/loading-button.png 263w,
/static/da85363f8aeaa475b9f5e2532c74429d/52211/loading-button.png 525w,
/static/da85363f8aeaa475b9f5e2532c74429d/e996b/loading-button.png 1050w,
/static/da85363f8aeaa475b9f5e2532c74429d/087e3/loading-button.png 1575w,
/static/da85363f8aeaa475b9f5e2532c74429d/8079d/loading-button.png 2100w,
/static/da85363f8aeaa475b9f5e2532c74429d/57e3f/loading-button.png 2598w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;To indicate that something is idle, loading, or failed, we would need to know the loading status in order to display a loading indicator:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;LoadingStatuses&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;idle&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;loading&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;failed&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To describe the condition of the button at any given time, we would need to set its &lt;code class=&quot;language-text&quot;&gt;icon&lt;/code&gt; and its &lt;code class=&quot;language-text&quot;&gt;color&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Icon&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  icon&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  color&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can have a more comprehensive object structure, that sets the default icons and colors for every single loading state. Here’s when we can use the &lt;code class=&quot;language-text&quot;&gt;Record&amp;lt;T&gt;&lt;/code&gt; utility type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; icons&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Record&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;LoadingStatuses&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Icon&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  idle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    icon&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;normal&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    color&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;black&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  loading&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    icon&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;spinner&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    color&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;blue&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  failed&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    icon&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;warning&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    color&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;red&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that TypeScript will require all the statuses to be present in your object, and it will not allow you to add statuses that aren’t part of the &lt;code class=&quot;language-text&quot;&gt;LoadingStatuses&lt;/code&gt; type.&lt;/p&gt;
&lt;h2&gt;Pick&lt;/h2&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;Pick&amp;lt;T, K extends keyof T&gt;&lt;/code&gt; utility type is meant to help us create a subset of a type, by specifying the members we want to keep from the original type. This works very similar to the &lt;code class=&quot;language-text&quot;&gt;Partial&amp;lt;T&gt;&lt;/code&gt; utility type, but it gives us more control over the properties that TypeScript has to check.&lt;/p&gt;
&lt;p&gt;Here’s an enhanced version of our &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; interface:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Readonly&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;
  genres&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
  timesPlayed&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The goal is to create a collection with the times every track has been played:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; plays &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;3&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  timesPlayed&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here’s how we can easily use the &lt;code class=&quot;language-text&quot;&gt;Pick&amp;lt;T, K extends keyof T&gt;&lt;/code&gt; utility type to define a new &lt;code class=&quot;language-text&quot;&gt;TrackPlays&lt;/code&gt; type alias:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TrackPlays&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Pick&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Track&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;id&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;timesPlayed&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This means that TypeScript will not complain if we don’t pass any other required property from the original Track interface:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; plays&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; TrackPlays &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;3&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  timesPlayed&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you hover over the &lt;code class=&quot;language-text&quot;&gt;TrackPlays&lt;/code&gt; type, you will get an overview of its contents:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/10a7772842c2d0c7552658f6798a27a6/6b95e/tltr-pick-utility-type.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 34.60076045627377%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAABYlAAAWJQFJUiTwAAABGElEQVR42oXR2U7CUBSF4TIJFg0QFWiroEylE1rQGpFATBSZWkUbY3z/9/g9YL0wAbz4cnIu1srO3pIW9xkVvhgUPrnJ+Xj5MZY8oxh7pSgFlCLlPxZbSWoiwMgscdIhRn6J1xzx0HToVbrY2jUNtc9FyaNaXLldU1MTEfa3FMZfUBILFGlGNTfAME3unA7P3RpDV+ferdF361hmA9c6xzHraPuP0dQbCs9SIXU5xNp7wj4ZikAbz+zQM1w6+pXQ4lLXsVttunpT/A20zJMo3DLhaXJJ++AD83BKTZ5jH08wj8ZUslPU9AQlKaR+lH/f2Hz7DrV4gC2/Y2VDWpk3URBEx/CjPW2y4yiaKFDFpdXYYk1Z2RH4zzdkuciJt21FsAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;tltr-pick-utility-type.png&quot;
        title=&quot;&quot;
        src=&quot;/static/10a7772842c2d0c7552658f6798a27a6/e996b/tltr-pick-utility-type.png&quot;
        srcset=&quot;/static/10a7772842c2d0c7552658f6798a27a6/17741/tltr-pick-utility-type.png 263w,
/static/10a7772842c2d0c7552658f6798a27a6/52211/tltr-pick-utility-type.png 525w,
/static/10a7772842c2d0c7552658f6798a27a6/e996b/tltr-pick-utility-type.png 1050w,
/static/10a7772842c2d0c7552658f6798a27a6/6b95e/tltr-pick-utility-type.png 1458w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Omit&lt;/h2&gt;
&lt;p&gt;Similar to Pick, &lt;code class=&quot;language-text&quot;&gt;Omit&amp;lt;T, K extends keyof T&gt;&lt;/code&gt; works the opposite way. You specify the properties you want to &lt;em&gt;omit&lt;/em&gt; from the original type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Readonly&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;
  genres&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
  timesPlayed&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TrackWithoutGenres&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Omit&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Track&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;genres&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; plays&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; TrackWithoutGenres &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;1&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the example above, TypeScript will no longer complain if we forget to include the &lt;code class=&quot;language-text&quot;&gt;genres&lt;/code&gt; property. Basically, with this utility type, we can make all the required properties &lt;em&gt;optional&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;Exclude&lt;/h2&gt;
&lt;p&gt;Consider the following genres enum:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Genres&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;pop&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;rock&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;alternative&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;metal&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;r&amp;amp;b&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;jazz&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can use the &lt;code class=&quot;language-text&quot;&gt;Exclude&amp;lt;UnionType, ExcludedMembers&gt;&lt;/code&gt; utility type to exclude some of the states:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CommercialGenres&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Exclude&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Genres&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;jazz&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;metal&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The produced type will only contain &lt;code class=&quot;language-text&quot;&gt;&apos;pop&apos; | &apos;rock&apos; | &apos;alternative&apos; | &apos;r&amp;amp;b’&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Extract&lt;/h2&gt;
&lt;p&gt;Similar to &lt;code class=&quot;language-text&quot;&gt;Exclude&lt;/code&gt;, we can use &lt;code class=&quot;language-text&quot;&gt;Extract&amp;lt;Type, Union&gt;&lt;/code&gt; to create a type with types that match with the given union:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CommercialGenres&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Extract&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Genres&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;rock&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;instrumental&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;ost&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will only return &lt;code class=&quot;language-text&quot;&gt;‘rock’&lt;/code&gt;, since the other two types are not included in &lt;code class=&quot;language-text&quot;&gt;Genres&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You can use the &lt;code class=&quot;language-text&quot;&gt;Extract&amp;lt;Type, Union&gt;&lt;/code&gt; utility type to get the common properties between 2 object types.&lt;/p&gt;
&lt;h2&gt;NonNullable&lt;/h2&gt;
&lt;p&gt;There are situations when we want to ensure that a value is not &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;. Here’s where &lt;code class=&quot;language-text&quot;&gt;NonNullable&amp;lt;Type&gt;&lt;/code&gt; utility type can help:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TrackDuration&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;printDuration&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; NonNullable&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;TrackDuration&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;duration&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; nullDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; TrackDuration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;printDuration&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nullDuration&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Argument of type &apos;null&apos; is not assignable to parameter of type &apos;number&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, the &lt;code class=&quot;language-text&quot;&gt;printDuration()&lt;/code&gt; function is expecting a &lt;code class=&quot;language-text&quot;&gt;duration&lt;/code&gt; parameter that is not &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;. That’s why when we call it with the &lt;code class=&quot;language-text&quot;&gt;nullDuration&lt;/code&gt;, which is &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt;, we get back an error.&lt;/p&gt;
&lt;p&gt;This utility type can be very useful if you are using an older version of the language (prior to 2.0) or if you are working on a project that has the &lt;code class=&quot;language-text&quot;&gt;strictNullChecks&lt;/code&gt; option disabled in the compiler configuration. In both cases, TypeScript will consider &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt; as allowed values for basically &lt;em&gt;every&lt;/em&gt; type. TypeScript’s strict mode can help us avoid all common mistakes that arise with nullable types. If we were using strict mode, the behavior would have been the same if we had used &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt; as the type of the &lt;code class=&quot;language-text&quot;&gt;duration&lt;/code&gt; parameter.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;As we’ve seen, utility types help you reuse existing types and modify them according to your needs. This reduces code duplication, because you only need to declare the original types once. It also helps you enforce strict policies in your functions, such as readonly and non-nullable.&lt;/p&gt;
&lt;p&gt;I’m only listing built-in utility types that TypeScript offers out-of-the-box. No strings attached. But utility types don’t stop here. There are plenty of &lt;a href=&quot;https://github.com/piotrwitek/utility-types&quot;&gt;libraries&lt;/a&gt; out there, implementing a variety of utilities for every use case.&lt;/p&gt;
&lt;p&gt;Ah, and remember!&lt;/p&gt;
&lt;p&gt;TypeScript is an open source language. Don’t forget to check the implementation of these utility types. To do that, you can right-click on one of them and select “Go to definition”, or something similar, while using your favorite text editor. I know, it’s kinda difficult to read the source code, but it will help you understand how to build your own!&lt;/p&gt;
&lt;p&gt;In fact, in the next articles we will try to do exactly that! We will learn some of the most powerful features of TypeScript, that will help you build your own utility types. I may also curate another list with my favorite third-party utilities.&lt;/p&gt;
&lt;p&gt;Cover Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/xB2XP29gn10&quot;&gt;Javier Miranda&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Conditional types are TypeScript's secret weapon]]></title><description><![CDATA[It's time to learn how to write conditional types in TypeScript, once and forever.]]></description><link>http://www.nicotsou.com/tltr-typescript-conditional-types/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-conditional-types/</guid><pubDate>Mon, 23 May 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;Generics can help you create something that acts like “functions”, but with types. What if I told you that you can use a ternary operator in our type aliases, to write advanced conditional types?&lt;/p&gt;
&lt;p&gt;Do I have your attention? Great! Here we go…&lt;/p&gt;
&lt;p&gt;TypeScript supports &lt;strong&gt;conditional types&lt;/strong&gt;, and this enables endless possibilities. In fact, this is a unique feature that no other programming language supports.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Conditional types&lt;/strong&gt; allow you to add logic to your types. As a result, you can reuse existing types, by reducing the boilerplate of having multiple types for every use case.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You can add conditionals to your types to create powerful utility types, similar to the ones I presented in a previous article.&lt;/p&gt;
&lt;p&gt;In the following sections we will review the &lt;code class=&quot;language-text&quot;&gt;extends&lt;/code&gt; keyword. It’s useful to understand how it works, since it plays a big role in conditional types. We will then write our first conditional type, and I will explain how it works. At the end of the article, we will review a more comprehensive example, to understand how we can use conditional types in our projects.&lt;/p&gt;
&lt;p&gt;To fully understand the following paragraphs, I strongly recommend reading my previous article on the structural type system of TypeScript.&lt;/p&gt;
&lt;p&gt;Cupcake? 🧁&lt;/p&gt;
&lt;h2&gt;The extends keyword&lt;/h2&gt;
&lt;p&gt;If you deal with generic types in your code, you may come across the &lt;code class=&quot;language-text&quot;&gt;extends&lt;/code&gt; keyword. This is basically similar to the &lt;code class=&quot;language-text&quot;&gt;implements&lt;/code&gt; keyword we were using to implement interfaces, but instead of setting a contract, that you have to implement within your class, &lt;code class=&quot;language-text&quot;&gt;extends&lt;/code&gt; keyword is basically a &lt;strong&gt;type constraint&lt;/strong&gt;. It specifically instructs TypeScript to &lt;em&gt;only&lt;/em&gt; allow a limited set of types.&lt;/p&gt;
&lt;p&gt;Here’s a simplified example that illustrates how we use it:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WithId&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playlist&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; WithId&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  items&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;removeFromPlaylist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;idToRemove&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;item&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      item&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id &lt;span class=&quot;token operator&quot;&gt;!==&lt;/span&gt; idToRemove&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// Here we use the id&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The class Playlist&lt;code class=&quot;language-text&quot;&gt;&amp;lt;T extends WithId&gt;&lt;/code&gt; is a &lt;a href=&quot;https://nicotsou.com/tltr-typescript-generics/&quot;&gt;generic class&lt;/a&gt; with a &lt;strong&gt;generic constraint&lt;/strong&gt; on the interface &lt;code class=&quot;language-text&quot;&gt;WithId&lt;/code&gt;. Not all types are acceptable here. It accepts a type &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt; that &lt;em&gt;extends&lt;/em&gt; the &lt;code class=&quot;language-text&quot;&gt;WithId&amp;lt;T&gt;&lt;/code&gt; interface. Practically, this means that type &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt; must contain all the members of the &lt;code class=&quot;language-text&quot;&gt;WithId&lt;/code&gt; interface.&lt;/p&gt;
&lt;p&gt;Why do we need that? Because within our &lt;code class=&quot;language-text&quot;&gt;removeFromPlaylist()&lt;/code&gt; method we are consuming the &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; property of an &lt;code class=&quot;language-text&quot;&gt;item&lt;/code&gt;. If we don’t put this constraint &lt;code class=&quot;language-text&quot;&gt;T extends WithId&lt;/code&gt;, TypeScript will complain because otherwise &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt; can be &lt;em&gt;any&lt;/em&gt; object structure. It may or may not contain an &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; property. Of course we can use a &lt;a href=&quot;https://nicotsou.com/tltr-typescript-narrowing/&quot;&gt;type guard&lt;/a&gt; to check if the id exists in our object, but by limiting the accepted types using the &lt;code class=&quot;language-text&quot;&gt;extends&lt;/code&gt; keyword, we make the whole implementation cleaner and less sensitive to errors.&lt;/p&gt;
&lt;h2&gt;Conditional types&lt;/h2&gt;
&lt;p&gt;Here’s how to write a conditional type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;SomeType &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;OtherType&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; TrueType &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; FalseType&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The syntax is simple, but it may become difficult once you start adding more rules to it. It works like a ternary operator:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;someCondition &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;truthy&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;falsy&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To understand how a conditional type works, consider the following type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FileTypes&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp4&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;pdf&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;zip&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;png&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;gif&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can use a conditional type to extract a list of playable formats, which in our case are &lt;code class=&quot;language-text&quot;&gt;mp3&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;mp4&lt;/code&gt; files:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FilterMediaFileTypes&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp4&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;never&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can use this filter utility type with &lt;code class=&quot;language-text&quot;&gt;FileTypes&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MediaFileTypes&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; FilterMediaFileTypes&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;FileTypes&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; 
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; mediaFile&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; MediaFileTypes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;zip&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// Error: Type &quot;zip&quot; is not assignable to type &quot;mp3&quot; | &quot;mp4&quot;.&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you hover over the &lt;code class=&quot;language-text&quot;&gt;MediaFileTypes&lt;/code&gt; type, you will see that the available types we can assign to it are &lt;code class=&quot;language-text&quot;&gt;mp3&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;mp4&lt;/code&gt;. That’s why TypeScript will throw an error if we try to assign &lt;code class=&quot;language-text&quot;&gt;zip&lt;/code&gt; in the &lt;code class=&quot;language-text&quot;&gt;mediaFile&lt;/code&gt; variable.&lt;/p&gt;
&lt;p&gt;Interesting, right? But how? 😳&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/d01f471734647469d0feacb7bef23eb4/358a9/ball.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 45.62737642585551%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABbUlEQVR42mNgwAHq6+uZQPSK/jL3TTOrS3YsaWld0J4eV+pnxQtVwshADPj/H6Jw1ZQpPFvmVMw8tqH579U9nf+fX1n09/nNtf+3zq9dBpRmIdZQuIIpFSHrF1V6/m/0UfrTFaHzb3WNx9+rO9r+Pr2y/P+Stqw8iOUQXzDg9iYDWEGurZxVurn43ww93n/pWjz/sg2F/1c6yf5fVevz5+XVxf8PrO5aDzHwP34XhjIwMIPoCGU+jxg13v/pOgL/0jX5/mdo8P3PNBL93+av8vfC6rL/5/ZM20NU+NUzQFwYLc9pEaXC8ztJledfIhBnaAEN1RX4X2Ev8efYopz/5/ZOPcRAJAB7wR4Y6LEqPCvT1bn/xyhw/s7T5/9TbCL4u8JO9O+OqSn/18+uTSMqDJFdmaDEqxqrzH0nRZ3nf44Wz98KU8H/Hf5qf2eWBuQjpwaSXJqmwqecpMpbmqzMMaHQXGxSa5RJEHLkYQMAmfaS6vOjYgQAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;ball&quot;
        title=&quot;&quot;
        src=&quot;/static/d01f471734647469d0feacb7bef23eb4/e996b/ball.png&quot;
        srcset=&quot;/static/d01f471734647469d0feacb7bef23eb4/17741/ball.png 263w,
/static/d01f471734647469d0feacb7bef23eb4/52211/ball.png 525w,
/static/d01f471734647469d0feacb7bef23eb4/e996b/ball.png 1050w,
/static/d01f471734647469d0feacb7bef23eb4/087e3/ball.png 1575w,
/static/d01f471734647469d0feacb7bef23eb4/8079d/ball.png 2100w,
/static/d01f471734647469d0feacb7bef23eb4/358a9/ball.png 3950w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;How do conditional types work?&lt;/h2&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;FilterMediaFileTypes&amp;lt;T&gt;&lt;/code&gt; type returns a conditional type. It receives a type &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt;, and it checks whether it extends the union &lt;code class=&quot;language-text&quot;&gt;&apos;mp3&apos; | &apos;mp4&apos;&lt;/code&gt;. In case this is true, it returns the type &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt;. Otherwise it returns &lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt;. Since it is a generic type, it will work with any kind of type, not just &lt;code class=&quot;language-text&quot;&gt;FileTypes&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The magic happens when we actually use this filter, to produce the &lt;code class=&quot;language-text&quot;&gt;MediaFileTypes&lt;/code&gt; type. Let me break down what &lt;em&gt;exactly&lt;/em&gt; is happening:&lt;/p&gt;
&lt;p&gt;As a first step, TypeScript will recursively apply the filter to all the members of FileTypes:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MediaFileTypes&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; FilterMediaFileTypes&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; FilterMediaFileTypes&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;mp4&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; FilterMediaFileTypes&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;pdf&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; FilterMediaFileTypes&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;zip&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; FilterMediaFileTypes&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;png&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
  &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; FilterMediaFileTypes&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;gif&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Right after that, it will evaluate these conditional types:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MediaFileTypes&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp4&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;never&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;never&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;never&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;never&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As we’ve seen, the &lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt; type is not meant to be assigned anywhere. That’s why TypeScript will remove it completely:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MediaFileTypes&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp4&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And that’s how TypeScript evaluated our condition. The last step is to use this type in our &lt;code class=&quot;language-text&quot;&gt;mediaFile&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; mediaFile&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp4&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;zip&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Magic! 🪄&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/55edb8fd0aae277a5fafbe1ef8158fe8/0f7bd/spotify.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 73.38403041825096%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABYlAAAWJQFJUiTwAAADiElEQVR42q2Ty0/cVhSHXTqdIQwz4LF9/R4/8IwHGDIpw6s8QsOjEGhgmMwAKaFRqoBxiCMgTapu2k3aRaWqm0qVqqpSKmWX/6V/RTS7qvvk+tc7LuqiUnddfPI51/bnc8715fR0uqQpvRc9PT2POI6LfPX9qDUZRvszUfRg/ovocPYiOpj5L86i9XorWhrZfPTR2O5ZY/K0zPVlMkcDAzmk02nKhBhW69ifitAeP0Wr9hiH009wb/YZ4+m/eIbDuc/Rmj/GztR92p46we5cEHCpVCbI5weQzWbfMCF1pGF6ZyqkRzcj+v35t/TujVO6VfuMNutHtDn+gO5c0rzk9nhAm9eO3+xOPkRj4d4J19/fz4R59Pb2JhVqAw6+vvscL7/7Bb+/+g3HzTMs2A2sDu9hqdzCit/GcqWNFUY3X2b52ug+3awdYn2yHXKEmEHRLkOSxERoEQ+//vQCL374ET9v3MJXG5/i/Pg5jhtneHzwJYLWExw1L3C4HiJsP8XRzjk+9LfoUqWBursYcj1XrgTpfA75XI5y77AKeQtb81torO/hfmUGB2OLWF84wPb8HXz8wR7WJm5jqbaN6yObWL62jRtjtzBhLdFpZwWj2mTIZVKpYIC1zBcKtDeTgSCocHcXMexNozo0i5JaQ4mMJXhSFd7ldaiLOIqyfBWjZp2OGHVYgheyTWHCwUEUmPC9VAq5HA/r5gRcqwJfH4GjlJMx2KSUYEkey0vJui3/zZDmU1fxofBmyPE8HwwyYba/n2a6u50rwB0cgm9UYao2FMGAKhowFRuW5kKTzARb91BUHKjEhCnb1JAsSLwSciPVaqA6Dt41PZqdWUWfdxXVko+9jR04jguraINIMohIklhTdYgFEYZuQCEKBCJBUVTavc/n+ZAzbTPITddB5jbp9MNvoFzfxHBpCPv7LTguE1oWRFEEIQSGoUOWCZuzAF3XkjVeEkBkmYqCCPb7MaFuBoJpIm/Yb635tVgdrcVFRYlXx2pxqVyKDcOIhYIQsxnHqqrGsqz8E0uCGA8qQiwS6a1QKHSFJ5yiKCdFJpRkQolQgMS+rqkaSqyl7rrJqFarqNVq0DQtgb2ToKkqVFapYZpU1/Vu5SGnEvUT0y7+KWnya9ZCh5XfYS12BBa7rpvg+36nUql0WFUdJuwwWaf7LJN0TMPoeJ73mo3mD7a5B+xwcFmG8z/R9xeQDY6M/IGxzQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Spotify&quot;
        title=&quot;&quot;
        src=&quot;/static/55edb8fd0aae277a5fafbe1ef8158fe8/e996b/spotify.png&quot;
        srcset=&quot;/static/55edb8fd0aae277a5fafbe1ef8158fe8/17741/spotify.png 263w,
/static/55edb8fd0aae277a5fafbe1ef8158fe8/52211/spotify.png 525w,
/static/55edb8fd0aae277a5fafbe1ef8158fe8/e996b/spotify.png 1050w,
/static/55edb8fd0aae277a5fafbe1ef8158fe8/087e3/spotify.png 1575w,
/static/55edb8fd0aae277a5fafbe1ef8158fe8/8079d/spotify.png 2100w,
/static/55edb8fd0aae277a5fafbe1ef8158fe8/0f7bd/spotify.png 2540w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;By the way, if you are looking for music to fill the void while programming, you can &lt;a href=&quot;https://nicotsou.com/music-for-creative-minds/&quot;&gt;check my playlists&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Using conditional types with object structures&lt;/h2&gt;
&lt;p&gt;In a real music player app, we have to deal with various file types. We want to deal with playable media files, for example a &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; function. There are also places when we only want to deal with cover art files, like the part which displays the currently playing track at the bottom of the screen, or the thumbnails within the browser screen. We also want to deal with downloadable files, for example a receipt or a zip file with all user data (GDPR).&lt;/p&gt;
&lt;p&gt;We keep the list with all the file extensions:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FileTypes&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp4&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;pdf&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;zip&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;png&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;gif&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can introduce multiple types to support all the kinds of object structures we have to deal with:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Song&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  filename&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token function-variable function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;VideoClip&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  filename&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp4&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token function-variable function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;CoverArt&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  filename&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;png&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token function-variable function&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Receipt&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  filename&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;pdf&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token function-variable function&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now my goal is to implement that &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; function. Not all of these object structure types are compatible. Only the ones that contain the &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; method can actually be playable. We have built this logic using inheritance in the past. Now, I want to show you a more dynamic way. We will use conditional types, to automate this process and reduce the boilerplate of inheritance.&lt;/p&gt;
&lt;p&gt;First, we need to combine all these file types into one type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;PlayerFile&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Song &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; VideoClip &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; CoverArt &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Receipt&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Awesome! Now let’s write our conditional:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;never&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This generic type &lt;code class=&quot;language-text&quot;&gt;Playable&amp;lt;T&gt;&lt;/code&gt; accepts &lt;em&gt;any&lt;/em&gt; kind of type &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt; and checks if the &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; method is included. If yes, it returns the type &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt;. Otherwise it returns &lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt;. This is the exact same conditional as we had before.&lt;/p&gt;
&lt;p&gt;We are now ready to write our &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; function:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;file&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Playable&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;PlayerFile&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  file&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This function &lt;em&gt;only&lt;/em&gt; accepts playable types. If you hover over the &lt;code class=&quot;language-text&quot;&gt;file&lt;/code&gt; parameter, you will get &lt;code class=&quot;language-text&quot;&gt;Song | VideoClip&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now, let’s declare a &lt;code class=&quot;language-text&quot;&gt;song&lt;/code&gt; and a &lt;code class=&quot;language-text&quot;&gt;receipt&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; song&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Song &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  filename&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;dabadoo&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;playing...&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; receipt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Receipt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  filename&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;dabadoo&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  type&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;pdf&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;download&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;printing...&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can test if our logic works:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;song&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// works fine!&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;receipt&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// Error: Argument of type &apos;Receipt&apos; is not assignable to parameter of type &apos;Song | VideoClip&apos;.&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If we try to call the &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; function with a &lt;code class=&quot;language-text&quot;&gt;song&lt;/code&gt;, it will work without issues. If we try to call it with a &lt;code class=&quot;language-text&quot;&gt;receipt&lt;/code&gt; instead, we will get back an error.&lt;/p&gt;
&lt;p&gt;What a nice feeling of having a programming language working for you? In the end, that’s why we invented them, right?&lt;/p&gt;
&lt;p&gt;We didn’t have to specify any logic to connect these types. We don’t even need to declare these types at all. With &lt;strong&gt;type inference&lt;/strong&gt;, TypeScript can automatically make connections and ensure that the &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; method exists.&lt;/p&gt;
&lt;p&gt;Pure magic 🪄&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;To sum up, conditional types allow you to write if-else statements for your types. This not only reduces the boilerplate of having to declare dependencies across your types and how they are interconnected.&lt;/p&gt;
&lt;p&gt;Understanding conditional types is the missing piece of the puzzle to understanding TypeScript. Now you can basically read almost every TypeScript code. It’s not a feature you will use daily in your code, for sure. But there will be cases when you will find it extremely useful.&lt;/p&gt;
&lt;p&gt;It also unlocks a large set of language features which would have taken a lot of customizations to be built. TypeScript development team was able to satisfy a lot of feature requests, by writing reusable generic types that leverage conditional types.&lt;/p&gt;
&lt;p&gt;Stay tuned for more. 😉&lt;/p&gt;
&lt;p&gt;Acknowledgements: I have to give credit to &lt;a href=&quot;https://github.com/ds300&quot;&gt;David Sheldrick&lt;/a&gt; and his awesome &lt;a href=&quot;https://artsy.github.io/blog/2018/11/21/conditional-types-in-typescript/&quot;&gt;article&lt;/a&gt; about Conditional types. It really helped me put together this article.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/2/conditional-types.html&quot;&gt;official documentation&lt;/a&gt; has also nice examples to learn from.&lt;/p&gt;
&lt;p&gt;Cover photo credit: &lt;a href=&quot;https://unsplash.com/photos/xRRQlR8Qu-Y&quot;&gt;Li Zhang&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[TypeScript doesn't care about your class names]]></title><description><![CDATA[What differentiates TypeScript from all the other programming languages? How its types are related?]]></description><link>http://www.nicotsou.com/tltr-typescript-structural-type-system/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-structural-type-system/</guid><pubDate>Sat, 14 May 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;TypeScript has a &lt;strong&gt;structural type system&lt;/strong&gt;. This means that the language does not depend on the origin of a type, when deciding if it’s compatible with another one, but it compares their &lt;em&gt;members&lt;/em&gt;. If all the members of a type exist in another type with their appropriate types, then the two types are &lt;em&gt;compatible&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;In this article we will observe this behavior to understand it better. I will then explain the top level types that we have in TypeScript and how they are connected. We will also review the case of nullables, to understand how they relate to the other types.&lt;/p&gt;
&lt;p&gt;Gianduja? 🍫&lt;/p&gt;
&lt;h2&gt;A practical example&lt;/h2&gt;
&lt;p&gt;We are used to the idea that an &lt;strong&gt;object&lt;/strong&gt; is created from a &lt;strong&gt;class&lt;/strong&gt;. And that’s true with TypeScript as well; you can use classes to create objects. But objects in TypeScript are &lt;em&gt;not&lt;/em&gt; bound to their classes.&lt;/p&gt;
&lt;p&gt;Consider the following example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Dog&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nobody&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Cat&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nobody&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;These types do &lt;em&gt;not&lt;/em&gt; derive from any kind of parent class, like &lt;code class=&quot;language-text&quot;&gt;Animal&lt;/code&gt; for example. And yet they are interchangeable:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; cat &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Cat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; dog &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; animal&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Dog &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cat &lt;span class=&quot;token comment&quot;&gt;// it works&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Observe what’s happening when I assign a &lt;code class=&quot;language-text&quot;&gt;cat&lt;/code&gt; to a &lt;code class=&quot;language-text&quot;&gt;dog&lt;/code&gt;. No errors whatsoever. Both &lt;code class=&quot;language-text&quot;&gt;Cat&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;Dog&lt;/code&gt; have a &lt;code class=&quot;language-text&quot;&gt;name&lt;/code&gt; property of type &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;. The structural contract is fulfilled.&lt;/p&gt;
&lt;p&gt;To spice things up, let’s introduce an &lt;code class=&quot;language-text&quot;&gt;Animal&lt;/code&gt; interface:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Animal&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Our &lt;code class=&quot;language-text&quot;&gt;Animal&lt;/code&gt; interface is &lt;em&gt;still&lt;/em&gt; not connected to our classes. To differentiate between the two classes, let’s add a &lt;code class=&quot;language-text&quot;&gt;bark()&lt;/code&gt; method to the &lt;code class=&quot;language-text&quot;&gt;Dog&lt;/code&gt; class:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Cat&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nobody&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Dog&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nobody&apos;&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;bark&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;bark&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you may have guessed, our &lt;code class=&quot;language-text&quot;&gt;cat&lt;/code&gt; can be assigned without issues to an &lt;code class=&quot;language-text&quot;&gt;Animal&lt;/code&gt; object:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; cat &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Cat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; animal&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Animal &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cat&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// it works&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What about the &lt;code class=&quot;language-text&quot;&gt;Dog&lt;/code&gt;? Well, this will &lt;em&gt;also&lt;/em&gt; work just fine:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; dog &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; animal&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Animal &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dog&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// it also works&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Both &lt;code class=&quot;language-text&quot;&gt;Cat&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;Dog&lt;/code&gt; have the property &lt;code class=&quot;language-text&quot;&gt;name: string&lt;/code&gt; in common. That makes them compatible with &lt;code class=&quot;language-text&quot;&gt;Animal&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This doesn’t work both ways, because &lt;code class=&quot;language-text&quot;&gt;Dog&lt;/code&gt; has an additional method that &lt;code class=&quot;language-text&quot;&gt;Animal&lt;/code&gt; doesn’t support. If you try to assign an &lt;code class=&quot;language-text&quot;&gt;animal&lt;/code&gt; to a &lt;code class=&quot;language-text&quot;&gt;dog&lt;/code&gt;, you will get back an error:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// create an animal object&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; animal&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Animal &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Apple&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// Asign it to a Dog class&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt;&lt;/span&gt; dog&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Dog &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; animal &lt;span class=&quot;token comment&quot;&gt;// Property &apos;bark&apos; is missing in type &apos;Animal&apos; but required in type &apos;Dog&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We say that &lt;code class=&quot;language-text&quot;&gt;Dog extends Animal&lt;/code&gt;, because &lt;code class=&quot;language-text&quot;&gt;Dog&lt;/code&gt; contains &lt;em&gt;all&lt;/em&gt; the members that &lt;code class=&quot;language-text&quot;&gt;Animal&lt;/code&gt; contains, and maybe some more. But the opposite, as we’ve seen, is not true.&lt;/p&gt;
&lt;p&gt;I hope this gives you an overview of how a &lt;strong&gt;structural type system&lt;/strong&gt; works in practice. It’s completely different from a &lt;strong&gt;nominal type system&lt;/strong&gt;, in which the language is strictly checking for the &lt;em&gt;origin&lt;/em&gt; of an object. In languages like Java for example, the examples above would not work, because &lt;code class=&quot;language-text&quot;&gt;Cat&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;Dog&lt;/code&gt; are completely unrelated classes, regardless of if they are similar.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Now a small reminder; If you want to check if an object is related to another one, you have to use the &lt;code class=&quot;language-text&quot;&gt;instanceof&lt;/code&gt; operator:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;dog &lt;span class=&quot;token keyword&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Cat&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// false&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We are basically checking if the &lt;code class=&quot;language-text&quot;&gt;dog&lt;/code&gt; has the &lt;code class=&quot;language-text&quot;&gt;cat&lt;/code&gt; in its prototype chain. It certainly doesn’t.&lt;/p&gt;
&lt;p&gt;So why is TypeScript that strange? 🙋‍♂️&lt;/p&gt;
&lt;h2&gt;Why is structural typing useful?&lt;/h2&gt;
&lt;p&gt;The reason this feature exists is to make the language backwards compatible with JavaScript applications. JavaScript is a dynamic language, and it never had strict types. TypeScript adds only a minimum set of rules, to enforce strict type checks in your code, by preserving the mechanics of the JavaScript language. This not only makes it backwards compatible, but it also requires less effort from your side to integrate it into your projects.&lt;/p&gt;
&lt;p&gt;That’s how TypeScript won the race for strict typing languages. There were plenty of them out there, that were compiling to plain JavaScript. But nobody wants to write everything from scratch. Nobody wants to learn a new language. Such a shame, and yet a reality.&lt;/p&gt;
&lt;p&gt;Existing JavaScript code will work as is in TypeScript, &lt;em&gt;without&lt;/em&gt; any refactoring. That’s why structural types are useful.&lt;/p&gt;
&lt;h2&gt;A journey to TypeScript’s type universe&lt;/h2&gt;
&lt;p&gt;If we have to visualize the previous example, we will end up with the diagram below. Every bubble represents a type. As we’ve seen, types can be classes, type aliases, interfaces, or a union which is a combination of types.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/b28225e4e337351858b150184441ba3d/29114/Typescript_top_level_types.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAABlElEQVR42o1Sy1LCQBAkgjwUCIj4wAfoEcuLlhcvXPmBPRgImyy4kqSChIjBU77B3/An4zQxejEUW7W1r9menu7JZFKGlHKX2WFZs6y6YSwuHGfZcpxVQ4hlxfO8EoUomW0GY19Zm4CmU/9I150G568dABJQS8r5qWm6TSkXh3hnLMxvBAvDcIexQNW0VZ2AalhN86M5HnsHQjitwcCtxkBeCczjPcumAnIeFgGCsphtl3E2zfm1EH4bIGCGpNx+O0E8ktkUl1r+cOjvA0yzVnVoGJ/99mTyfoN7MEQcmK2BqQrMVIau61alDPcYkyrYEag6mfhXoxfvzJDLO0N6t+Px7NiihIh/IsZImoanCCEq0At7GKLrs0uYAfGF7bc5DzrQOGEHHe+pEpxTNaRZkzJQIfZoRMyMxaPx7D9ABs55UQaBCqdRfhRFCqTZ0DIsjwB8ZMQUH2EKaXhuWbHzeOv1ermfrsilsvsDhZu/WRVrbdDsEv2ZxCTMALhFa0dKv/9Z6HZjtmv9yGEyrJAki+//779vYP6sGfUOoq4AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Typescript top level types.png&quot;
        title=&quot;&quot;
        src=&quot;/static/b28225e4e337351858b150184441ba3d/e996b/Typescript_top_level_types.png&quot;
        srcset=&quot;/static/b28225e4e337351858b150184441ba3d/17741/Typescript_top_level_types.png 263w,
/static/b28225e4e337351858b150184441ba3d/52211/Typescript_top_level_types.png 525w,
/static/b28225e4e337351858b150184441ba3d/e996b/Typescript_top_level_types.png 1050w,
/static/b28225e4e337351858b150184441ba3d/087e3/Typescript_top_level_types.png 1575w,
/static/b28225e4e337351858b150184441ba3d/29114/Typescript_top_level_types.png 1920w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;Dog&lt;/code&gt; type can be assigned to both the &lt;code class=&quot;language-text&quot;&gt;Cat&lt;/code&gt; and the &lt;code class=&quot;language-text&quot;&gt;Animal&lt;/code&gt; types. That logically doesn’t make sense, because the &lt;code class=&quot;language-text&quot;&gt;Animal&lt;/code&gt; is the logical parent of these two types. That’s the tricky part you need to understand with TypeScript. The same applies with the &lt;code class=&quot;language-text&quot;&gt;object&lt;/code&gt; type, which can accept any object structure. At the very top we have &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt;, which can accept all the types.&lt;/p&gt;
&lt;p&gt;The structural typing system doesn’t affect only object structures, but other kinds of types as well. The situation gets more complicated when you create unions, which can combine &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt; types. I will explain how those are connected in the next articles.&lt;/p&gt;
&lt;p&gt;For a birds-eye view, here are the top level types that are available in TypeScript:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We have &lt;em&gt;immutable&lt;/em&gt; primitive types: &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;, and &lt;code class=&quot;language-text&quot;&gt;boolean&lt;/code&gt;, and &lt;code class=&quot;language-text&quot;&gt;Symbol&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;And we have reference types which can all be represented by the type &lt;code class=&quot;language-text&quot;&gt;object&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/e4a28dacb0b5cb87b27654b48605ab9b/29114/Typescript-top-level-types.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAABvklEQVR42pVS7UrDMBRddX5/DJ3OIVNQ/EARFEVwgtSfe4H82VrSJlncWuvsss6JP/IcvoIvWXM2B9NNwQuXJk1y7jn3nkxmclhfObLXU1horaeQaZpahHxMZ/4KQgguWPgioyiak1IuOo5asm07Symd5zxeN4BZnFOq57/e/BbpkJnFWLLi+719IdSe7z/vM8bM/nWTsU7ZFMnhNgoBEMW+wUAC0XoWh9Vqc61W06uExAv1urqmonXh8PiAhck22FWrvTXcU0otuW4vz/nTFlSMcQMoDj2vvSPl064QUQnSpHyfwd5/UKdevXvriEfDONr0vOTQMCwwps6kfNn90fOBXDwyEq8gizWSYwD2Qb3kkvNOxWWtshv28m4Y5v16YjPZKnPePQEwCH2DM7QNk/aG05cS5ikNivgXBMEyHg1Yt4+85ptRoBejSK8GgV4mROYqlWhuTC4STfZEcidEfOM/xOfYgzXl6t4M6RgDMiBFs94ekoB9bFtnJ1oGk6W0UwRT05ccvIdBCNEt1WrdEpg3GqoAu4AAADHhiYDDHqDicA0f9h1AyCxkYvJ936XpqPGtzD/DSkcAxpr/Iz4B+fa6xgKa4iIAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Typescript-top-level-types.png&quot;
        title=&quot;&quot;
        src=&quot;/static/e4a28dacb0b5cb87b27654b48605ab9b/e996b/Typescript-top-level-types.png&quot;
        srcset=&quot;/static/e4a28dacb0b5cb87b27654b48605ab9b/17741/Typescript-top-level-types.png 263w,
/static/e4a28dacb0b5cb87b27654b48605ab9b/52211/Typescript-top-level-types.png 525w,
/static/e4a28dacb0b5cb87b27654b48605ab9b/e996b/Typescript-top-level-types.png 1050w,
/static/e4a28dacb0b5cb87b27654b48605ab9b/087e3/Typescript-top-level-types.png 1575w,
/static/e4a28dacb0b5cb87b27654b48605ab9b/29114/Typescript-top-level-types.png 1920w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;As you can see, the bubbles are completely independent here. That’s because they are not compatible with each other. You can’t assign an &lt;code class=&quot;language-text&quot;&gt;object&lt;/code&gt; to a &lt;code class=&quot;language-text&quot;&gt;boolean&lt;/code&gt;, for example.&lt;/p&gt;
&lt;p&gt;Now, let’s zoom in, to see which values are compatible for each of them. Note that a &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt; type can also include &lt;code class=&quot;language-text&quot;&gt;NaN&lt;/code&gt;. An &lt;code class=&quot;language-text&quot;&gt;object&lt;/code&gt; type can include any object declaration &lt;code class=&quot;language-text&quot;&gt;{}&lt;/code&gt;, arrays, regular expressions, dates, and the list goes on:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// the following asignments will work fine&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; aNumber&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;NaN&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; generalObject&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; object &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
generalObject &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token regex&quot;&gt;&lt;span class=&quot;token regex-delimiter&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token regex-source language-regex&quot;&gt;some string&lt;/span&gt;&lt;span class=&quot;token regex-delimiter&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;token regex-flags&quot;&gt;i&lt;/span&gt;&lt;/span&gt;
generalObject &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Next to these, TypeScript adds some special wildcard types:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt;: the parent of all TypeScript types. You instruct TypeScript that you don’t know what a type can be, but it’s ok. You give your permission to TypeScript. You’re saying “I know what I’m doing here”. Most of the cases you simply don’t.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;unknown&lt;/code&gt;: This is again the parent of all types, with a small catch. You instruct TypeScript that you don’t know what the type can be, but you want to protect yourself from unnecessary mistakes.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt;: This is the opposite of the previous ones. It basically means no type must be assigned to that thing, no matter what. Think of it as the value &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt;. It is useless as a value, but it plays a big role in the logic of your application.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/83820b32d154be000323339016d331eb/29114/Typescript-top-level-types-with-values.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 93.5361216730038%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAATCAYAAACQjC21AAAACXBIWXMAAAsTAAALEwEAmpwYAAADfklEQVR42n1UTWgbRxReybJlRz+WLFuWLKWy5cgxoZbd2inFmCxp7FbEJnYIAyGSs7JlVtpdj3ZXq7UUX+ZSKE3pwZCLoJBTafG5p2LwoSGnHtISSG6FHtLmEAxxDr1N34stEMTrgYHV6M33vve9b54gdCzGmFtkzAMfbrkld0sS64VjV3sTQro6Yj0YL5y38IIusdAvN4z03m2aJPTBkCw3+tv/ybJ8YeGbZnilVhs05N04APbAWbcj4FLB8j0SVT8fKF/5O1NO4BndQFB2ATdpNPpfTShX347S1eez1Y+WLCvKTqo4eylKM4xl3jLsuTt0Z56YuwlCzL4tupPUdRbCGD6kxvigNsFRAtPsK5Wqw6fXXR8A2pt2IGtZvj+njPTROL3+b0ZJ5ykNlkoPhimlXl3SQ3er9ezk17txZIsangvIROYRVeb/SdRHDz7f/uznperHEoDkAIyt6qFn2Uq09ZUR/2NKv3awUM0eQnyhYkUdAQHS3ZJb3WvNZkRS6zFtvRnJMRq8Ab/rpB57Ol1OPMsWfMcp5fqbceXimqZFVFX1O2qIlsESeYr18qiSheYkeNLsezmxPWbkjTgwDa6ClrO/t7pFXQ9ZIM+5Xd607cDrjDrDhytRnpJ6/8lavr9S0ISwMv8QGK4w7HZtUt405jA+l6NeQva7HAFRLy6QrnLRztzdtr64L1vXaJ4F39sHLmtaM8II6SnfNxP5PA2iN0/MzV1nAp6a2NUgcv/+ojmFCY5SkGSEfvJk3g5sgNEFztyF7ywffq+DhiI0xpEhhazCY9AvqF4Cr60waBL4zc0BVCnWLub2aPBdSp05TmpTGL//pTmgSvWYIyC+TwIs2UptkIsnbxXNfrNej4FHR/jw9hj3bwxxQfTwuDzJB5RFtJhweDZLF3YMdcnLRrwo25+Wy+Y0k6RelOKxCMwj2sj38HKWTTNRVKyrD4kaA8MH8Y07MXTj3traSZZK9awkNUYxyT1g+eKyHUB2Py4rYbtYzfwqanMC5y7lXjNMCOtxBESAk4GwG0dQsMngpT3q/W3BSP+XqGbABT2HWHKynMDn155GDou7cDggEO4txZrdBO0wCTTEO//Dt4F1sI4EphYfMT+le97OGfkhHJSAjSmVzIFCpRKVgQFOm1MpPKgnlodmbo+t8wBdbaOeTmMPMns/wTvk6Ig/c1r/Dy7mWCcg5WCOAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Typescript-top-level-types-with-values.png&quot;
        title=&quot;&quot;
        src=&quot;/static/83820b32d154be000323339016d331eb/e996b/Typescript-top-level-types-with-values.png&quot;
        srcset=&quot;/static/83820b32d154be000323339016d331eb/17741/Typescript-top-level-types-with-values.png 263w,
/static/83820b32d154be000323339016d331eb/52211/Typescript-top-level-types-with-values.png 525w,
/static/83820b32d154be000323339016d331eb/e996b/Typescript-top-level-types-with-values.png 1050w,
/static/83820b32d154be000323339016d331eb/087e3/Typescript-top-level-types-with-values.png 1575w,
/static/83820b32d154be000323339016d331eb/29114/Typescript-top-level-types-with-values.png 1920w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Of course, we can create our own custom types:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// we have a custom type&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;OneTwoThree&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// we create a variable&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; aValue&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; OneTwoThree &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// since all of its values are numbers, it is assignable to a number&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; anyNumber&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; aValue &lt;span class=&quot;token comment&quot;&gt;// it works fine&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// and of course we can assign it to a type of any&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; anything&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;any&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; aValue &lt;span class=&quot;token comment&quot;&gt;// it works fine&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the previous example, our type alias &lt;code class=&quot;language-text&quot;&gt;OneTwoThree&lt;/code&gt; can contain specific numbers. This immediately makes it compatible with the &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt; type. You can’t assign any number that you want to the &lt;code class=&quot;language-text&quot;&gt;aValue&lt;/code&gt; variable, but you can assign the &lt;code class=&quot;language-text&quot;&gt;aValue&lt;/code&gt; variable to &lt;code class=&quot;language-text&quot;&gt;anyNumber&lt;/code&gt;. In the same way, you can assign &lt;code class=&quot;language-text&quot;&gt;anyNumber&lt;/code&gt; to &lt;code class=&quot;language-text&quot;&gt;anything&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If you want to learn more details about the available types of TypeScript, I have &lt;a href=&quot;https://nicotsou.com/tltr-typescript-types/&quot;&gt;another article&lt;/a&gt; which explains everything you need to know about them.&lt;/p&gt;
&lt;h2&gt;Nullable primitive values&lt;/h2&gt;
&lt;p&gt;In JavaScript, we have the following &lt;strong&gt;nullable&lt;/strong&gt; &lt;strong&gt;primitive &lt;em&gt;values&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;: When no value is assigned.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt;: When we specifically assign a null value to an object.&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;
&lt;p&gt;Don’t be confused, nullable types are not valid types in TypeScript, but &lt;em&gt;values&lt;/em&gt; that can be assigned to variables.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Yes, I hear you. You can set a variable to &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; alwaysUndefined&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; alwaysNull&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Error: Type undefined is not assignable to type null.&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the previous examples, we have declared the variables &lt;code class=&quot;language-text&quot;&gt;alwaysUndefined&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;alwaysNull&lt;/code&gt;, which respectively can only accept &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt; as allowed values. Here we are basically instructing TypeScript, to use the &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt; values similar to how we use a string value &lt;code class=&quot;language-text&quot;&gt;“green”&lt;/code&gt; or a number value &lt;code class=&quot;language-text&quot;&gt;123&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; alwaysGreen&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;green&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;green&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Always123&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;123&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Kinda boring, isn’t it? 😑&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Now I want to make a small note here; TypeScript will &lt;em&gt;not&lt;/em&gt; allow assigning &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt; values to a variable by default. That’s &lt;em&gt;only&lt;/em&gt; possible when you have specifically disabled &lt;code class=&quot;language-text&quot;&gt;strictNullChecks&lt;/code&gt; in the compiler configuration file, and you have set the &lt;code class=&quot;language-text&quot;&gt;strict&lt;/code&gt; mode set to off.&lt;/p&gt;
&lt;p&gt;By default, &lt;code class=&quot;language-text&quot;&gt;strict&lt;/code&gt; mode is on, which sets all the strict flags to true. If you want to write robust code, you should definitely avoid having nullable variables.&lt;/p&gt;
&lt;p&gt;The following will not work, if we have &lt;code class=&quot;language-text&quot;&gt;strict&lt;/code&gt; mode enabled, which is the default option:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; space&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; object &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Type null is not assignable to type object.&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This was a useful introduction that will help you create a mind map to have a holistic view on types in TypeScript. In my next posts, I will cover more ways you can dynamically generate type combinations. Until then, stay curious. 🤓&lt;/p&gt;
&lt;p&gt;Cover Credit: &lt;a href=&quot;https://unsplash.com/photos/r-O95aZ6wvI&quot;&gt;Mehdi MeSSrro&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[TLTR; TypeScript Narrowing]]></title><description><![CDATA[How do we narrow down a type to more specific ones? Everything you need to know.]]></description><link>http://www.nicotsou.com/tltr-typescript-narrowing/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-narrowing/</guid><pubDate>Tue, 15 Mar 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;So you have an unknown parameter in your function. Is it a string? Is it a boolean? Is it some kind of weird combination of properties and methods? How can you tell?&lt;/p&gt;
&lt;p&gt;In this article we are going to address one of the most important topics; How do we narrow down a type to more specific ones? What options the JavaScript language has built-in and what are the features TypeScript adds to protect us from common mistakes?&lt;/p&gt;
&lt;p&gt;If you don’t have a lot of experience with JavaScript, this article will be a gold mine for you. I tried to collect as much information as I could, to make you understand the important bits and pieces of my most favorite language, that usually drive people crazy and discourage them completely.&lt;/p&gt;
&lt;p&gt;Enough with the intro. Let’s start with a coding challenge. Hooray! 🤗&lt;/p&gt;
&lt;p&gt;Consider the following JavaScript function &lt;code class=&quot;language-text&quot;&gt;getWidth()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getWidth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;width&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; width &lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;number&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;width&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; width&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This basically returns values for the css &lt;code class=&quot;language-text&quot;&gt;width&lt;/code&gt; property. The logic is the following:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// number values get the suffix px:&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;getWidth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// &apos;100px&apos;&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// string values get no suffix:&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;getWidth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;100%&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// &apos;100%&apos;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;getWidth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;auto&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// &apos;auto&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Can you guess what the type of the argument &lt;code class=&quot;language-text&quot;&gt;width&lt;/code&gt; is in every single line of the code above? Do you think this code will work as expected?&lt;/p&gt;
&lt;p&gt;I am going to reveal the solution in the next paragraph. Spend a minute thinking about it before you continue.&lt;/p&gt;
&lt;p&gt;Chocolate? 🍫&lt;/p&gt;
&lt;h2&gt;The Control Flow Analysis of TypeScript&lt;/h2&gt;
&lt;p&gt;To solve the mystery, let’s fire up our favorite TypeScript text editor and paste the challenge there. Try to hover over the &lt;code class=&quot;language-text&quot;&gt;width&lt;/code&gt; argument in every single line. As you do that, you will notice that TypeScript will give you a special union type with all the types.&lt;/p&gt;
&lt;p&gt;Here’s what we have:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getWidth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;width&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; string &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; width &lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;number&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// string | number | undefined&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;width&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// number&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; width&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// string | undefined&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that within the &lt;code class=&quot;language-text&quot;&gt;if&lt;/code&gt; block, the type of the argument &lt;code class=&quot;language-text&quot;&gt;width&lt;/code&gt; is a number. That’s logical, since we are checking if the &lt;code class=&quot;language-text&quot;&gt;width&lt;/code&gt; parameter is a number within the &lt;code class=&quot;language-text&quot;&gt;if&lt;/code&gt; conditional. But how does TypeScript know what it will be? Has it compiled the code already?&lt;/p&gt;
&lt;p&gt;It turns out that it does. 😏&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;TypeScript evaluates your code at compilation time, to detect how the type of a value changes at any given position.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Combine this with type inference and you get pretty sophisticated error detections.&lt;/p&gt;
&lt;p&gt;Now, back to our example. If we hover over the &lt;code class=&quot;language-text&quot;&gt;width&lt;/code&gt; argument in the last &lt;code class=&quot;language-text&quot;&gt;return&lt;/code&gt; statement, we will see that the possible values are &lt;code class=&quot;language-text&quot;&gt;string | undefined&lt;/code&gt;. That’s logical, because if the &lt;code class=&quot;language-text&quot;&gt;width&lt;/code&gt; argument was a number, the execution would have stopped right inside the &lt;code class=&quot;language-text&quot;&gt;if&lt;/code&gt; block. TypeScript understands that, and removes the &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt; type from the union. But what about the &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;? Will this be a problem?&lt;/p&gt;
&lt;p&gt;Oops, I did it again! 😬&lt;/p&gt;
&lt;p&gt;Of course, &lt;code class=&quot;language-text&quot;&gt;width&lt;/code&gt; can be &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;, because it’s an optional parameter. This means that you will not get the best results if you call this function with no arguments:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token function&quot;&gt;getWidth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// returns &apos;undefinedpx&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With the help of TypeScript, we know that our function needs some refactoring:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getWidth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;width&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; width &lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;number&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;width&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; width &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;auto&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, no matter what, the function will always return a value or it will default to &lt;code class=&quot;language-text&quot;&gt;auto&lt;/code&gt;. TypeScript helped me identify the bug here.&lt;/p&gt;
&lt;p&gt;Now, you may be wondering, do I need to hover over my types and check all these things? Why didn’t TypeScript complain about this? 😒&lt;/p&gt;
&lt;p&gt;I could have prevented this from happening, if I had specified a type for the return value:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getWidth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;width&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; width &lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;number&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;width&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; width&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// Error: Type undefined is not assignable to type string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now I get back a very informative error:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/45c4799e9834b106f5a2939b0de61e83/e9fa0/typescript-not-assignable.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 30.798479087452467%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAABYlAAAWJQFJUiTwAAABXklEQVR42jWR227aUBBFfYcGgnXwsbFNfGxsblGAAiZR1FTqS1opDzQtKST//yOrB6M8LK0tjbQ1ozGK63+UwQnReUKpHbl6pN9fcjO8R4g7kniLlCvUcMFIrQjEDMcZ0fLKBs875wrXUwjnCeNcWMl3pP9IUtzRDabIeIYIJ0TpLTKZY3kRTvuC5UoMW+OEjU1t09bZFfjOgy7sHamCI0H3nmy8IUwW5GVNPFw2VqMaIafENysSTV7uKKodQTQnK7b0wzlptsaXJb6tC/POgbH8oGstSeMJYTgjLb6ST2oGmT451+eXa7JqQ6KWzcZxtkBEUz1f4IdjeoEmKhCtbxilf2Icnkh7e/LohW37B+X1d7LBM6PBT1T0zDh9wW890DbXXFlbzYaOVV9s15dsrwmdXxjZ1UE/5Q3VeiP/cuTW/cvUPRAZvzV7BpqzE/OV1PxDYnz6k9fGqSY29vwHHRCiGt5Ed1kAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-not-assignable.png&quot;
        title=&quot;&quot;
        src=&quot;/static/45c4799e9834b106f5a2939b0de61e83/e996b/typescript-not-assignable.png&quot;
        srcset=&quot;/static/45c4799e9834b106f5a2939b0de61e83/17741/typescript-not-assignable.png 263w,
/static/45c4799e9834b106f5a2939b0de61e83/52211/typescript-not-assignable.png 525w,
/static/45c4799e9834b106f5a2939b0de61e83/e996b/typescript-not-assignable.png 1050w,
/static/45c4799e9834b106f5a2939b0de61e83/087e3/typescript-not-assignable.png 1575w,
/static/45c4799e9834b106f5a2939b0de61e83/8079d/typescript-not-assignable.png 2100w,
/static/45c4799e9834b106f5a2939b0de61e83/e9fa0/typescript-not-assignable.png 2292w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;That’s the reason we really have to encourage our team colleagues to be very descriptive with types.&lt;/p&gt;
&lt;p&gt;One last thing; If somebody tries to pass &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt; instead of &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;, TypeScript will throw an error:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token function&quot;&gt;getWidth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Error: Argument of type &apos;null&apos; is not assignable to parameter of type &apos;string | number | undefined&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It got me covered! 😋&lt;/p&gt;
&lt;p&gt;So TypeScript is evaluating your code at compile-time and it can predict what type a value can have. Combine this with the &lt;em&gt;type inference&lt;/em&gt; feature and you get a very sophisticated bug prevention system.&lt;/p&gt;
&lt;p&gt;We’ve seen this behavior with the &lt;code class=&quot;language-text&quot;&gt;unknown&lt;/code&gt; keyword:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;someplay&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;files&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;unknown&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  files&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;t&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;t&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Error: Object is of type &apos;unknown&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, TypeScript prevented us from calling the method &lt;code class=&quot;language-text&quot;&gt;map()&lt;/code&gt; that may not exist in the files argument. In fact, &lt;code class=&quot;language-text&quot;&gt;files&lt;/code&gt; may not be an array at all.&lt;/p&gt;
&lt;p&gt;Control flow analysis is being improved with every version of the language. More and more features are being added.&lt;/p&gt;
&lt;h2&gt;Type Guards&lt;/h2&gt;
&lt;p&gt;Here’s how you can check if the &lt;code class=&quot;language-text&quot;&gt;files&lt;/code&gt; argument of the previous example is an array:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;someplay&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;files&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;unknown&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;files &lt;span class=&quot;token keyword&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;&lt;span class=&quot;token builtin&quot;&gt;Array&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    files&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;t&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;t&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I used a condition that checks if the &lt;code class=&quot;language-text&quot;&gt;files&lt;/code&gt; variable is an array. Within the &lt;code class=&quot;language-text&quot;&gt;if&lt;/code&gt; block, we know for sure that the type of the &lt;code class=&quot;language-text&quot;&gt;files&lt;/code&gt; variable is an array, and therefore we can call its &lt;code class=&quot;language-text&quot;&gt;map()&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;For a TypeScript developer, it’s important to understand how to narrow down types to more specific ones. These kinds of checks are called &lt;strong&gt;type guards&lt;/strong&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;A &lt;em&gt;type guard&lt;/em&gt; is a form of a code expression, which helps TypeScript refine types down to more specific types.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Let’s review all the tools JavaScript has already built-in. Yes, all these will work at runtime.&lt;/p&gt;
&lt;h3&gt;The &lt;code class=&quot;language-text&quot;&gt;typeof&lt;/code&gt; operator and its misbehavior&lt;/h3&gt;
&lt;p&gt;You are probably familiar with the &lt;code class=&quot;language-text&quot;&gt;typeof&lt;/code&gt; operator in JavaScript:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Checking the type of a value&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;something&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &apos;string&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2022&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &apos;number&apos;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Checking the type of a variable&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b
&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; add &lt;span class=&quot;token comment&quot;&gt;// &apos;function&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It always returns a string and the possible return values, according to &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#description&quot;&gt;MDN&lt;/a&gt;, are the following: &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;object&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;boolean&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;bigint&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;symbol&lt;/code&gt;, and &lt;code class=&quot;language-text&quot;&gt;function&lt;/code&gt;. For all the other values that are not in this list, &lt;code class=&quot;language-text&quot;&gt;typeof&lt;/code&gt; will return &lt;code class=&quot;language-text&quot;&gt;&quot;object&quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Can we really depend on &lt;code class=&quot;language-text&quot;&gt;typeof&lt;/code&gt; to check all kinds of values? No we can’t. It’s very limited and kinda confusing. For example, if you try to get the type of an array, here’s what you will get back:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &apos;object&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Of course, arrays in JavaScript &lt;em&gt;are&lt;/em&gt; indeed objects. But someone will argue that functions are &lt;em&gt;too&lt;/em&gt;. So why do we get a return value for functions and not for arrays, which are also really common in our daily code?&lt;/p&gt;
&lt;p&gt;The results of &lt;code class=&quot;language-text&quot;&gt;typeof&lt;/code&gt; are not only incomplete, but they are &lt;em&gt;buggy&lt;/em&gt; too. You may probably know that &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt; is also an object:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// returns &apos;object&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That didn’t go very well. 🤪&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Instead of having the programming language work for us, we have to do the thinking here.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Thankfully, TypeScript knows about such misbehaviors and it can protect us from unnecessary bugs.&lt;/p&gt;
&lt;h3&gt;The &lt;code class=&quot;language-text&quot;&gt;instanceof&lt;/code&gt; operator&lt;/h3&gt;
&lt;p&gt;We can check if a specific object is an instance of another object:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;values&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;values &lt;span class=&quot;token keyword&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;&lt;span class=&quot;token builtin&quot;&gt;Array&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; values&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;previousValue&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; currentValue&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; previousValue &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; currentValue&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; values
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Remember that in JavaScript we have prototypical inheritance. Every object has a prototype, and it derives all its logic. This works like a chain. The parent object of all objects is the actual &lt;code class=&quot;language-text&quot;&gt;Object&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In the example above, we are counting the numbers by reducing an array of values. If we get a single number instead of an array, we are returning the number itself. We check if the &lt;code class=&quot;language-text&quot;&gt;values&lt;/code&gt; argument is an instance of the &lt;code class=&quot;language-text&quot;&gt;Array&lt;/code&gt; object. If you don’t check this before calling the reduce method, TypeScript will throw an error, because a number doesn’t implement this method.&lt;/p&gt;
&lt;h3&gt;Searching for the truth&lt;/h3&gt;
&lt;p&gt;I bet you are also familiar with the &lt;code class=&quot;language-text&quot;&gt;if&lt;/code&gt; statements, so I’m not going to elaborate more on this topic.&lt;/p&gt;
&lt;p&gt;Maybe you are not familiar with the fact that JavaScript can accept &lt;em&gt;any&lt;/em&gt; kind of value in a conditional:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3.14&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// oops, that&apos;s not a boolean, but is it truthy?&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;the value is truthy&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What JavaScript is doing here, is to coerce the &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt; to a &lt;code class=&quot;language-text&quot;&gt;boolean&lt;/code&gt;, and then to evaluate the condition:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3.14&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;the value is truthy&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You may be wondering how JavaScript decides which values are &lt;em&gt;truthy&lt;/em&gt; or &lt;em&gt;falsy&lt;/em&gt;? That’s one of the trickiest quirks of the language.&lt;/p&gt;
&lt;p&gt;Prepare to be amazed! 😋&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Falsy values&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When you compare values, the following are &lt;em&gt;always&lt;/em&gt; equals *&lt;strong&gt;*false&lt;/strong&gt;: &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;false&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;0&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;-0&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;NaN&lt;/code&gt; and the empty string &lt;code class=&quot;language-text&quot;&gt;“”&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// All the following return false&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// The obvious boolean false&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// A null value&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// An undefined value&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// The number zero&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// The negative number zero&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0n&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// The bigint zero&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// The result of an operation that is not a number&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// An empty string&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Everything else &lt;em&gt;always&lt;/em&gt; equals &lt;strong&gt;true&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Be careful, all the following will return true&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// The obvious boolean true&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Any object&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Any array (which is an object)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Any date (which is an object)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Any number, except zero&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Any negative number, except zero&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;12n&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Any bigint, except zero&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3.14&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Any float number, except zero&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3.14&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Any negative float number, except zero&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;0&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Any string, except an empty one, including &quot;0&quot;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;false&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Any string, except an empty one, including &quot;false&quot;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;Infinity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Any object&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;Infinity&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Any object&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Any function&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;The list of truthy values in JavaScript is infinite.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You can also use the &lt;strong&gt;ternary operator&lt;/strong&gt; to save some space:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&apos;false&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;truthy&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;falsy&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// returns &apos;truthy&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I recommend for newbies to spend some time getting familiar with the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Glossary/Truthy&quot;&gt;truthy&lt;/a&gt; and the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Glossary/Falsy&quot;&gt;falsy&lt;/a&gt; values of JavaScript.&lt;/p&gt;
&lt;h3&gt;Switch statements &amp;#x26; Exhaustiveness checking&lt;/h3&gt;
&lt;p&gt;We can simplify the example above by using a &lt;strong&gt;switch&lt;/strong&gt; statement:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getWidth&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;width&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; width&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;number&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;width&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;string&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; width
    &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;auto&apos;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Switch statements support &lt;em&gt;any&lt;/em&gt; value you can imagine, even a computed one. We use them to refactor multiple blocks of if statements. More on them later.&lt;/p&gt;
&lt;p&gt;TypeScript can be really powerful when you are using a &lt;code class=&quot;language-text&quot;&gt;switch&lt;/code&gt; statement. With a feature called e&lt;strong&gt;xhaustiveness checking&lt;/strong&gt;, you can be sure that you handled all the available cases.&lt;/p&gt;
&lt;p&gt;Let’s illustrate this logic in the following example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ActionTypes&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;LOADING&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;LOADED&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;LoadingState&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  isLoading&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;reducer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  currentState&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; LoadingState&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  actionType&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; ActionTypes
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; LoadingState &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;actionType&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;LOADING&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;currentState&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; isLoading&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;LOADED&apos;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;currentState&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; isLoading&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; _exhaustiveCheck&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;never&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; actionType&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; _exhaustiveCheck&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;**&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt; keyword can play the role of the type guard here. Remember that we can’t assign any type of value to a variable with the type &lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt;, except from values that are themselves type of &lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We basically have a reducer here, which accepts the &lt;code class=&quot;language-text&quot;&gt;currentState&lt;/code&gt; and the &lt;code class=&quot;language-text&quot;&gt;actionType&lt;/code&gt; and it returns the updated state. We define all our action types in a type alias. The magic happens within the &lt;code class=&quot;language-text&quot;&gt;default&lt;/code&gt; ****case. We assign the actionType to a variable that is only meant to be used for exhaustiveness checking. Theoretically, if we did exhaust all the possible cases of the &lt;code class=&quot;language-text&quot;&gt;actionType&lt;/code&gt; values, we would never end up with the default case.&lt;/p&gt;
&lt;p&gt;Every time we add a new case block, TypeScript eliminates one of the possible values. When we reach the default case, it expects that there are no types left, which means the type of the &lt;code class=&quot;language-text&quot;&gt;actionType&lt;/code&gt; argument is &lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Can you guess how powerful this is? 🤠&lt;/p&gt;
&lt;p&gt;Let’s imagine that we have another action, to indicate that something has failed:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ActionTypes&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;LOADING&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;LOADED&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;FAILED&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The moment we change our type alias, TypeScript will throw an error:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/539f8fea652296c5ffcf52efa9b6379b/a0730/typescript-exhaustiveness-checking.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 49.42965779467681%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB30lEQVR42o2SS2/aUBCFr4MfUMAG4zd+gYONeaWwqKqkUqumqRogISSUJlGJFLKIuur/X52ODYtWlaIsPh3fez1HM5rDrNIPuOojgvpP9MJbdIw1vMIixy9cweIuYbKLV8Oa4hJjf4vj9BdO+s94W7/DcfUOX9QNjt6s4Rws8h+t1xoGpRVGjQ1O7Ed8MDboFqkz8QIuP4PGnUNj36CT6txOjYMpzD1Gdv4Lk5uB2TSaLT8gVLfo2VNI2gQVuY+wNoBLGiikhCmnsKopeMHfwXsQSEUxIEIIogtZmIBlo1jSCo7+DM+coqAPUZZjtJWYjGIyTuAThhyRYYRa4xCON4TfGkOpR2C8gYJogwkaKsKIDDkyFMmwvoVvnMPyBlCNBLY7hKrH0MwuTKeXmwStSX7n+CN6H+R3shqhWovQsDpQiuNdhzZt2m7+hqVeIyz20S4dwSkN4LnvkSafkUQf0U1PEcefoND45XJKY3ZQlGJIRFFKSA+hCO+yDi9hUzzMxhNa8j3Oqjc4Vb7jq7zCWX2NfnWFjrREm86Jdou+dY9W5QYGm+ZL+Jc5mEuxyTBo7Gz0Nn+FSCADfomOeI0mv6BNzykSczKZQ8/1hdhkZrvwzvIibV+k77//K+ZezuEfTKgYEQPak3YAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-exhaustiveness-checking.png&quot;
        title=&quot;&quot;
        src=&quot;/static/539f8fea652296c5ffcf52efa9b6379b/e996b/typescript-exhaustiveness-checking.png&quot;
        srcset=&quot;/static/539f8fea652296c5ffcf52efa9b6379b/17741/typescript-exhaustiveness-checking.png 263w,
/static/539f8fea652296c5ffcf52efa9b6379b/52211/typescript-exhaustiveness-checking.png 525w,
/static/539f8fea652296c5ffcf52efa9b6379b/e996b/typescript-exhaustiveness-checking.png 1050w,
/static/539f8fea652296c5ffcf52efa9b6379b/a0730/typescript-exhaustiveness-checking.png 1131w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;You have successfully guarded your reducer from action types that it doesn’t handle.&lt;/p&gt;
&lt;h3&gt;Logical Operators&lt;/h3&gt;
&lt;p&gt;JavaScript has the following operators: &lt;code class=&quot;language-text&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;||&lt;/code&gt;. But there’s a catch!&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The result value of AND, and OR operations in JavaScript is &lt;em&gt;not&lt;/em&gt; a &lt;strong&gt;Boolean&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The logical OR &lt;code class=&quot;language-text&quot;&gt;||&lt;/code&gt; operators return the &lt;strong&gt;first&lt;/strong&gt; value if it’s true, or the &lt;strong&gt;second&lt;/strong&gt; one if it’s false:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; s &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;text&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;
s &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; n &lt;span class=&quot;token comment&quot;&gt;// returns &apos;text&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It’s also a good hack to set a default value in case of a missing variable:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; tracks &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; response &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the example above, if the &lt;code class=&quot;language-text&quot;&gt;response&lt;/code&gt; is &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;, we will set an empty array as the initial value. How cool is that?&lt;/p&gt;
&lt;p&gt;The logical AND &lt;code class=&quot;language-text&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt; operators return the value of the &lt;strong&gt;second&lt;/strong&gt; argument if both values are true, or the &lt;strong&gt;first&lt;/strong&gt; one if both values are false, or the first falsy value:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; s &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;text&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;
s &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; n &lt;span class=&quot;token comment&quot;&gt;// returns &apos;text&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It’s a good hack to display UI elements:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;renderTracks&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;tracks&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; tracks &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; tracks&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;track&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;track&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This example will make sense to you if you are familiar with React’s JSX. Basically, we are checking whether the &lt;code class=&quot;language-text&quot;&gt;tracks&lt;/code&gt; parameter is truthy, which means it’s an array. Since the second part will &lt;em&gt;always&lt;/em&gt; be truthy, this is the value we will return. If tracks is &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;, we will return that value. It may seem like a huge hack, but it’s been used in every React application.&lt;/p&gt;
&lt;p&gt;Finally, the &lt;code class=&quot;language-text&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator is &lt;a href=&quot;https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Operator_Precedence&quot;&gt;more precedent&lt;/a&gt; than &lt;code class=&quot;language-text&quot;&gt;||&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// with the parenthesis&lt;/span&gt;
a &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// without the parenthesis (same result)&lt;/span&gt;
a &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; c&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Comparison Operators&lt;/h3&gt;
&lt;p&gt;There are two different ways to compare if the two values are equal. The &lt;code class=&quot;language-text&quot;&gt;==&lt;/code&gt; double equals and the &lt;code class=&quot;language-text&quot;&gt;===&lt;/code&gt; triple equals operators.&lt;/p&gt;
&lt;p&gt;The double equals &lt;code class=&quot;language-text&quot;&gt;==&lt;/code&gt; performs an &lt;strong&gt;implicit&lt;/strong&gt; &lt;strong&gt;coercion&lt;/strong&gt; before the comparison; aka it doesn’t check the types of the values. You should &lt;strong&gt;only&lt;/strong&gt; use it if you and everybody in your team &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness&quot;&gt;understand how it works&lt;/a&gt; and if you are familiar with all its &lt;a href=&quot;https://youtu.be/et8xNAc2ic8&quot;&gt;misbehavior&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;5&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// true&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I will address how this works in a moment. For now I want you to remember that most of the time you should use the triple equals operator &lt;code class=&quot;language-text&quot;&gt;===&lt;/code&gt; which also checks the type of the variable:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;5&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// true&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Objects are being checked for their &lt;em&gt;reference&lt;/em&gt;. JavaScript will &lt;em&gt;never&lt;/em&gt; compare their values.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nicos&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nicos&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// false&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Ah, and &lt;code class=&quot;language-text&quot;&gt;NaN&lt;/code&gt; is not equal to itself:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token number&quot;&gt;NaN&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;NaN&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;NaN&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;NaN&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// false&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Which is the reason we use &lt;code class=&quot;language-text&quot;&gt;Object.is()&lt;/code&gt; method:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;Object&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// true&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It is safe to use &lt;code class=&quot;language-text&quot;&gt;==&lt;/code&gt; with the &lt;strong&gt;typeof&lt;/strong&gt; operator:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; five &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;number&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;no way!&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To compare if the two values are not equal, of course, you can use &lt;code class=&quot;language-text&quot;&gt;!=&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;!==&lt;/code&gt;. They work similarly with equal operators. Abstract relational comparison (&lt;code class=&quot;language-text&quot;&gt;&amp;lt;&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;&gt;&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;&amp;lt;=&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;&gt;=&lt;/code&gt;) always coerces the values, similar to &lt;code class=&quot;language-text&quot;&gt;==&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;And one more thing to make fun of your other JavaScript colleagues;  &lt;code class=&quot;language-text&quot;&gt;a &gt; b&lt;/code&gt; is actually &lt;code class=&quot;language-text&quot;&gt;!(b &amp;lt; a)&lt;/code&gt;. 😎&lt;/p&gt;
&lt;p&gt;TypeScript understands all these rules as you compare values that can accept different types, and it can predict what the result type will be. This doesn’t work all of the time. It is suggested to setup a linter, if you want to have an indicator that you are probably making some mistakes, as you type code.&lt;/p&gt;
&lt;h3&gt;Some words about Coercion&lt;/h3&gt;
&lt;p&gt;In JavaScript there is a weird feature called &lt;strong&gt;Coercion&lt;/strong&gt;. Somehow the language itself takes care of the casting for you, so that you don’t have to. As we’ve seen this is happening everywhere in your code.&lt;/p&gt;
&lt;p&gt;Here’s an example we’ve seen before:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;5&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// true&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Obviously the &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt; five is not equal to the &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt; five. But that’s how the double equal operator is working. It first coerces (converts) the values, and &lt;em&gt;then&lt;/em&gt; it compares them. In JavaScript, you can compare &lt;em&gt;any&lt;/em&gt; value, regardless of their types. Conditionals can even be used anywhere in your code, as a valid &lt;em&gt;expression&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;That’s basically called &lt;strong&gt;implicit coercion&lt;/strong&gt;, because you don’t explicitly cast the value by yourself.&lt;/p&gt;
&lt;p&gt;A lot of people prefer &lt;strong&gt;explicit coercion&lt;/strong&gt;. Here’s an example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;5&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// true&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, there’s a misconception about &lt;em&gt;how&lt;/em&gt; types will be coerced. Do you expect JavaScript to convert the string to a number or the number to a string? Let’s give it a try:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;1&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &apos;51&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Yup! We have a winner! 😋
In general, there is a priority in place:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// String beats everyone&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;1&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// &apos;21true&apos;&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// Number beats booleans&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// 3&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// Avoid non-primitive types&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// false&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Coercion is &lt;em&gt;indeed&lt;/em&gt; a powerful feature, which is intended to help you reduce code and improve readability. It’s one of the reasons developers who are new to coding like JavaScript. It’s extremely unpredictable, though. And you should spend an amount of time getting used to it.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“Every JavaScript developer should spend some time to &lt;a href=&quot;https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/&quot;&gt;understand the concept of Coercion&lt;/a&gt; and learn its rules.”
&lt;cite&gt;Kyle Simpson&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;The &lt;code class=&quot;language-text&quot;&gt;in&lt;/code&gt; operator&lt;/h3&gt;
&lt;p&gt;We can check if an object contains a member, by using the &lt;code class=&quot;language-text&quot;&gt;in&lt;/code&gt; operator:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  filename&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token function-variable function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;UnPlayable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  filename&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;playFile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;file&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Playable &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; UnPlayable&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;play&apos;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; file&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    file&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example, we have two interfaces. Only one of them contains the &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; method. In the play function, we can narrow down the &lt;code class=&quot;language-text&quot;&gt;file&lt;/code&gt; objects, to find the ones that are playable.&lt;/p&gt;
&lt;h2&gt;Type predicates&lt;/h2&gt;
&lt;p&gt;What if I told you that you can use all the tools you’ve learned above to build your own type guards? Sounds intriguing? 😉&lt;/p&gt;
&lt;p&gt;Here’s an example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;isPlayable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;file&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Object&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; file &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; Playable &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;play&apos;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; file
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Mind the &lt;code class=&quot;language-text&quot;&gt;is&lt;/code&gt; operator in the return value of this function. This is not a function that returns a Playable. It acts as a type guard that checks if an object &lt;em&gt;is&lt;/em&gt; Playable. By saying &lt;code class=&quot;language-text&quot;&gt;file is Playable&lt;/code&gt;, we are creating a logical indicator that helps TypeScript treat this function as a type guard.&lt;/p&gt;
&lt;p&gt;We can now use this type guard to simplify the logic of our &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; function:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;playFile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;file&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Playable &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; UnPlayable&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;isPlayable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;file&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    file&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice how TypeScript now recognizes the type guard and it doesn’t complain about the missing &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; method in &lt;code class=&quot;language-text&quot;&gt;file&lt;/code&gt;. What a nice feature. This feature is quite useful in a language like JavaScript.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;This article took a lot of time, but I’m really happy I collected everything you need to know about narrowing types in TypeScript.&lt;/p&gt;
&lt;p&gt;If it helped you learn something new, then a tweet would be great 🙂.&lt;/p&gt;
&lt;p&gt;Cover Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/TMa_5BLvDqQ&quot;&gt;Rodion Kutsaev&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Master Generics in TypeScript]]></title><description><![CDATA[Understanding generics will help you read most of the TypeScript code that was ever written.]]></description><link>http://www.nicotsou.com/tltr-typescript-generics/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-generics/</guid><pubDate>Wed, 26 Jan 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;TypeScript can speed up our JavaScript projects. Its type system can show you errors right within your text editor. This can be cool most of the time. By spotting errors faster, you spend less time on debugging and bug fixing.&lt;/p&gt;
&lt;p&gt;Type checking can also cause frustration. In fact, sometimes it’s quite hard to deal with types. Since there were no types around, JavaScript developers got used to writing flexible code that covered a lot of use cases. A function that accepts all kinds of objects. A class that can be applied to multiple types. The situation becomes worse when you have to support legacy JavaScript code.&lt;/p&gt;
&lt;p&gt;Don’t get me wrong. Most of the time, having code that handles multiple types is convenient and totally legal. But how can we set boundaries?&lt;/p&gt;
&lt;p&gt;Moreover, there are cases that TypeScript becomes very demanding. Focusing on your product and its business logic becomes the hardest thing in the world. Instead, you are writing unnecessary code just to satisfy its type system.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you spent a day in your life just trying to get rid of a type mismatch, that you don’t even understand where it came from, you are not alone!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Thankfully, TypeScript provides a solution to this problem. It has support for generic types. This unlocks a ton of possibilities. Some people think this is the way to write code for your types. In the following paragraphs, we will learn what problem generics solve, their syntax, and how you can use them practically in your code.&lt;/p&gt;
&lt;p&gt;This article belongs to my series Too Long To Read TypeScript. Every article covers in detail a core concept of the language. I am also producing a &lt;a href=&quot;https://www.youtube.com/watch?v=D_0KI6SePcA&amp;#x26;list=PL73mkIDIrfyPKjkJ1V151lcgGEDHs3tgG&quot;&gt;free YouTube course&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Candy? 🍬&lt;/p&gt;
&lt;h2&gt;What is the problem Generics are solving?&lt;/h2&gt;
&lt;p&gt;The problem of introducing types into a language which was never designed with them in mind, shines as we try to create a more production-ready application. Let me give you a simplified example, which demonstrates the challenge that we face here.&lt;/p&gt;
&lt;p&gt;Consider the following function. It receives a song title and it displays it on screen:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;echoTitle&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; title&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;At the moment, our function is working only with &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt; types and it is returning a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;. Obviously the following will not work:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token function&quot;&gt;echoTitle&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2021&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// Type error, we are passing a number instead of a string&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In order to have a similar function, that works with &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt; types, we will need to define it separately:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;echoYear&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;year&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; year&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;echoYear&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2021&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it works perfectly!&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But what if we really, really want to have a single function, which just returns the types we pass on it, like in the good old JavaScript days:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; arg&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Well, somebody will think, let’s use &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arg&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;any&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; arg&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Wohohoo! Problem solved. Commit and push! 😎&lt;/p&gt;
&lt;p&gt;But wait a minute. Is this really type checking? I would argue it is not. As you may know, &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt; must be avoided, because it’s basically a wildcard that &lt;em&gt;disables&lt;/em&gt; type checking completely. Something like &lt;code class=&quot;language-text&quot;&gt;ts-ignore&lt;/code&gt;. As long as I hate to admit it, yes, &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt; is not the answer here. We need to be flexible by staying secure. But then what?&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/5b4b278ba4c1c7606615c7a6085da5b2/3bc42/flexible-girl-in-an-acrobatic-pose-in-the-gym-2021-11-16-12-20-02-utc.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAwAEBf/EABUBAQEAAAAAAAAAAAAAAAAAAAEA/9oADAMBAAIQAxAAAAHqCRQtlk//xAAaEAACAwEBAAAAAAAAAAAAAAAAAQIRMQMT/9oACAEBAAEFAm1WlGQl0qXsf//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABcQAQEBAQAAAAAAAAAAAAAAAAAREDH/2gAIAQEABj8C2LHH/8QAGxABAAIDAQEAAAAAAAAAAAAAAQARIUFRMYH/2gAIAQEAAT8hFdcRTR+kYLzrrc7jVewXM//aAAwDAQACAAMAAAAQDN//xAAWEQEBAQAAAAAAAAAAAAAAAAAAESH/2gAIAQMBAT8QyK//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAcEAEAAgMAAwAAAAAAAAAAAAABABEhMVFBkcH/2gAIAQEAAT8QAVOvBDWgxejDAucbxFuzPyXBCtzrfIkz72f/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;flexible-girl-in-an-acrobatic-pose-in-the-gym-2021-11-16-12-20-02-utc.JPG&quot;
        title=&quot;&quot;
        src=&quot;/static/5b4b278ba4c1c7606615c7a6085da5b2/9ecec/flexible-girl-in-an-acrobatic-pose-in-the-gym-2021-11-16-12-20-02-utc.jpg&quot;
        srcset=&quot;/static/5b4b278ba4c1c7606615c7a6085da5b2/5d001/flexible-girl-in-an-acrobatic-pose-in-the-gym-2021-11-16-12-20-02-utc.jpg 263w,
/static/5b4b278ba4c1c7606615c7a6085da5b2/7349d/flexible-girl-in-an-acrobatic-pose-in-the-gym-2021-11-16-12-20-02-utc.jpg 525w,
/static/5b4b278ba4c1c7606615c7a6085da5b2/9ecec/flexible-girl-in-an-acrobatic-pose-in-the-gym-2021-11-16-12-20-02-utc.jpg 1050w,
/static/5b4b278ba4c1c7606615c7a6085da5b2/39d31/flexible-girl-in-an-acrobatic-pose-in-the-gym-2021-11-16-12-20-02-utc.jpg 1575w,
/static/5b4b278ba4c1c7606615c7a6085da5b2/1f368/flexible-girl-in-an-acrobatic-pose-in-the-gym-2021-11-16-12-20-02-utc.jpg 2100w,
/static/5b4b278ba4c1c7606615c7a6085da5b2/3bc42/flexible-girl-in-an-acrobatic-pose-in-the-gym-2021-11-16-12-20-02-utc.jpg 5110w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Solving this challenge will help us a lot with our TypeScript journey. For example, how would you define the map array method itself, a server response promise, or even our most favorite &lt;code class=&quot;language-text&quot;&gt;console.log()&lt;/code&gt;? They can all work with any type. They are all very popular and hard to replace.&lt;/p&gt;
&lt;p&gt;What if I told you that TypeScript has support for such &lt;em&gt;generic&lt;/em&gt; cases. Well, my friends. Let me introduce you to Generics…&lt;/p&gt;
&lt;h2&gt;Generic Functions&lt;/h2&gt;
&lt;p&gt;What if we had some sort of a &lt;em&gt;parameter&lt;/em&gt;, which stores the type of the argument. You could then use that parameter to define what our function accepts and returns.&lt;/p&gt;
&lt;p&gt;This is exactly how &lt;strong&gt;generics&lt;/strong&gt; work:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Type&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arg&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Type&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Type &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; arg&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that now our general &lt;code class=&quot;language-text&quot;&gt;echo()&lt;/code&gt; function accepts an extra parameter of &lt;code class=&quot;language-text&quot;&gt;Type&lt;/code&gt;. We use &lt;code class=&quot;language-text&quot;&gt;&amp;lt;&gt;&lt;/code&gt; to indicate the existence of this extra type parameter. This is basically a generic variable, that we declare when we will consume the &lt;code class=&quot;language-text&quot;&gt;echo&amp;lt;&gt;()&lt;/code&gt; function. Then, we use this generic to define the type of the &lt;code class=&quot;language-text&quot;&gt;arg&lt;/code&gt; parameter and the type of the return value.&lt;/p&gt;
&lt;p&gt;Let’s break down that function declaration:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;// we have a function
function
// That is named echo
         echo
// Which accepts a type parameter, named Type
             &amp;lt;Type&gt;
// And also accepts a normal parameter, named arg
                   (arg      )
// and it&apos;s value is the same as the one Type uses
                   (   : Type)
// And it returns a value that uses the type of Type
                              : Type&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For simplicity, the TypeScript community simply uses the capital letter &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt; to name this type parameter:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;arg&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; arg&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can use our newly created function. In order to do so, we &lt;em&gt;have&lt;/em&gt; to specify the type that we want to use for our generic &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2021&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// will work&lt;/span&gt;
&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// will also work&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now compare this approach with the &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt; approach. TypeScript will &lt;em&gt;prevent&lt;/em&gt; any other type from being passed to this function accidentally or intentionally:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; year &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;2021&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;year&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Year is not a number, and this will fail&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And remember, we are not limited to using built-in types only. We can use our own classes, interfaces, type aliases, or anything else.&lt;/p&gt;
&lt;p&gt;So far we have been &lt;em&gt;explicitly&lt;/em&gt; defining the types in our generic functions. TypeScript also supports an &lt;em&gt;implicit&lt;/em&gt; way:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; year &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;2021&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;year&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// T is inferred as a string&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I have simply removed the type and now it echoes the string. This means that you can simply consume generic functions, without explicitly specifying their types. Type inference kicks in. Try hovering over this echo function, to see what type is inferred.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I don’t know about you, but this looks like JavaScript to me.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/4a9e94dfb0e08c942e13c7ffc47ee56c/eb645/shubham-dhage-VRFN2dIBbmU-unsplash.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 30.038022813688215%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAl0lEQVR42k2QWxLFEBBErUR2gnilqML+d5NzdV2V/phM6GMaE2MMIdz3HbdSSjnnUkqt9XmeusVK2pKNCkJj9NESJhoqQGttjDHnpDk8FT8NNu+9Eeac40cHsYcb8rouay28YDmxYVYEowzkzH8p84HXWuSX4VxKN/3B4hXmbPfe59Y3Nk4qw0WZ81ra+A7Xm5UtHX2keS/J7V128sR0EgAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;shubham-dhage-VRFN2dIBbmU-unsplash.png&quot;
        title=&quot;&quot;
        src=&quot;/static/4a9e94dfb0e08c942e13c7ffc47ee56c/e996b/shubham-dhage-VRFN2dIBbmU-unsplash.png&quot;
        srcset=&quot;/static/4a9e94dfb0e08c942e13c7ffc47ee56c/17741/shubham-dhage-VRFN2dIBbmU-unsplash.png 263w,
/static/4a9e94dfb0e08c942e13c7ffc47ee56c/52211/shubham-dhage-VRFN2dIBbmU-unsplash.png 525w,
/static/4a9e94dfb0e08c942e13c7ffc47ee56c/e996b/shubham-dhage-VRFN2dIBbmU-unsplash.png 1050w,
/static/4a9e94dfb0e08c942e13c7ffc47ee56c/087e3/shubham-dhage-VRFN2dIBbmU-unsplash.png 1575w,
/static/4a9e94dfb0e08c942e13c7ffc47ee56c/8079d/shubham-dhage-VRFN2dIBbmU-unsplash.png 2100w,
/static/4a9e94dfb0e08c942e13c7ffc47ee56c/eb645/shubham-dhage-VRFN2dIBbmU-unsplash.png 2500w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/VRFN2dIBbmU&quot;&gt;Shubham Dhage&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Generic Classes&lt;/h2&gt;
&lt;p&gt;So far, we’ve seen how generics can be used with functions. Let’s review another example, this time with classes.&lt;/p&gt;
&lt;p&gt;Here’s a &lt;code class=&quot;language-text&quot;&gt;Playlist&lt;/code&gt; class that stores some tracks:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playlist&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	tracks&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;token function&quot;&gt;addToPlaylist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTrack&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;token function&quot;&gt;removeFromPlaylist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;trackToRemove&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We’ve seen the class &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; in previous posts:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What if I had also music videos in addition to tracks? I will most probably need another playlist class that supports a new type of &lt;code class=&quot;language-text&quot;&gt;VideoClip&lt;/code&gt;. Or I can simply modify my &lt;code class=&quot;language-text&quot;&gt;Playlist&lt;/code&gt; class with a generic:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playlist&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; items&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;addToPlaylist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newItem&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newItem&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;removeFromPlaylist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;idToRemove&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      i&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id &lt;span class=&quot;token operator&quot;&gt;!==&lt;/span&gt; idToRemove&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The same concept applies here. I have a generic type &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt;, which stores the type of the class I want to instantiate. We use this type &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt; to define the &lt;code class=&quot;language-text&quot;&gt;items&lt;/code&gt; list and the &lt;code class=&quot;language-text&quot;&gt;newItem&lt;/code&gt; parameter of our &lt;code class=&quot;language-text&quot;&gt;addToPlaylist()&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;Now here’s how you can use this class with multiple types of object types:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; songPlaylist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Playlist&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Track&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playlist&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Track&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; videoPlaylist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Playlist&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;VideoClip&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playlist&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;VideoClip&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Just like that, we now have the same functionality that handles a playlist, without having to maintain two different classes.&lt;/p&gt;
&lt;p&gt;Now the magic begins. As I keep adding items in my playlist, TypeScript prevents me from storing something I’m not supposed to:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;songPlaylist&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;addToPlaylist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// works fine&lt;/span&gt;
songPlaylist&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;addToPlaylist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;VideoClip&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// throws an error, songPlaylist accepts only Tracks&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Generic Constraints&lt;/h2&gt;
&lt;p&gt;Something you will most probably experience with generic classes, is the fact that TypeScript may start complaining about properties that aren’t defined.&lt;/p&gt;
&lt;p&gt;In our previous example, the TypeScript compiler is complaining about the &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; property. It simply can’t find it in class &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/77bdf8f1f3e1419dd5aeca9b4433a58e/fe720/typescript-generic-class-id-problem.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 39.543726235741445%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAABYlAAAWJQFJUiTwAAABV0lEQVR42p2RW0/CQBCFFyhUQQkPSG80QEq3pS2gAoKxoJhGQ1oql6hv/v9/cZwCGkl58uHLmd3ZmWzOYXp+A7v8hX5pgUl5AfsyRl1cQRNiyCyCRMgshJyJ9iQ1+6nTMCWzhFr8gCx+oi68QxfWaAobqtf0gHo0WGMxrqhOVCJqByS6Sy2UszEalS14YwHnJsLIfsWDGWBoBRjwJ4ytKTzjGW5rDqc5h6n5sPQZOo1HaOJLailTc0sohQ3aso/xrQvucZiuCdvl6HgJFhxSt2fDI2zqOd3kzKGXn/YLs4kle5gmRDCqWwyNADM+QmCM4Tt38Cf0u+F0x/1kjq4zgNXuosP7sM0ebN5DqxKQNUvohegXJpOHrbMV3NIK7XPS4hrXFxQUURcpoAINkGr5CKoQHqFkwrSHWi55/AZpZ35I5oeoHlQ6pCyx04meTFnNHpuq/EH+B9+TvOb7q9LhgwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-generic-class-id-problem.png&quot;
        title=&quot;&quot;
        src=&quot;/static/77bdf8f1f3e1419dd5aeca9b4433a58e/e996b/typescript-generic-class-id-problem.png&quot;
        srcset=&quot;/static/77bdf8f1f3e1419dd5aeca9b4433a58e/17741/typescript-generic-class-id-problem.png 263w,
/static/77bdf8f1f3e1419dd5aeca9b4433a58e/52211/typescript-generic-class-id-problem.png 525w,
/static/77bdf8f1f3e1419dd5aeca9b4433a58e/e996b/typescript-generic-class-id-problem.png 1050w,
/static/77bdf8f1f3e1419dd5aeca9b4433a58e/fe720/typescript-generic-class-id-problem.png 1448w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;That’s unfortunate, because the type of &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt; is &lt;code class=&quot;language-text&quot;&gt;unknown&lt;/code&gt;. TypeScript can’t tell if &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; exists there or not. I wonder how I can tell TypeScript that this &lt;em&gt;thing&lt;/em&gt;, no matter what it is, it must have a property &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; in it. Otherwise, I cannot find how to remove it from my list.&lt;/p&gt;
&lt;p&gt;But wait a minute, this is what interfaces are meant for, right? Here’s what we need:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WithId&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And here’s how to combine it with our generic type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playlist&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; WithId&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; items&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;addToPlaylist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newItem&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newItem&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;removeFromPlaylist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;idToRemove&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      i&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id &lt;span class=&quot;token operator&quot;&gt;!==&lt;/span&gt; idToRemove&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example I have created an interface &lt;code class=&quot;language-text&quot;&gt;WithId&lt;/code&gt;, which has an &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; property in it. Now all the types that are passed in my class must implement this interface. Basically, this requires them to have an &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; property. So, even if they aren’t explicitly using the &lt;code class=&quot;language-text&quot;&gt;WithId&lt;/code&gt; interface, they could be compatible if they have that property.&lt;/p&gt;
&lt;p&gt;Here’s what will happen if they don’t:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/8c373e28d14eb284af816dc3fa3b3fef/19bb3/class-property-id-missing.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 20.53231939163498%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAYAAACOXx+WAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA7ElEQVR42nXOzU7CQBSG4ULBRIniwvhT1LZip9Npp4UOFQwEiYlGaFrCRu//Sl4LxKWLJ985u9e6a9f4/R3Dyx+CZk3vG/3wgdFzxuoNLRaMouNmcnkwilaHP3icIP0pwn1BeAa3v8Jy7Aq/W+N2toiTLZ69QzgzFuOUiYrJMk1e5BRTc6DTmDQ7UokkSRUqligd4F+/Yg3sLU67wmltuG2V3Fgl2f2S92jETKWY3DCdFOTjAhkmhEIhAkW4J/7ESCnxr+b7wl1TWDV1n81ueOquiXprnk8rhmc10fkXyUWJ19xOZ8Og+z/HLvkF/Th3J7+2HHcAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;class-property-id-missing.png&quot;
        title=&quot;&quot;
        src=&quot;/static/8c373e28d14eb284af816dc3fa3b3fef/e996b/class-property-id-missing.png&quot;
        srcset=&quot;/static/8c373e28d14eb284af816dc3fa3b3fef/17741/class-property-id-missing.png 263w,
/static/8c373e28d14eb284af816dc3fa3b3fef/52211/class-property-id-missing.png 525w,
/static/8c373e28d14eb284af816dc3fa3b3fef/e996b/class-property-id-missing.png 1050w,
/static/8c373e28d14eb284af816dc3fa3b3fef/087e3/class-property-id-missing.png 1575w,
/static/8c373e28d14eb284af816dc3fa3b3fef/19bb3/class-property-id-missing.png 2066w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Now let’s modify our &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; class to implement the new &lt;code class=&quot;language-text&quot;&gt;WithId&lt;/code&gt; interface:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;WithId&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTrackId&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newTrackId&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Again, if I don’t explicitly add &lt;code class=&quot;language-text&quot;&gt;implements WithId&lt;/code&gt; in my class declaration, TypeScript will not complain at all, since the &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; property is present in my &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; class, and its type matches. Just because this is a class under my control, this is considered good practice. It will be difficult to track which properties are really required in this class and which are not.&lt;/p&gt;
&lt;p&gt;Finally, instead of creating an interface, we can solve the missing type declaration, by simply providing its type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playlist&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Use this approach if you think that the interface will not be reused. I personally prefer to create interfaces, because it just looks better when I read a class declaration and it is much easier to modify them in the future.&lt;/p&gt;
&lt;h2&gt;Generic Interfaces&lt;/h2&gt;
&lt;p&gt;We can create generic interfaces, similar to how we declare generic classes:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IPlaylist&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	items&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	
	&lt;span class=&quot;token function&quot;&gt;addToPlaylist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newItem&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token function&quot;&gt;removeFromPlaylist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;itemToRemove&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And check this out. Now we can use the &lt;code class=&quot;language-text&quot;&gt;IPlaylist&lt;/code&gt; interface in our &lt;code class=&quot;language-text&quot;&gt;TrackPlaylist&lt;/code&gt; class:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TrackPlaylist&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IPlaylist&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Track&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; items&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;addToPlaylist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newItem&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;removeFromPlaylist&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;itemToRemove&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Just like that, we now have an implementation of the &lt;code class=&quot;language-text&quot;&gt;Playlist&lt;/code&gt; interface, which stores &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; instances.&lt;/p&gt;
&lt;p&gt;Generic Interfaces are quite popular in TypeScript. They help you express many challenging scenarios and they are used quite frequently.&lt;/p&gt;
&lt;h2&gt;Discovering generics in more places&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Understanding generics will help you read most of the TypeScript code that was ever written.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Now let’s combine everything we’ve learned. Remember the array method &lt;code class=&quot;language-text&quot;&gt;map()&lt;/code&gt;? We could use generic types to implement its signature. Here’s the definition of the &lt;code class=&quot;language-text&quot;&gt;Array&lt;/code&gt; interface, which contains the &lt;code class=&quot;language-text&quot;&gt;map()&lt;/code&gt; method:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;&lt;span class=&quot;token builtin&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token constant&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function-variable function&quot;&gt;callbackfn&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;value&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; index&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; array&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; thisArg&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;U&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that here they utilize two generic types, one at the interface level, named &lt;code class=&quot;language-text&quot;&gt;T&lt;/code&gt;, and one within our method, named &lt;code class=&quot;language-text&quot;&gt;U&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;At the beginning of this series, we talked about arrays. Here’s how we declare an array variable:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; daysOfTheWeekend&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Saturday&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Sunday&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Since arrays in JavaScript are basically objects, we could actually use the &lt;code class=&quot;language-text&quot;&gt;Array&lt;/code&gt; interface to declare an array:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; daysOfTheWeekend&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Saturday&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Sunday&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will have the exact same results as before. Failing to pass the right values will fail:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;daysOfTheWeekend&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// fails because this is an array with string values&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, if you are in VS Code, try to right click on the &lt;code class=&quot;language-text&quot;&gt;Array&lt;/code&gt; interface and select Go to Definition.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/18e2986db37828e4bffe91aaba2d7fa3/997c2/typescript-es5-type-definition.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 25.85551330798479%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAABYlAAAWJQFJUiTwAAABMElEQVR42lWQXUvCcBjFJ6I5p3NTMufm3nzDoiSiugj8AN2Gb3txMmESGiZd9el//Y0Kuvhxzs05h+eRDCOkN8zwnRTPWNO1EmxrjXPSToLnr+jYMX43oWXEXLQims2IjrnCFRm9vkTTZmj6lIaSIhXsJdV+RLGXUB1MKdgBkrlAav9g/NecOSdnrWg8fjB+/qQ/eUce75Bv9yjtoyhsz8i35shOgtoLUZyQYntJQXBmBt/8+uIfU5o3GaPJkeHTAfthT+Mqo6q/iUJrQd54oeTHqMMN+nBF2QuQ3QDFj1C8kJKzpOxGlB2BG1PqLDCuN1xODrj3r9h3GeejlJq6RzoT5xWbImxu0b0dDXdLzczQrIzaiZMXqFZKxYlR7AjZClDFWH0g3iSGTr7ihlS1jC9mopt0ppR2yAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-es5-type-definition.png&quot;
        title=&quot;&quot;
        src=&quot;/static/18e2986db37828e4bffe91aaba2d7fa3/e996b/typescript-es5-type-definition.png&quot;
        srcset=&quot;/static/18e2986db37828e4bffe91aaba2d7fa3/17741/typescript-es5-type-definition.png 263w,
/static/18e2986db37828e4bffe91aaba2d7fa3/52211/typescript-es5-type-definition.png 525w,
/static/18e2986db37828e4bffe91aaba2d7fa3/e996b/typescript-es5-type-definition.png 1050w,
/static/18e2986db37828e4bffe91aaba2d7fa3/087e3/typescript-es5-type-definition.png 1575w,
/static/18e2986db37828e4bffe91aaba2d7fa3/8079d/typescript-es5-type-definition.png 2100w,
/static/18e2986db37828e4bffe91aaba2d7fa3/997c2/typescript-es5-type-definition.png 3162w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;You will witness one of the built-in type definitions that TypeScript already provides for us. More specifically, in the &lt;code class=&quot;language-text&quot;&gt;lib.es5.d.ts&lt;/code&gt; file, TypeScript contains type definitions for all ES5 features. This can give us an idea of how generics can be used to define highly reusable code. Now you can apply functional programming to your project, without worrying about types.&lt;/p&gt;
&lt;p&gt;I would encourage you to spend some time reading type definition files. They will help you understand how to use generics in your code. They can act as a good practice to apply what you’ve learned. You can also take ideas about challenging topics and see how other developers have overcome them.&lt;/p&gt;
&lt;p&gt;Cover Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/PACWvLRNzj8&quot;&gt;Shubham Dhage&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Type Wars: The TypeScript Saga]]></title><description><![CDATA[In this article, we will review and compare side-by-side the three main TypeScript keywords to create object structures.]]></description><link>http://www.nicotsou.com/tltr-typescript-type-wars/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-type-wars/</guid><pubDate>Wed, 19 Jan 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;In previous articles, we’ve seen that TypeScript is a superset of JavaScript, that adds strict type support and a better developer experience. We discovered &lt;a href=&quot;/tltr-typescript-types&quot;&gt;type aliases&lt;/a&gt;, &lt;a href=&quot;/tltr-typescript-classes&quot;&gt;classes&lt;/a&gt; and &lt;a href=&quot;/tltr-typescript-interfaces&quot;&gt;interfaces&lt;/a&gt;. Pretty cool stuff!&lt;/p&gt;
&lt;p&gt;Some of us, including the author of this blog, are pretty skeptical about choosing which of these types to use under certain circumstances. Failing to do so, will increase confusion in the team and potentially will make the code harder to follow. The most difficult to pick is how to define the structure of an object. We can either use a &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt;, an &lt;code class=&quot;language-text&quot;&gt;interface&lt;/code&gt;, or even a plain &lt;code class=&quot;language-text&quot;&gt;type&lt;/code&gt; alias. There are some subtle differences between these three keywords.&lt;/p&gt;
&lt;p&gt;Well, not anymore! In this article, we will review their syntax and we will compare them side-by-side. Once and forever. I hope after reading the following paragraphs, you will feel more confident choosing the right type keywords in your projects, without any hesitation.&lt;/p&gt;
&lt;p&gt;Coconut? 🥥&lt;/p&gt;
&lt;h2&gt;Too many keywords to choose from&lt;/h2&gt;
&lt;p&gt;Consider the following &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; object, that we’ve seen so far in the previous articles:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Hard rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Progressive rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I can specify that this is an object in the following ways:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// using the object keyword:&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; object &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Hard rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Progressive rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// using the object notation:&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Hard rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Progressive rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The first one is using the &lt;code class=&quot;language-text&quot;&gt;object&lt;/code&gt; keyword, which pretty much describes every object. The second one uses &lt;em&gt;object notation&lt;/em&gt; instead. These &lt;code class=&quot;language-text&quot;&gt;{}&lt;/code&gt; object literals after the &lt;code class=&quot;language-text&quot;&gt;:&lt;/code&gt; colon don’t create a new object. It’s TypeScript’s object notation for setting an object as a type.&lt;/p&gt;
&lt;p&gt;This basically tells TypeScript that &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; is expected to be an object. But this is not specific enough, is it? For example, if we try to assign a new object, it will work just fine:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; message&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;goodbye track!&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// track object has been replaced&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There is a need to describe what is expected in the individual properties within the object notation:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Hard rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Progressive rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Clearly this works much better now. If we try to store an irrelevant object to the &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; variable, we will get back a compilation error:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; problem&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;I don&apos;t know what I&apos;m doing!&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// throws an error&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we are guarding this &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; variable with precise prescriptions on what values it should accept. But again, what if we want to pass this object in multiple places? Clearly this long object notation is not efficient enough. And you know what they say about repeating yourself and how to keep things DRY.&lt;/p&gt;
&lt;p&gt;Ah, and good luck if the object structure of our &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; variable change. We don’t want to put ourselves in a situation that requires a complete rewrite of our application just to support some new features. I hear you. We could try to use a type alias.&lt;/p&gt;
&lt;h2&gt;Type Aliases&lt;/h2&gt;
&lt;p&gt;Here’s the syntax of a type alias:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can refactor our &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; object like so:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Hard rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Progressive rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we use the &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; alias to guard the &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; variable. Basically, the result is the same as before, if we try to assign a non-compatible object value, or any other type that doesn’t match the &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; alias, TypeScript will do one thing that knows very well. To throw beautiful colors.&lt;/p&gt;
&lt;p&gt;Let’s review the main features of type aliases.&lt;/p&gt;
&lt;h3&gt;Type aliases support primitive types&lt;/h3&gt;
&lt;p&gt;First off, they can support primitive types and object structures:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;maybeAString&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we used a union, to have a string that accepts &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt; as a value. By default, TypeScript will yell at you if you try to leave a string &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/15db05380595b7015274bb9279eecff8/9c62f/typescript-used-before-assigned-error.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 20.152091254752854%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAYAAACOXx+WAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA8klEQVR42kXOW0vCcACG8X/T2bbyNHdeGzNnS1dbSqRRFIEQZCgeLoPCm+j7f4CnMcMufrzwXL3Ck7dk1o7c/2Ry+c3D4Ic82DHxvrg2PjDFEkda4xyt8KRNYb1X2Ry4f82VVghfWhLLGyJli2+9EkbPpMmMYSGJZ9jtKa56i6GOqFWGqPJVIUWp/itbLaUp3yMsMS9evGGLBS1ljHOWYJh9DDfBDVOCXla0AY1ORMvu0S60rHM6br9cw79AawZouk9dSxG2eGdvgVV9JNTG+PIIW5/QDV8IvCfi7gxTv+NEyThV81JduzmsdpyhKikNecovyBNuXav44/YAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-used-before-assigned-error.png&quot;
        title=&quot;&quot;
        src=&quot;/static/15db05380595b7015274bb9279eecff8/e996b/typescript-used-before-assigned-error.png&quot;
        srcset=&quot;/static/15db05380595b7015274bb9279eecff8/17741/typescript-used-before-assigned-error.png 263w,
/static/15db05380595b7015274bb9279eecff8/52211/typescript-used-before-assigned-error.png 525w,
/static/15db05380595b7015274bb9279eecff8/e996b/typescript-used-before-assigned-error.png 1050w,
/static/15db05380595b7015274bb9279eecff8/087e3/typescript-used-before-assigned-error.png 1575w,
/static/15db05380595b7015274bb9279eecff8/8079d/typescript-used-before-assigned-error.png 2100w,
/static/15db05380595b7015274bb9279eecff8/9c62f/typescript-used-before-assigned-error.png 2162w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;But we can if we introduce our own type alias. It won’t be a string. It &lt;code class=&quot;language-text&quot;&gt;maybeAString&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;A type alias can fully support every feature of an &lt;code class=&quot;language-text&quot;&gt;interface&lt;/code&gt;. The opposite isn’t possible. You see, interfaces only describe object structures, not primitive types.&lt;/p&gt;
&lt;p&gt;This unlocks the possibility to use a type alias as an enum:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FileTypes&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp4&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;No other type annotation keyword in TypeScript has support for primitive types. Easy choice!&lt;/p&gt;
&lt;h3&gt;Type aliases can only be declared once&lt;/h3&gt;
&lt;p&gt;If we try to declare another track, we will get back an error:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// error: Duplicate identifier &apos;Track&apos;.&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;So, when to use a type alias?&lt;/h3&gt;
&lt;p&gt;That’s a type alias. It’s basically a shortcut to create custom types. Something like a variable that you can store any type annotation that you want. It basically supports all the operators and primitives of TypeScript. You must use type aliases when you have to support primitive types. They can take multiple forms. For example, they can act as an enum.&lt;/p&gt;
&lt;p&gt;Avoid them if you are building a shared library or if you are writing type definitions. Type aliases can’t be extended, like interfaces, so the consumers of your library will not be able to add new features if they want. This is limiting for type definitions that are meant to support existing JavaScript libraries.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Now somebody may argue, if interfaces are the same, then why use a &lt;code class=&quot;language-text&quot;&gt;type&lt;/code&gt; alias and not an &lt;code class=&quot;language-text&quot;&gt;interface&lt;/code&gt; instead? What’s wrong with interfaces?&lt;/p&gt;
&lt;p&gt;Great question. Really! Let’s elaborate more on that in the next section.&lt;/p&gt;
&lt;h2&gt;Interfaces&lt;/h2&gt;
&lt;p&gt;The previous example can be easily refactored to use an &lt;code class=&quot;language-text&quot;&gt;interface&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ITrack&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Mind the difference in their syntax; The &lt;code class=&quot;language-text&quot;&gt;=&lt;/code&gt; equals sign is not required when you declare an interface. We can then use the &lt;code class=&quot;language-text&quot;&gt;ITrack&lt;/code&gt; interface to guard our &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; object:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; ITrack &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Hard rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Progressive rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, I used the letter &lt;code class=&quot;language-text&quot;&gt;I&lt;/code&gt; as a prefix to distinguish the interface from the type alias.&lt;/p&gt;
&lt;p&gt;Now what is the difference? Do these examples produce different results? Does TypeScript get a different message? The answer is no! These two examples are completely identical. TypeScript will behave the same. There is no fundamental change whatsoever.&lt;/p&gt;
&lt;p&gt;Let’s review the main characteristics of interfaces, to understand what differentiates them from other type keywords.&lt;/p&gt;
&lt;h3&gt;Interfaces can act as type annotations for object structures&lt;/h3&gt;
&lt;p&gt;Interfaces are meant to declare an object structure. They don’t support primitive types, like type aliases do. In the example above, we replaced the &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; type alias with the interface &lt;code class=&quot;language-text&quot;&gt;ITrack&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Interfaces can connect unrelated classes together&lt;/h3&gt;
&lt;p&gt;You know that classes can &lt;em&gt;implement&lt;/em&gt; interfaces. TypeScript will check if all the members of the interface are being declared appropriately in your class.&lt;/p&gt;
&lt;p&gt;Consider the following interface, plain and simple:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ISpam&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;annoyReaders&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can use this interface in a blog post class:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;BlogPost&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ISpam&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;TLTR; TypeScript&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2022&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	author&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;nicotsou&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;annoyReaders&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Accept our coookie!&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But we can also use it to an email demon:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;EmailDemon&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ISpam&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;annoyReaders&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Please accept our coookie!&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;These two classes are completely irrelevant. By implementing the &lt;code class=&quot;language-text&quot;&gt;ISpam&lt;/code&gt; interface, they both share the same functionality. This means that we can now write code that implements functionality for both these objects, without worrying about unexpected results. We will expect that they will contain a method &lt;code class=&quot;language-text&quot;&gt;annoyReaders()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Beware of the cookies!&lt;/p&gt;
&lt;h3&gt;Interfaces can be extended&lt;/h3&gt;
&lt;p&gt;In contrast to type aliases, interfaces can be defined multiple times in your project. Their definitions can be merged. This feature is called &lt;em&gt;declaration merging&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Consider the following declarations of the same interface &lt;code class=&quot;language-text&quot;&gt;ITrack&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// declaration A&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ITrack&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// declaration B&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ITrack&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  length&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;They can be in the same file, or in different places within our project. TypeScript will not complain about this duplication. Instead, these two definitions will be merged into the following object structure:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ITrack&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// declaration A&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// declaration B&lt;/span&gt;
  length&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This feature of interfaces makes them ideal for type definitions for shared libraries. Consumers of these type definitions will have the opportunity to extend them in the case they want to add missing implementations.&lt;/p&gt;
&lt;h3&gt;So, where to use an interface?&lt;/h3&gt;
&lt;p&gt;It is common to use interfaces to represent the shape of objects, function arguments, and classes you want to behave in a common manner. Use interfaces for type definitions in your shared libraries.&lt;/p&gt;
&lt;p&gt;Some examples are a form model, your React component properties, the configuration object of a function, the response object of an api, or the payload of an event action.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/b28f2672e0daf589507e11d652c1c31e/0f98f/dan-counsell-y57MGB34dEE-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAMEAv/EABYBAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAAB05C8rycX/8QAGhAAAgIDAAAAAAAAAAAAAAAAAhEBIQASI//aAAgBAQABBQIOY7YIynToZlf/xAAXEQADAQAAAAAAAAAAAAAAAAAAAQIR/9oACAEDAQE/AXJiP//EABYRAQEBAAAAAAAAAAAAAAAAAAARAf/aAAgBAgEBPwGrr//EABkQAAIDAQAAAAAAAAAAAAAAAAABECExEf/aAAgBAQAGPwLrdFtThp//xAAbEAADAQEAAwAAAAAAAAAAAAAAAREhQTFRYf/aAAgBAQABPyF3qcZ6Lm1cGFbcFJhz9eR3kf/aAAwDAQACAAMAAAAQ68//xAAXEQEBAQEAAAAAAAAAAAAAAAABABFB/9oACAEDAQE/EB2D5f/EABcRAAMBAAAAAAAAAAAAAAAAAAABYRH/2gAIAQIBAT8QT7C5/8QAHBABAQEBAAIDAAAAAAAAAAAAAREhADFBUWFx/9oACAEBAAE/EFwPGma+/jmu9qhlnr94kSdAbnJ4qAEelsIK1dXXgSMGQO//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Miniatures fighting&quot;
        title=&quot;&quot;
        src=&quot;/static/b28f2672e0daf589507e11d652c1c31e/9ecec/dan-counsell-y57MGB34dEE-unsplash.jpg&quot;
        srcset=&quot;/static/b28f2672e0daf589507e11d652c1c31e/5d001/dan-counsell-y57MGB34dEE-unsplash.jpg 263w,
/static/b28f2672e0daf589507e11d652c1c31e/7349d/dan-counsell-y57MGB34dEE-unsplash.jpg 525w,
/static/b28f2672e0daf589507e11d652c1c31e/9ecec/dan-counsell-y57MGB34dEE-unsplash.jpg 1050w,
/static/b28f2672e0daf589507e11d652c1c31e/39d31/dan-counsell-y57MGB34dEE-unsplash.jpg 1575w,
/static/b28f2672e0daf589507e11d652c1c31e/0f98f/dan-counsell-y57MGB34dEE-unsplash.jpg 1920w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo credit: &lt;a href=&quot;https://unsplash.com/photos/y57MGB34dEE&quot;&gt;Dan Counsell&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;Interfaces vs type aliases&lt;/h3&gt;
&lt;p&gt;That’s a tricky choice, because in the end they both can describe object structures. Both interfaces and type aliases can be &lt;em&gt;implemented,&lt;/em&gt; which makes type aliases something like a super-type. Interfaces can be represented fully by types, but types cannot be represented with an interface. That’s because types can represent primitive types, object data structures or both if you use a union.&lt;/p&gt;
&lt;p&gt;Interfaces can be extended. That makes interfaces really great to work with when we are defining type definitions for our libraries.&lt;/p&gt;
&lt;p&gt;Types are commonly used in situations when you have an instance of a variable that you want to pass around, but I have seen projects using them to describe object structures.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;I hear a man screaming at the back of my head. Yes, I confess. Sometimes I do hear voices.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“Nicos, what about classes? They can also be used to describe object structures, right? What’s wrong with classes?”&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Another great question. Another paragraph for this article. OK, let’s see how this will go.&lt;/p&gt;
&lt;h3&gt;Classes&lt;/h3&gt;
&lt;p&gt;We will create a &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; class to describe the shape of our &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; object. I will name it &lt;code class=&quot;language-text&quot;&gt;TrackClass&lt;/code&gt;, just to avoid name conflicts:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TrackClass&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The syntax is similar to the interface, only the keyword at the beginning changes. But if you try this in your text editor, you will see that TypeScript cries multiple lines of errors:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/86e75e3f388f0341b33a5006d05179d4/08485/typescript-no-constructor-error.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 20.91254752851711%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAYAAACOXx+WAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA2ElEQVR42l2Oy07DMBRETe1K1KEiCnbsPFoeAomoQmpD2pSuWLDhIYEEfAD//xEHx4hNFkdz78wsRjj5wVX5Q3P6TTv7YqU/yeUbVryQi1dMUDvCjG4jnmPXBsRgXlePrJonFnnPefFA4Xt8vsNm99R+T2G3ONPhTdCzjtL1VIHBW4T+sjxg0k3ot4i4Irujumhw9S3zbInxN6T2Eqkd03mBOvHIxKESH3DxjyR/+YDUeeyIcvqOUwfqo5Zy0mFlWDXZ4sOdqpZjtUbLNbOAViMGX/1nm+j9AsyWac6qZAcMAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-no-constructor-error.png&quot;
        title=&quot;&quot;
        src=&quot;/static/86e75e3f388f0341b33a5006d05179d4/e996b/typescript-no-constructor-error.png&quot;
        srcset=&quot;/static/86e75e3f388f0341b33a5006d05179d4/17741/typescript-no-constructor-error.png 263w,
/static/86e75e3f388f0341b33a5006d05179d4/52211/typescript-no-constructor-error.png 525w,
/static/86e75e3f388f0341b33a5006d05179d4/e996b/typescript-no-constructor-error.png 1050w,
/static/86e75e3f388f0341b33a5006d05179d4/087e3/typescript-no-constructor-error.png 1575w,
/static/86e75e3f388f0341b33a5006d05179d4/08485/typescript-no-constructor-error.png 2014w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;We did define our class members, but we didn’t provide a way to instantiate objects out of it. Remember, a class is meant to be used as a blueprint to create objects. But how can we create objects if there is no &lt;code class=&quot;language-text&quot;&gt;constructor&lt;/code&gt; in place? Ha! We just found our first difference. Excitement. We will find out more as we go. But first, let’s fix that class:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TrackClass&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; title&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;releasedAt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; releasedAt&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;gernes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; gernes&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isFavorite &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; isFavorite&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;OK, now we are ready to review the characteristics of classes.&lt;/p&gt;
&lt;h3&gt;Classes are available at runtime&lt;/h3&gt;
&lt;p&gt;A class is not yet another TypeScript thingy, that we solely use for type annotation. It’s a JavaScript keyword nowadays. This means that it is available at runtime. This doesn’t apply to type aliases and interfaces, which are TypeScript exclusives. They will be completely removed from the transpiled code.&lt;/p&gt;
&lt;h3&gt;Classes can instantiate objects&lt;/h3&gt;
&lt;p&gt;The main purpose of a class is to create objects:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; TrackClass &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;TrackClass&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token number&quot;&gt;1974&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Hard rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Progressive rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can’t use other types like this, but we can use a class to annotate objects that have a compatible structure:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; TrackClass &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Hard rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Progressive rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, this example may look convenient to you, but again, a class is not meant to be used as a type to describe existing objects. In situations like this, consider using an interface or a type alias. It only makes sense to have a class, when you use its constructor to create objects and you want to apply certain rules upon their creation.&lt;/p&gt;
&lt;h3&gt;Classes support OOP features&lt;/h3&gt;
&lt;p&gt;Classes support a lot of features that we can find in classical object-oriented programming languages. You can have &lt;code class=&quot;language-text&quot;&gt;private&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;protected&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;public&lt;/code&gt; members. You can have setters and getters, &lt;code class=&quot;language-text&quot;&gt;readonly&lt;/code&gt;, or &lt;code class=&quot;language-text&quot;&gt;static&lt;/code&gt; properties. You can implement inheritance and many nice design patterns much easier. These features, together with the &lt;code class=&quot;language-text&quot;&gt;constructor&lt;/code&gt; we mentioned before, can provide safety, a sense of privacy to your models. They set standards and make your code much easier to understand and maintain.&lt;/p&gt;
&lt;p&gt;Although, using such OOP features is a design decision you have to make for your project, using a &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt; is the only way to implement it. No other type keyword has support for such OOP features. No can dosville baby doll. Don’t ask me why, that episode of HIMYM was playing while I was writing this article.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/12fdf49648dbd3d71f16d116511e9172/0f98f/stillness-inmotion-5N75xeV9x9Q-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.52471482889734%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAABAABBf/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAXYYh04sf//EABsQAAICAwEAAAAAAAAAAAAAAAECAAMEEyIh/9oACAEBAAEFAh4LHCKvS7CZcYuRz//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABkQAAIDAQAAAAAAAAAAAAAAAAABESIxUf/aAAgBAQAGPwInRPpaGMVT/8QAGhAAAwADAQAAAAAAAAAAAAAAAAERIUFRMf/aAAgBAQABPyGKPBgENBUikMRHIXXpcWhkNF0//9oADAMBAAIAAwAAABCjD//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8QP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8QP//EABsQAQEAAgMBAAAAAAAAAAAAAAERADEhQVFx/9oACAEBAAE/ECBQUK4rSAxjrBbAAHrEEL3wUyKVyc1Q6p7vDTGJe0Pmf//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Miniatures fighting&quot;
        title=&quot;&quot;
        src=&quot;/static/12fdf49648dbd3d71f16d116511e9172/9ecec/stillness-inmotion-5N75xeV9x9Q-unsplash.jpg&quot;
        srcset=&quot;/static/12fdf49648dbd3d71f16d116511e9172/5d001/stillness-inmotion-5N75xeV9x9Q-unsplash.jpg 263w,
/static/12fdf49648dbd3d71f16d116511e9172/7349d/stillness-inmotion-5N75xeV9x9Q-unsplash.jpg 525w,
/static/12fdf49648dbd3d71f16d116511e9172/9ecec/stillness-inmotion-5N75xeV9x9Q-unsplash.jpg 1050w,
/static/12fdf49648dbd3d71f16d116511e9172/39d31/stillness-inmotion-5N75xeV9x9Q-unsplash.jpg 1575w,
/static/12fdf49648dbd3d71f16d116511e9172/0f98f/stillness-inmotion-5N75xeV9x9Q-unsplash.jpg 1920w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/5N75xeV9x9Q&quot;&gt;Stillness InMotion&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;How do abstract classes compare to interfaces?&lt;/h3&gt;
&lt;p&gt;You may be tempted to use an abstract class instead of an interface. And it’s a very good option, for those who want to structure their models in a certain way. Besides the previous differences that we mentioned, there’s a fundamental one that separates interfaces from abstract classes.&lt;/p&gt;
&lt;p&gt;Interfaces can be used to set rules for the classes that implement them. Think of them like a contract. You accept the terms and conditions, now you have to implement them fully. This is similar to how abstract classes work.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;A class can implement multiple interfaces. Classes can only inherit from a single class.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Remember the &lt;code class=&quot;language-text&quot;&gt;ISpam&lt;/code&gt; interface and how we used it to connect two unrelated classes? If we had used an abstract class &lt;code class=&quot;language-text&quot;&gt;Spam&lt;/code&gt;, we would have to &lt;em&gt;inherit&lt;/em&gt; from it, and this unavoidably would have established a connection between these classes. That’s not what we want, right?&lt;/p&gt;
&lt;p&gt;By inheriting from an abstract class, your classes are strictly dependent on that abstract class. Interfaces on the other side, are loosely coupled. This means we could potentially use them to apply a certain behavior to classes that originate from different parents, or classes that are completely independent.&lt;/p&gt;
&lt;p&gt;On the other side, we can use abstract classes and inheritance to structure our code in a way that makes sense. In that case, we will get for free all the OOP features, that abstract classes support. This means we can be much more descriptive about how certain class members must be instantiated.&lt;/p&gt;
&lt;h2&gt;Enums&lt;/h2&gt;
&lt;p&gt;TypeScript has a dedicated &lt;code class=&quot;language-text&quot;&gt;enum&lt;/code&gt; keyword:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; FileTypesEnum &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  Mp3 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  Mp4 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp4&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In comparison with the type alias that we used before, enums have the following features.&lt;/p&gt;
&lt;h3&gt;Available at runtime&lt;/h3&gt;
&lt;p&gt;As we said, type aliases are used solely for type annotations and they don’t make it at runtime. On the other side, you can access the contents of an enum at runtime, because it will be included in the transpiled JavaScript code. The truth is, with enums you have the option to decide if you want them at runtime or not; with type aliases you don’t.&lt;/p&gt;
&lt;h3&gt;Type aliases vs enums&lt;/h3&gt;
&lt;p&gt;An &lt;code class=&quot;language-text&quot;&gt;enum&lt;/code&gt; is better to be used when we know all the values upfront. For example, the different statuses of an asynchronous api call, the days of the week, the different status flags, or to map state values to their keys. Use type aliases for dynamically generated values, or value sets that you don’t know upfront.&lt;/p&gt;
&lt;p&gt;Ah, and stay tuned, because in the following articles I will show you how to dynamically generate enums by using plain types. We still have a lot to learn from TypeScript.&lt;/p&gt;
&lt;p&gt;Cover Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/VEEqzwGwnpk&quot;&gt;Shubham Dhage&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[2021 happened. I survived.]]></title><description><![CDATA[In this Types Special, we will discover how JavaScript deals with types and how you can make your life simpler with TypeScript.]]></description><link>http://www.nicotsou.com/retrospective-2021/</link><guid isPermaLink="false">http://www.nicotsou.com/retrospective-2021/</guid><pubDate>Wed, 05 Jan 2022 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;This post is dedicated to 2021. I will try to summarize all the important events, changes, content I consumed and things I learned. Happy new year everyone!&lt;/p&gt;
&lt;h2&gt;The year we had to stay safe. Again. 🦠&lt;/h2&gt;
&lt;p&gt;It’s been almost a year since I started writing on this blog and it feels like yesterday. Basically, it feels like last month was 2019. It’s like we are stuck in a bad dream. A dream that repeats itself and it doesn’t want to end.&lt;/p&gt;
&lt;p&gt;We had a lot of faith in you 2021. Yes, in you, don’t look away. It’s going to be better, they were saying. No more restrictions, they were saying. Now we have the vaccine, they were saying. And here we are, again.&lt;/p&gt;
&lt;p&gt;This was the year that we consumed more rapid tests than chewing gum. It’s like, there was no other sickness or possibility for something negative to happen in general. Do you have a headache? Do your eyes hurt? Do you have pain in the stomach? Did you miss your period? Are your tests failing? Your wifi went offline? Just take a rapid test. You will feel better.&lt;/p&gt;
&lt;p&gt;Another year where hospitals are full. Family meetings are considered suicidal. Working from the office is illegal. Partying is also illegal and they know where you live. Working from home is the new black. Traveling is harder than bitcoin. Computer chips are the new toilet paper. Metaverse is still under development. And my 5G signal has improved, after the covid vaccinations. Thank you Bill Gates!&lt;/p&gt;
&lt;p&gt;Are you there? Are you safe?&lt;/p&gt;
&lt;h2&gt;Another year with endless online meetings ☎️&lt;/h2&gt;
&lt;p&gt;We are experiencing the rise of online meetings. It’s free, there are unlimited rooms available, and you can bring people from anywhere in the world. And now it’s not just about Facebooks and Facetimes. Our lives became an endless online call.&lt;/p&gt;
&lt;p&gt;Do you have a topic to discuss with your colleagues? Schedule a call. Annual winter party? Schedule a call. Team bonding event? Schedule a call. Watching a movie with your friends. Schedule a call. You are not sure which tie to put on? Schedule a call. Your cat is sick? Schedule a call and take a rapid test. What did I just tell you?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I call this Pijama-driven development.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Everybody loves online meetings because of these nice backgrounds and effects. Just make sure you mute your microphone when you are on another call with your friends, because the meeting gets boring. Ah, and remove that cat filter your kid put in yesterday. And please, please center this camera, we want to see your face, not your ceiling.&lt;/p&gt;
&lt;p&gt;Yes, this was the year I organized a virtual birthday party on Zoom. I also attended a virtual marriage on Skype. Two phrases I would never imagine myself saying.&lt;/p&gt;
&lt;h2&gt;The year I started my blog. For real. 👨🏼‍💻&lt;/h2&gt;
&lt;p&gt;I can’t be more grateful for 2021. We had all the time of the world to focus on the things that brings us joy, and we didn’t have the sadness and the reality shock of 2020. It was a good opportunity for me to start this blog and I’m really thrilled about it.&lt;/p&gt;
&lt;p&gt;I was preparing this site for years. Different designs, different technologies, different article topics and features. At the end I never had the dedication to finalize it. There was something else that was always more important. Not anymore. This blog became my creativity booster. It started as my second identity and now it becomes my own identity. Kinda weird if you think about it, but this is how it feels.&lt;/p&gt;
&lt;p&gt;It’s difficult to write, I must admit. Besides the fact that English is not my main language and sometimes it’s hard to express myself, this whole personal blog concept used to be really confusing in my head. I haven’t found the recipe that works well for me. I was also not organized enough. Or to phrase it more accurately, not organized at all.&lt;/p&gt;
&lt;p&gt;I now have a complete roadmap with things I want to produce as a content creator. This blog is just a working draft of what I have prepared for you, my friends.&lt;/p&gt;
&lt;p&gt;Let’s see what 2022 has to bring. Cheers to that. 🥂&lt;/p&gt;
&lt;h2&gt;The year I started my YouTube channel 📺&lt;/h2&gt;
&lt;p&gt;In case you missed it, I started a new &lt;a href=&quot;https://www.youtube.com/channel/UCdUj8NuSRk8M-0gem5lTUlA&quot;&gt;YouTube channel&lt;/a&gt;. My first series is called &lt;a href=&quot;https://youtube.com/playlist?list=PL73mkIDIrfyPKjkJ1V151lcgGEDHs3tgG&quot;&gt;Too Long To Read; TypeScript&lt;/a&gt; and it’s going to be a complete course, that you can watch for free. I’m using future tense here, because I’m still recording the videos.&lt;/p&gt;
&lt;p&gt;Starting a YouTube channel was a very interesting activity and I would like to share more about it in a separate post. A public note to myself.&lt;/p&gt;
&lt;h2&gt;The year I discovered Notion 📝&lt;/h2&gt;
&lt;p&gt;Speaking of planning, 2021 was the year I discovered Notion. This is not affiliate bullshit. I’m a big fan of this product. It is an affordable way to put my thoughts together and create a plan for my projects and personal improvement.&lt;/p&gt;
&lt;p&gt;I have so many things to share about Notion, but I believe it deserves its own post.&lt;/p&gt;
&lt;h2&gt;Another year with good reads 📚&lt;/h2&gt;
&lt;p&gt;Here, I want to list the most important books I’ve read in 2021.&lt;/p&gt;
&lt;p&gt;I want to begin with “The Now Habit” by Neil Fiore. I know it sounds cheesy, but this book changed my life. For a procrastinator like me, reading this book felt like someone was able to read my mind and do some research about it. Read it if you want to overcome your procrastination.&lt;/p&gt;
&lt;p&gt;I really enjoyed the books “It Doesn’t Have To Be Crazy At Work” and “Rework” by Jason Fried and David Heinemeier, the co-founders of Basecamp. They are both very easy to read. They list a lot of nice ideas about how modern tech companies work. Besides the &lt;a href=&quot;https://www.theverge.com/2021/5/3/22418208/basecamp-all-hands-meeting-employee-resignations-buyouts-implosion&quot;&gt;conflicts&lt;/a&gt; within the company last year, I do believe most of their points are totally valid.&lt;/p&gt;
&lt;p&gt;I wasn’t aware of the book “The Software Craftsman” by Sandro Mancuso. I was always confusing this book with another one, for some reason. I also didn’t know he used to work in Switzerland. I also had a chance to connect with him via our local Java meetup. I totally recommend his book. I was able to relate a lot to the events and concepts he is explaining. It helps a lot if you want to understand the profession of the software engineer on a higher level. Five stars!&lt;/p&gt;
&lt;p&gt;One of the books I always wanted to read was “The Clean Coder” by Uncle Bob, known as Robert C. Martin. It was easy to follow, although I believe he has written better books. It seemed to me this was a collection of personal notes. Some of them useful, others not so much. I had the chance to watch his speech at an internal EPAM event.&lt;/p&gt;
&lt;p&gt;Another interesting read was the book “Outliers, The Story Of Success” by Malcolm Gladwell. It was a fascinating journey to answer one important question. What does it take to be successful? What characteristics do all successful people have in common? For sure, you will be surprised as much as I was.&lt;/p&gt;
&lt;p&gt;In 2021 I also discovered the work of Austin Kleon. I’ve read all of his books “Steal Like An Artist”, “Show Your Work!”, and “Keep Going”. I would definitely recommend them to creative people who have always wanted to produce something, but always struggled to find the courage to make it happen.&lt;/p&gt;
&lt;h2&gt;Another year with great technologies 🔭&lt;/h2&gt;
&lt;p&gt;In 2021, I discovered languages and frameworks I hadn’t played with in the past. I discovered &lt;a href=&quot;https://www.python.org&quot;&gt;Python&lt;/a&gt; and &lt;a href=&quot;https://www.ruby-lang.org/en/&quot;&gt;Ruby&lt;/a&gt;. In other parallel universes they are probably my main languages. Recently I started playing with &lt;a href=&quot;https://dart.dev&quot;&gt;Dart&lt;/a&gt; and &lt;a href=&quot;https://flutter.dev&quot;&gt;Flutter&lt;/a&gt;. In another parallel universe, I am ditching React.&lt;/p&gt;
&lt;p&gt;I learned Gatsby, which I used to build this blog. It was the year I discovered the &lt;a href=&quot;https://testing-library.com&quot;&gt;testing-library&lt;/a&gt;, and I had a chance to refresh my &lt;a href=&quot;https://www.docker.com&quot;&gt;Docker&lt;/a&gt; and &lt;a href=&quot;https://d3js.org&quot;&gt;D3&lt;/a&gt; skills.&lt;/p&gt;
&lt;p&gt;Since I was preparing for my &lt;a href=&quot;https://www.youtube.com/watch?v=D_0KI6SePcA&amp;#x26;list=PL73mkIDIrfyPKjkJ1V151lcgGEDHs3tgG&quot;&gt;TypeScript course&lt;/a&gt;, I spent a lot of time mastering the language. You know what they say, the best way to learn something well is to teach it by yourself.&lt;/p&gt;
&lt;p&gt;Finally, I did a lot of preparation for the AWS certificate, which I will probably take. Someday.&lt;/p&gt;
&lt;h2&gt;Another one, with great games 🎮&lt;/h2&gt;
&lt;p&gt;The titles I enjoyed in 2021 are Control, It Takes Two, A Way Out, Returnal, and Doom Eternal. I also played Life Is Strange; True Colors, and Ghost Of Tsushima. I played Phantasmagoria, a MSDOS gem from &lt;a href=&quot;https://www.google.com/search?client=safari&amp;#x26;rls=en&amp;#x26;sxsrf=AOaemvJPaZVjCKlKEN3z1XeU3kgnY0cfRA:1641376880825&amp;#x26;q=Roberta+Williams&amp;#x26;stick=H4sIAAAAAAAAAOPgE-LUz9U3MDO2MC9TAjMN83JNsrWUs5Ot9JPL0vWT83MLSktSi-LLMlNS89MTc1OtUlKLM9PzUosWsQoE5SelFpUkKoRn5uRkJuYW72BlBABe4xkMUwAAAA&amp;#x26;sa=X&amp;#x26;ved=2ahUKEwjBz7GvrZr1AhX5_bsIHbC5DYkQmxMoAXoECDUQAw&quot;&gt;Roberta Williams&lt;/a&gt;. I also remembered the Theme Hospital and the recent Two Point Hospital. I love this game so much!&lt;/p&gt;
&lt;p&gt;In 2021 I introduced myself to VR gaming. I bought an Oculus Quest 2, which I really recommend if you want to start. Some titles I would definitely recommend are Superhot VR, Arizona Sunshine, Synth Riders, The Climb, Population: One, Beat Saber, and After The Fall.&lt;/p&gt;
&lt;h2&gt;Another one, with the best music 🎧&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://open.spotify.com/playlist/3BcoT8k0wjINDe1xF9VU8l?si=302de71e66794df2&quot;&gt;The sound of 2021&lt;/a&gt; was really great. You know that I’m making mixtapes for creative minds, like a 15 year-old who lives in the 80s, right? Well &lt;a href=&quot;https://nicotsou.com/music-for-creative-minds/&quot;&gt;now you know&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;Some of the albums I was playing on repeat this year; London Grammar, The Avener, Monolink, Roosevelt, Rüfüs Du Sol, Jungle, Jack Savoretti, Joel Culpepper, Foster The People, and of course Mandrugada.&lt;/p&gt;
&lt;p&gt;I can’t wait for the live stages to open again. It’s goin’ to be legen-, wait for it…&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;That’s all folks! Let’s see what 2022 will bring us. Until next year, stay safe, stay curious 🙂&lt;/p&gt;
&lt;p&gt;My inspiration for this blog post was the excellent retrospective by &lt;a href=&quot;https://www.taniarascia.com/2021-into-2022/&quot;&gt;Tania Rascia&lt;/a&gt;. You should check out her blog, it’s awesome!&lt;/p&gt;
&lt;p&gt;Cover Credit: &lt;a href=&quot;https://unsplash.com/photos/tsMVvBbpyGM&quot;&gt;Kazuend&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Master Interfaces in TypeScript]]></title><description><![CDATA[It's time to talk about TypeScript interfaces, once and forever.]]></description><link>http://www.nicotsou.com/tltr-typescript-interfaces/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-interfaces/</guid><pubDate>Wed, 29 Dec 2021 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;Interfaces help us define the shape of our object structures.&lt;/p&gt;
&lt;p&gt;An interface can act as a blueprint for a class, allowing us to add common functionality to unrelated objects. We use this feature to organize our code, by making it easier to follow and scale.&lt;/p&gt;
&lt;p&gt;In TypeScript, interfaces can be used as type annotations, similar to &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;type&lt;/code&gt; aliases, and other built-in types. This helps us guard our code against types. This strict type system protects us from common errors, which eventually speeds up our development times.&lt;/p&gt;
&lt;p&gt;It also helps our code editor to have a better understanding of how our code is interconnected, and provides much more sophisticated auto-completion suggestions.&lt;/p&gt;
&lt;p&gt;In the following paragraphs I’m going to explain in detail what an &lt;code class=&quot;language-text&quot;&gt;interface&lt;/code&gt; is and how you can use them in your code. I will give you some practical use cases, which will help you recognize where you have to use interfaces in your code. Before I leave you, I will explain the concept of declaration merging, which is one unique characteristic of interfaces.&lt;/p&gt;
&lt;p&gt;🍦Soft Ice cream?&lt;/p&gt;
&lt;h2&gt;Why the hell I need another keyword?&lt;/h2&gt;
&lt;p&gt;Consider the following &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; object:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Queen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In a traditional JavaScript project, that’s a very convenient way to define objects. This specific one contains everything we need to know about a music track. Such object structures are created on the fly, as we interact with the different modules and packages of our application.&lt;/p&gt;
&lt;p&gt;Objects can store anything. An api response. An event payload. Some configuration parameters. Information about styling, animations and many more. The simplest way to deal with such object structures is to create classes with all your models. But what about objects you can’t control, such as browser event payloads?&lt;/p&gt;
&lt;p&gt;In the beginning, it didn’t cost anything to come up with object structures like these, so we went for it. As we go along we are adding more and more object structures. As the codebase grows, some of these object structures &lt;em&gt;may&lt;/em&gt; already be defined somewhere else in our project. And you know what they say, duplication should be avoided.&lt;/p&gt;
&lt;p&gt;Most of the times, it is difficult to recognize such duplications. You see, I may want to name the title of the track &lt;code class=&quot;language-text&quot;&gt;title&lt;/code&gt;, but maybe you had another naming convention in mind. Maybe you disagree with the schema of the response, or maybe you want to use underscores, for reasons unknown. And then another person joins the team and suddenly we have three different names. Now we have to collaborate with another team, which has three more different naming schemes for the same concept.&lt;/p&gt;
&lt;p&gt;We need a way to be able to quickly form data shapes, without implementing anything about them. That’s an interface. It describes the shape of these objects.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/cbd9539826fbfe133dddc129f4452920/3acf0/stylish-smiling-woman-in-retro-vintage-cafe-2021-08-30-16-39-51-utc.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAAUBAgME/8QAFgEBAQEAAAAAAAAAAAAAAAAAAAED/9oADAMBAAIQAxAAAAFtmrtHaSMf/8QAGhAAAgMBAQAAAAAAAAAAAAAAAQIAERIDIv/aAAgBAQABBQKwJpWlx22ebeEAI//EABkRAAMAAwAAAAAAAAAAAAAAAAABAgMRIf/aAAgBAwEBPwFxzQ8dH//EABURAQEAAAAAAAAAAAAAAAAAAAAR/9oACAECAQE/AUf/xAAbEAACAQUAAAAAAAAAAAAAAAAAAgEQESFRYf/aAAgBAQAGPwImi36KZ2f/xAAcEAACAgIDAAAAAAAAAAAAAAABEQAhMVGBkaH/2gAIAQEAAT8h24LBhFPcNpQOUhecdOGZC2Hs/9oADAMBAAIAAwAAABA0D//EABgRAQADAQAAAAAAAAAAAAAAABEAASEx/9oACAEDAQE/EBceSlZk/8QAFxEAAwEAAAAAAAAAAAAAAAAAARARIf/aAAgBAgEBPxCU4v8A/8QAGxABAQACAwEAAAAAAAAAAAAAAREAITFBYZH/2gAIAQEAAT8Qm0D15cQQsCPo8wqnGPOTSOgjgYGoRdVsGUhioU0IM//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Stylish smiling woman in retro vintage cafe&quot;
        title=&quot;&quot;
        src=&quot;/static/cbd9539826fbfe133dddc129f4452920/9ecec/stylish-smiling-woman-in-retro-vintage-cafe-2021-08-30-16-39-51-utc.jpg&quot;
        srcset=&quot;/static/cbd9539826fbfe133dddc129f4452920/5d001/stylish-smiling-woman-in-retro-vintage-cafe-2021-08-30-16-39-51-utc.jpg 263w,
/static/cbd9539826fbfe133dddc129f4452920/7349d/stylish-smiling-woman-in-retro-vintage-cafe-2021-08-30-16-39-51-utc.jpg 525w,
/static/cbd9539826fbfe133dddc129f4452920/9ecec/stylish-smiling-woman-in-retro-vintage-cafe-2021-08-30-16-39-51-utc.jpg 1050w,
/static/cbd9539826fbfe133dddc129f4452920/39d31/stylish-smiling-woman-in-retro-vintage-cafe-2021-08-30-16-39-51-utc.jpg 1575w,
/static/cbd9539826fbfe133dddc129f4452920/3acf0/stylish-smiling-woman-in-retro-vintage-cafe-2021-08-30-16-39-51-utc.jpg 2000w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;What is an interface?&lt;/h2&gt;
&lt;p&gt;Historically, this was the main reason to use an interface. Classical object oriented programming languages, such as Java, and C#, also have interfaces that work similarly. This concept is similar to the &lt;em&gt;protocols&lt;/em&gt; of Objective-C and Swift. But JavaScript is a dynamic language. The concept of interfaces has evolved to be another way of guarding variables with type annotations.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;An interface describes the shape of an object structure.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;An interface is like a set of instructions that describe the data and the behavior of an object. You can’t implement its methods; you can only describe their signature.&lt;/p&gt;
&lt;p&gt;We can use interfaces as any other type annotation, to help TypeScript understand what is expected for a specific object variable. TypeScript will enforce a strict policy on that variable, to make sure that the instructions you set upfront are met. It will check the names of its properties, their types, and their restrictions. It will also check the names of the method functions, along with all their associated parameters and their return values.&lt;/p&gt;
&lt;p&gt;An interface can also be used to define the shape of a class. In the previous article we saw that a class is a &lt;em&gt;blueprint&lt;/em&gt; for an object. In a similar way, an interface acts as a blueprint for classes. Classes &lt;em&gt;implement&lt;/em&gt; interfaces, which means they strictly adhere to the shape of them.&lt;/p&gt;
&lt;h2&gt;Creating &amp;#x26; consuming interfaces&lt;/h2&gt;
&lt;p&gt;Let’s define an interface about a music track:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IsTrack&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// required properties&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// optional properties&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  genres&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The interface &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt;, defines the expected shape of these objects. That includes properties and their associated types. The properties &lt;code class=&quot;language-text&quot;&gt;releasedAt&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;genres&lt;/code&gt; are optional. No big surprises here compared to what we know about other types, right?&lt;/p&gt;
&lt;p&gt;Now, imagine the following JSON structure, as an api response:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-json line-numbers&quot;&gt;&lt;code class=&quot;language-json&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;title&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Bohemian Rhapsody&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;artist&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Queen&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;isFavorite&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;title&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Dream On&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;artist&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Aerosmith&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;isFavorite&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can store this response using our newly defined interface:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; tracks&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IsTrack&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; apiResponse&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see, we used an interface similar to how we would have used a type.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Interfaces can also contain function signatures:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IsTrack&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  genres&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token function&quot;&gt;setFavorite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By adding a &lt;code class=&quot;language-text&quot;&gt;setFavorite()&lt;/code&gt; method to our &lt;code class=&quot;language-text&quot;&gt;IsTrack&lt;/code&gt; interface, the previous response becomes incompatible. The consumers of this interface &lt;em&gt;must&lt;/em&gt; implement these additional two functions to modify favorites.&lt;/p&gt;
&lt;p&gt;As we’ve seen, interfaces become useful when multiple teams are working on the same project. A team may decide to name their properties differently. By creating interfaces and by storing them in a central place, you are avoiding this mess. You all know what a &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; is and what it needs to deal with it. The other team can now develop their part, without having any working code from your side. They just depend on the interface you have provided.&lt;/p&gt;
&lt;p&gt;You can export interfaces to make them available outside your project:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// track.ts&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;	id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This can become very useful when you are creating a shared library. Now, we can import this interface and use it:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// another.ts&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Track &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./track&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;TrackList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tracks&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Yeah, I know. I have an issue with the music tracks in this series. I am going through a phase. It will pass. I just need some time with myself.&lt;/p&gt;
&lt;h2&gt;Where can I use interfaces?&lt;/h2&gt;
&lt;p&gt;If you are wondering where and when to use interfaces in your projects, you are not the only one. This has bugged many JavaScript developers, including the author. You see, we never had to deal with classical OOP concepts.&lt;/p&gt;
&lt;p&gt;TypeScript has added a lot of flexibility to its interfaces, to support the JavaScript language. In this section, we will review the most important use cases of using interfaces.&lt;/p&gt;
&lt;h3&gt;Using Interfaces to implement unrelated classes&lt;/h3&gt;
&lt;p&gt;So far, we have been using interfaces as types. But interfaces work great with classes. Mind the new keyword &lt;code class=&quot;language-text&quot;&gt;implements&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IsPlayable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I know what you are thinking. This class doesn’t implement our interface fully, and TypeScript will let us know about that. But that’s why we have the interface, right?&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/3da08c1d305eff318ba6946451a05afe/f4fb1/typescript-interface-error.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 31.558935361216733%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAABC0lEQVR42n3P206DQBAG4LUEWukNVm0pJ9EKu13OKgJlabFCCilQ6vs/jItpvNPkm2SS+f+LASJoLf4cz8/+beHM9tq0Uq4PGl9pfK3wP8u0VvlqyRUSV0rjgTwpJbYWQQOW4IRnJSGbJM3iJE5Jmue7bJvt8h3JSJaRKI4oz/f8wHdcx/N9ZBm6+CaCDixGva62SbQhoffq2TY9Bi90HBp0XIwtwzBNE0KILkxkoudHMRzK4lWv3tfEQimGEYYBhA5NQhOiizVGFFrDX9g2VnK4oGWF+XKF01b6eJf6RK6TeW7flbpQ6MLnn272Kn8YftbGnTHpnrjjA9euuKPGNhLTyqPufwrTqmzzDUg4Oq/1lU6+AAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-interface-error.png&quot;
        title=&quot;&quot;
        src=&quot;/static/3da08c1d305eff318ba6946451a05afe/e996b/typescript-interface-error.png&quot;
        srcset=&quot;/static/3da08c1d305eff318ba6946451a05afe/17741/typescript-interface-error.png 263w,
/static/3da08c1d305eff318ba6946451a05afe/52211/typescript-interface-error.png 525w,
/static/3da08c1d305eff318ba6946451a05afe/e996b/typescript-interface-error.png 1050w,
/static/3da08c1d305eff318ba6946451a05afe/087e3/typescript-interface-error.png 1575w,
/static/3da08c1d305eff318ba6946451a05afe/f4fb1/typescript-interface-error.png 1766w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;We can combine multiple interfaces to mix and match the functionality we want to inherit.&lt;/p&gt;
&lt;p&gt;Consider the following interfaces:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IsTrack&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// Optional properties&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  genres&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IsPlayable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IsFavorable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;toggleFavorite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;IsTrack&lt;/code&gt; interface describes the shape of a track object. It’s similar to a model in MVC. The interface &lt;code class=&quot;language-text&quot;&gt;IsPlayable&lt;/code&gt; marks an object as playable, which means we expect these objects to have a &lt;code class=&quot;language-text&quot;&gt;duration&lt;/code&gt; and a &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; method that we can call. Similarly, the interface IsFavorable describes classes that provide this functionality to get the favorite boolean and &lt;code class=&quot;language-text&quot;&gt;toggle&lt;/code&gt; its value using the &lt;code class=&quot;language-text&quot;&gt;toggleFavorite()&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;Now let’s put these interfaces together to create our &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; class:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IsTrack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; IsFavorable&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; IsPlayable &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
  duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; id
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; title
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; artist
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isFavorite &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; isFavorite
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; duration
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;toggleFavorite&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; newValue &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isFavorite
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isFavorite &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newValue
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; newValue
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;playing...&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example, we are combining all three interfaces. Try this example by yourself and notice the TypeScript errors as you add more functionality to your class. Also, notice how your editor provides better auto-completion, as you define the expected class members.&lt;/p&gt;
&lt;h3&gt;Using interfaces to define the shape of object structures&lt;/h3&gt;
&lt;p&gt;The nature of JS is open. Data comes from multiple sources. Maybe you are consuming the YouTube api and you want to deal with its response. Maybe the user clicks on a button, or types something in an input. Maybe you want to call a helper function and pass a collection of these entities.&lt;/p&gt;
&lt;p&gt;You can create an interface next to your code to annotate this specific part of this interaction.&lt;/p&gt;
&lt;p&gt;Consider the following example, where we want to fetch some music tracks using the &lt;code class=&quot;language-text&quot;&gt;fetch&lt;/code&gt; api:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; mixtape&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; IsTrack&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;http://example.com/tracks.json&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;response&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; response&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;mixtape &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; data&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we use the &lt;code class=&quot;language-text&quot;&gt;IsTrack&lt;/code&gt; interface we declared above, to annotate the type of the &lt;code class=&quot;language-text&quot;&gt;mixtape&lt;/code&gt;. We know this object pretty well, because it’s a part of our application’s business logic.&lt;/p&gt;
&lt;p&gt;You also have the option to declare these interfaces next to your code, to deal with object structures that don’t align with your business logic. Handling user input is a good example here. Usually, we use event handlers to deal with all these use cases. And usually these events come with a &lt;em&gt;payload&lt;/em&gt;. That’s a specific object structure. We can use an interface to describe the shape of this payload, so that we know what is expected.&lt;/p&gt;
&lt;p&gt;In a similar way, you can use interfaces to define the expected structure of a function argument:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;FormValues&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  username&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  password&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; formData&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; FormValues &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  username&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; document&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;usernameField&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  password&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; document&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;passwordField&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;postToServer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;formData&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we have created an interface that contains a &lt;code class=&quot;language-text&quot;&gt;username&lt;/code&gt; and a &lt;code class=&quot;language-text&quot;&gt;password&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/2fb7698a625769f971ca66bb9dfe2354/3acf0/friends-enjoying-the-music-2021-08-27-00-02-22-utc.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 53.2319391634981%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAME/8QAFAEBAAAAAAAAAAAAAAAAAAAAAf/aAAwDAQACEAMQAAABuwXTaB//xAAYEAADAQEAAAAAAAAAAAAAAAABAgMABP/aAAgBAQABBQJasqpShWQoFkSSu4iTD//EABURAQEAAAAAAAAAAAAAAAAAABAR/9oACAEDAQE/Aaf/xAAVEQEBAAAAAAAAAAAAAAAAAAAQEf/aAAgBAgEBPwGH/8QAHRAAAgICAwEAAAAAAAAAAAAAAAECESFxAzFRgf/aAAgBAQAGPwJRd/SK7wyrvZxW/SOmZd5P/8QAGhABAAMBAQEAAAAAAAAAAAAAAQARITFh0f/aAAgBAQABPyHYeWLp8l8BNy9uNwaPe5cQ70zGc+DLmytq3P/aAAwDAQACAAMAAAAQTw//xAAWEQEBAQAAAAAAAAAAAAAAAAABEQD/2gAIAQMBAT8QlZMLv//EABcRAQADAAAAAAAAAAAAAAAAAAABETH/2gAIAQIBAT8QxaX/xAAcEAACAgIDAAAAAAAAAAAAAAABEQAhQZExweH/2gAIAQEAAT8QTpEP4bHYgTKI7A1qoBIcITYA9y8DcwuvY6jNijAZU00WMmf/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;friends enjoying the music&quot;
        title=&quot;&quot;
        src=&quot;/static/2fb7698a625769f971ca66bb9dfe2354/9ecec/friends-enjoying-the-music-2021-08-27-00-02-22-utc.jpg&quot;
        srcset=&quot;/static/2fb7698a625769f971ca66bb9dfe2354/5d001/friends-enjoying-the-music-2021-08-27-00-02-22-utc.jpg 263w,
/static/2fb7698a625769f971ca66bb9dfe2354/7349d/friends-enjoying-the-music-2021-08-27-00-02-22-utc.jpg 525w,
/static/2fb7698a625769f971ca66bb9dfe2354/9ecec/friends-enjoying-the-music-2021-08-27-00-02-22-utc.jpg 1050w,
/static/2fb7698a625769f971ca66bb9dfe2354/39d31/friends-enjoying-the-music-2021-08-27-00-02-22-utc.jpg 1575w,
/static/2fb7698a625769f971ca66bb9dfe2354/3acf0/friends-enjoying-the-music-2021-08-27-00-02-22-utc.jpg 2000w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Naming Conventions&lt;/h2&gt;
&lt;p&gt;Interfaces in TypeScript can be used similar to any other type annotation. To avoid name conflicts, the community is used to add a prefix to all the interfaces.&lt;/p&gt;
&lt;p&gt;There is no standard for the naming conventions by the language itself. So, TypeScript will not limit you from choosing any name for your interfaces. It is highly recommended, though, to decide on a naming convention and then use it in your project.&lt;/p&gt;
&lt;p&gt;I have seen projects that use the letter &lt;code class=&quot;language-text&quot;&gt;I&lt;/code&gt;, for example &lt;code class=&quot;language-text&quot;&gt;ITrack&lt;/code&gt;. I have also seen projects with the words &lt;code class=&quot;language-text&quot;&gt;Is&lt;/code&gt; , &lt;code class=&quot;language-text&quot;&gt;With&lt;/code&gt;, or &lt;code class=&quot;language-text&quot;&gt;Has&lt;/code&gt;, for example, we can have &lt;code class=&quot;language-text&quot;&gt;IsTrack&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;WithId&lt;/code&gt;, or &lt;code class=&quot;language-text&quot;&gt;HasFileExtentions&lt;/code&gt; accordingly. I have seen projects that use a suffix &lt;code class=&quot;language-text&quot;&gt;-able&lt;/code&gt;, like &lt;code class=&quot;language-text&quot;&gt;Comparable&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;Searchable&lt;/code&gt;, to add some unique characteristics to their classes or objects in general.&lt;/p&gt;
&lt;p&gt;I have seen projects that don’t use any specific naming convention. For example, if your codebase uses functional programming principles, you probably don’t use the &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt; keyword that often. That leaves you with a flexible namespace, because now you don’t have to separate classes from interfaces.&lt;/p&gt;
&lt;p&gt;I have also seen projects that use a combination of the naming conventions above.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;In a perfect world, you agree with your team to use a specific name convention, and you follow it with all your heart.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;Declaration Merging&lt;/h2&gt;
&lt;p&gt;The TypeScript implementation of interfaces differs from other programming languages. &lt;strong&gt;Declaration merging&lt;/strong&gt; allows you to declare interfaces with the same name multiple times.&lt;/p&gt;
&lt;p&gt;Here’s an illustration of this feature:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ITrack&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ITrack&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By declaring the same interface with different members, we are basically creating a bigger one:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ITrack&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;TypeScript will merge the previous declarations into one single &lt;code class=&quot;language-text&quot;&gt;ITrack&lt;/code&gt; interface. Of course, it will complain in case you try to define the same member, with a different type.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;This concept of interfaces sets them apart from type aliases, which &lt;em&gt;must&lt;/em&gt; have unique names across your project:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/2f77ddcf8d02ed1515fa5ca15a86412d/21482/typescript-duplicate-type-alias.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 44.106463878327%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAABPElEQVR42nWRCU/CQBCFl0NNE00kXN02WEr37gW0tBSKgUhUpBQ1Ev//P3GF4MEx+TLJTPIy72WACnLz6iW0FoE1GiOPtSJTDTvwF0sL7m5mTbBSQXYAUMHaVBau0/V8t9fzXd9BxMIU7cGEd4xa0gT5CTGUl5WFw/ucUkFkCUwYlqI9hGKjOj4jLq5h852LKHa6qWvGLmGUIoQlGBOECCaWUR2dFmulV7X6Jmzfth3ObVdwzqQJxpn0wShhQrB27cxlvZRrlw9REKfDKAnCYRKP0ySKw133u14Y+whOZDqtnGmlfwBT2dDKZq7Ppvp0UE/t9n1oPwfiqc8fY2/JjYmlDzqVeaucty5WBwC9vGbKR+96I24/64VcvqQBsh11sNx+SC6lyeUx37ZhIWsUMlhcwW0SOf5wnPNv5i9bPlEYzDfcMgAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-duplicate-type-alias&quot;
        title=&quot;&quot;
        src=&quot;/static/2f77ddcf8d02ed1515fa5ca15a86412d/e996b/typescript-duplicate-type-alias.png&quot;
        srcset=&quot;/static/2f77ddcf8d02ed1515fa5ca15a86412d/17741/typescript-duplicate-type-alias.png 263w,
/static/2f77ddcf8d02ed1515fa5ca15a86412d/52211/typescript-duplicate-type-alias.png 525w,
/static/2f77ddcf8d02ed1515fa5ca15a86412d/e996b/typescript-duplicate-type-alias.png 1050w,
/static/2f77ddcf8d02ed1515fa5ca15a86412d/21482/typescript-duplicate-type-alias.png 1350w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In general, I would prefer interfaces over types, when it’s possible. Use type aliases when your objects could be &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;, or if they can act as a different type, like for example a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Cover Credit: &lt;a href=&quot;https://unsplash.com/photos/LpbyDENbQQg&quot;&gt;Sebastian Svenson&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Master Classes in TypeScript]]></title><description><![CDATA[Discover how TypeScript helps us create classes and how it transforms JavaScript into a more traditional OOP experience.]]></description><link>http://www.nicotsou.com/tltr-typescript-classes/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-classes/</guid><pubDate>Wed, 22 Dec 2021 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;Since the very early versions of TypeScript, the language provided support for the &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt; keyword, together with some additional features, that you find in classic object-oriented languages.&lt;/p&gt;
&lt;p&gt;In this article we will discover how TypeScript helps us create classes and how it transforms JavaScript into a more traditional OOP experience. I will explain how to add types and how to implement inheritance, enforce private and protected members, add accessors and abstract classes. We will then talk about the structural type system of TypeScript, which differs from the other flavors of OOP, you may probably be familiar with.&lt;/p&gt;
&lt;p&gt;But first, let’s revisit what we’ve learned about classes and how JavaScript differs from it. This will help you have a better understanding of what’s going on.&lt;/p&gt;
&lt;p&gt;Sundae? 🍧&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;Let’s talk about objects, first&lt;/h2&gt;
&lt;p&gt;There are multiple ways to create an object in JavaScript. Here’s the most convenient and common way:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Queen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, in contrast to other languages, nobody stops me from editing this object by adding or changing its properties:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;timesPlayed&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Nobody except TypeScript of course. Here’s the same example, after adding types:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
	releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Queen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Type error: not a string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;timesPlayed&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;22&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// Property &apos;timesPlayed&apos; does not exist on type &apos;Track&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And here’s the error I will get when I try to set a value to a property that doesn’t exist:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/47a204d3861663f2684b43c622b841ef/3e096/typescript-wrong-property-name.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 27.376425855513308%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA+0lEQVR42mWPS0+DQBRGh0eF6VAoj2EYKEIFWirF0iZW40KXVh7a1qQPjQv//59wkLgyOTk5d/EtLiC9/Wj4dYPOS/28Nj7nxsdUPfjiOwU7hvvrf70loGYAh39dhd/T4DmgK88qqLnw8EKT8wvuWuYzic9kIZP/DIWcGQkF4ap27PW2qbYndmaSGCqeThKLpjqJDCfW8HhoXw3MgJ0Dq7WkuorpK2qEwYaABvjimy/WvrGe0btQXwbhfZw8BZcPUfRI3VvYT/soRcqMBbMMJxBNEMxtULbjEdck0m5uHwvnlOPDWGkcsSJ8RcWa8KUFNh24M/eC2y67n38AwDAlT3lgvFAAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-wrong-property-name.png&quot;
        title=&quot;&quot;
        src=&quot;/static/47a204d3861663f2684b43c622b841ef/e996b/typescript-wrong-property-name.png&quot;
        srcset=&quot;/static/47a204d3861663f2684b43c622b841ef/17741/typescript-wrong-property-name.png 263w,
/static/47a204d3861663f2684b43c622b841ef/52211/typescript-wrong-property-name.png 525w,
/static/47a204d3861663f2684b43c622b841ef/e996b/typescript-wrong-property-name.png 1050w,
/static/47a204d3861663f2684b43c622b841ef/087e3/typescript-wrong-property-name.png 1575w,
/static/47a204d3861663f2684b43c622b841ef/3e096/typescript-wrong-property-name.png 1718w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Brilliant! Now we can relax because our objects will follow a structure. There are rules in the game. TypeScript acts again as a guard against typos or common mistakes.&lt;/p&gt;
&lt;p&gt;Although, this adds a lot, we still have the problem of how to organize our code. You see, types are perfect for protecting us from errors, but they simply don’t exist at runtime. TypeScript will remove those types altogether, when it will compile our code to JavaScript.&lt;/p&gt;
&lt;p&gt;We need a better tool to define object structures and their relationships.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/800731fcf55a62c66b9879d8e0ecc945/93719/debby-hudson-RXKERUSGN8U-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAMBAgX/xAAVAQEBAAAAAAAAAAAAAAAAAAAAAf/aAAwDAQACEAMQAAAB1VNvAQH/xAAYEAACAwAAAAAAAAAAAAAAAAAAERIgIf/aAAgBAQABBQJk8p//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAYEAACAwAAAAAAAAAAAAAAAAAAASAhMf/aAAgBAQAGPwIdY4//xAAaEAEAAgMBAAAAAAAAAAAAAAABEBEAITFR/9oACAEBAAE/IdEPcvaPhBP/2gAMAwEAAgADAAAAEGAv/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPxA//8QAFhEAAwAAAAAAAAAAAAAAAAAAARAR/9oACAECAQE/EIV//8QAGhABAAMAAwAAAAAAAAAAAAAAAQARIRBBYf/aAAgBAQABPxBxC+I3oJVp3ZfHqLs//9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;images/debby-hudson-RXKERUSGN8U-unsplash.jpg&quot;
        title=&quot;&quot;
        src=&quot;/static/800731fcf55a62c66b9879d8e0ecc945/9ecec/debby-hudson-RXKERUSGN8U-unsplash.jpg&quot;
        srcset=&quot;/static/800731fcf55a62c66b9879d8e0ecc945/5d001/debby-hudson-RXKERUSGN8U-unsplash.jpg 263w,
/static/800731fcf55a62c66b9879d8e0ecc945/7349d/debby-hudson-RXKERUSGN8U-unsplash.jpg 525w,
/static/800731fcf55a62c66b9879d8e0ecc945/9ecec/debby-hudson-RXKERUSGN8U-unsplash.jpg 1050w,
/static/800731fcf55a62c66b9879d8e0ecc945/39d31/debby-hudson-RXKERUSGN8U-unsplash.jpg 1575w,
/static/800731fcf55a62c66b9879d8e0ecc945/1f368/debby-hudson-RXKERUSGN8U-unsplash.jpg 2100w,
/static/800731fcf55a62c66b9879d8e0ecc945/93719/debby-hudson-RXKERUSGN8U-unsplash.jpg 4000w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/RXKERUSGN8U&quot;&gt;Debby Hudson&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;What is a class?&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;A class is a blueprint for an object.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Think of it as a higher-level data type, similar to how you use &lt;code class=&quot;language-text&quot;&gt;const&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;let&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; x&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Similarly, you can use a &lt;em&gt;class&lt;/em&gt; to create objects that have a specific structure:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;MyClass myObject&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example we have a class &lt;code class=&quot;language-text&quot;&gt;MyClass&lt;/code&gt; and we instantiate the object &lt;code class=&quot;language-text&quot;&gt;myObject&lt;/code&gt;. Note that classes, by convention, start with a capital letter (PascalCase) but objects, like any other variable by convention, start with a small letter (camelCase).&lt;/p&gt;
&lt;p&gt;Matt Weisfeld, in his book “The Object-Oriented Thought Process”, resembles a class as a cookie cutter. You take the cookie dough and you use the cookie cutter to make the cookies. That’s how you can use a &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt; to &lt;em&gt;instantiate&lt;/em&gt; objects.&lt;/p&gt;
&lt;p&gt;Classes help us organize our code. Instead of having your code in a bunch of functions and variables, that are unrelated to each other, you can structure everything into a &lt;em&gt;class&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;In most programming languages, a &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt; is used to create an &lt;em&gt;object&lt;/em&gt;. It describes the contents of objects that belong to it. The data fields that it contains, and its operations. We call those &lt;em&gt;properties&lt;/em&gt; and &lt;em&gt;methods&lt;/em&gt; accordingly.&lt;/p&gt;
&lt;h2&gt;The ugly truth&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;As you may already know, there are no classes in JavaScript.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It is a prototype-based object oriented language. This basically means that any object in JavaScript inherits properties and methods from a &lt;em&gt;prototype&lt;/em&gt;. This prototype could be another object, or one of the built-in objects.&lt;/p&gt;
&lt;p&gt;This maximizes code reuse. Think of it like a traditional class inheritance, but without the necessity of defining everything upfront.&lt;/p&gt;
&lt;p&gt;It’s easy to follow a prototype chain. Just try to get the prototype of a &lt;code class=&quot;language-text&quot;&gt;date&lt;/code&gt; variable:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; date &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
Object&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getPrototypeOf&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;date&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// returns a reference to Date&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Up until now, in JavaScript, you couldn’t define the relationships of the objects upfront using a class. You &lt;em&gt;had&lt;/em&gt; to create objects that inherit from other objects. There was a need to have a more sophisticated way to create objects with a given structure. That’s why JavaScript developers were used to leverage plain functions to create objects. They worked exactly as &lt;em&gt;constructors&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Take a moment to review the functional constructor below:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Track&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; artist&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; releasedAt&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// set default values&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Untitled&apos;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Unknown Artist&apos;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;releasedAt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Unknown Release Date&apos;&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// assign values passed via arguments&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; title
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; artist
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;releasedAt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; releasedAt
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Track
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It’s quite an inconvenient way to write code, don’t you think?&lt;/p&gt;
&lt;p&gt;Since ES6, we can simply use the &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt; keyword to define classes:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Untitled&apos;&lt;/span&gt;
  artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Unknown Artist&apos;&lt;/span&gt;
  releasedAt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Unknown Release Date&apos;&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; artist&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; releasedAt&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; title
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; artist
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;releasedAt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; releasedAt
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is just a syntactic sugar. In the background it &lt;em&gt;still&lt;/em&gt; works exactly as before.&lt;/p&gt;
&lt;h2&gt;TypeScript to the rescue&lt;/h2&gt;
&lt;p&gt;TypeScript was one of the first languages that provided support for creating classes. Any valid JS &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt; declaration is valid in TypeScript as well.&lt;/p&gt;
&lt;p&gt;Depending on the compiler configuration, you have to define types for the class members.&lt;/p&gt;
&lt;p&gt;Remember the previous example? Let’s add some types on it:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Untitled&apos;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Unknown Artist&apos;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Unknown Release Date&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; title
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; artist
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;releasedAt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; releasedAt
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here’s how we can create an object from our class, by consuming its constructor:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Queen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you may have noticed, we don’t explicitly specify a type for the &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; object. TypeScript will automatically set that this is an instance of &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt;. There is a way to explicitly define the type, though:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Queen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Many developers prefer this way, because it’s much more readable. If you use a function that returns an object, for example, you will miss the information in your head what kind of object our &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; was. It’s a detail, I know. But it helps a lot!&lt;/p&gt;
&lt;p&gt;Now, let’s try to add a new property that doesn’t exist in the class definition:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Queen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;1975&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  newProp&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;with a value&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Yes, my friends, you guessed it correctly. We will get an error:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a22f5a6832831fb0b3f404ff90e0539c/3126c/typescript-property-dont-exist.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 52.851711026615966%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAABYlAAAWJQFJUiTwAAACFUlEQVR42o2T6W7aQBSFBwzBdjAYbLwSbBaDbWxK0paoilrURerKDlkaqjZ9/3c4vR4StVJbKT8+Hc8d657juWPWVPeI5C0G4hp2YYZWKWMOU5hBZ59hsC9oEOYjYan1DRP/J55XrjBWrnDqbDENL0k3CNUlb+4WZzBzWePM4GBi/K9hV71G0rxDv36DWNqiL2+Q6Cs8dXcY2zuM9A1SbQNfWuHkaAm3sICbm8EV5tzkr4aueAm/ukfP+I7Y3CNp3CIoruBJa3jlHbzCEj1adys7JOYtusoOPaoFZGzlZ/9ISJtnlWucte4Q1r5i7P5Av3aDlriBIyzppTmsHEF6OMsZr5lUszPYgmOxQ3PmlF8h9t8gaL9FMvgAz36JgNaD7juUSylKuQhyYQhJiDkPz2I+vq8NIQohqsI5N2WG/gyaHaBuBNDtAQw3gm4N6DlERWtDs/pQaj7XmhngWPWg6h3UGj3SLt8r6y2IxZAaLsA07RzR6AL9+ALpkymGKSUMXyBKDpphOilOvFNYbgrNIEMz5uuGNYTtjqDUO5Qy5p/OVDmBTo6aFqBq9KE7EVRKazQjKJSQHTVQkG2OkCFZnOKxc1DZARP13wlTe46J+4mux2uk7fc4TxYY+h/RMem+laco5ydQ6HwU4UHvyf+hhQlqwpQPjPnSDs3ihu7XGk2aeDNPU8st6C/JppZNcPkosnTZlH8BVOs0BVF84ewAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-property-dont-exist.png&quot;
        title=&quot;&quot;
        src=&quot;/static/a22f5a6832831fb0b3f404ff90e0539c/e996b/typescript-property-dont-exist.png&quot;
        srcset=&quot;/static/a22f5a6832831fb0b3f404ff90e0539c/17741/typescript-property-dont-exist.png 263w,
/static/a22f5a6832831fb0b3f404ff90e0539c/52211/typescript-property-dont-exist.png 525w,
/static/a22f5a6832831fb0b3f404ff90e0539c/e996b/typescript-property-dont-exist.png 1050w,
/static/a22f5a6832831fb0b3f404ff90e0539c/087e3/typescript-property-dont-exist.png 1575w,
/static/a22f5a6832831fb0b3f404ff90e0539c/3126c/typescript-property-dont-exist.png 1876w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;In JavaScript, when a variable doesn’t have a value, it’s &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;. The same applies for our class properties. We could leverage the usage of unions to allow &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt; values:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;&lt;/span&gt;  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; id&lt;/span&gt;    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; title
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; artist
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;releasedAt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; releasedAt
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We could also have optional properties with the &lt;code class=&quot;language-text&quot;&gt;?&lt;/code&gt; character:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    isFavorite&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; id
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; title
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; artist
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;releasedAt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; releasedAt
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isFavorite &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; isFavorite
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, the &lt;code class=&quot;language-text&quot;&gt;isFavorite&lt;/code&gt; flag can be completely omitted when we are instantiating object from this class. Basically, this means its value will be &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/0d4e4b5100f0239485cafd9da91ce51b/4dbb8/eugenia-kozyr-r1sTNKz0omE-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 69.58174904942965%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAOABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAQAD/8QAFQEBAQAAAAAAAAAAAAAAAAAAAQP/2gAMAwEAAhADEAAAAc0Z2ol//8QAGBAAAgMAAAAAAAAAAAAAAAAAABAREiH/2gAIAQEAAQUCWuLH/8QAFREBAQAAAAAAAAAAAAAAAAAAABH/2gAIAQMBAT8BR//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EAB0QAAICAQUAAAAAAAAAAAAAAAABESFhIjFBUbH/2gAIAQEABj8Czzk8NDSRHWxF3ZLk/8QAHRABAAICAgMAAAAAAAAAAAAAAQARMUEhUWGBsf/aAAgBAQABPyHF3SD0TYFXleOoDL6Gx4sXvZ+zhdHZuANxXNM//9oADAMBAAIAAwAAABCIz//EABoRAAICAwAAAAAAAAAAAAAAAAHwABExgbH/2gAIAQMBAT8Qoq6EBHHZ/8QAGBEAAwEBAAAAAAAAAAAAAAAAAAHwEWH/2gAIAQIBAT8Qx12xCTP/xAAcEAEAAgMAAwAAAAAAAAAAAAABABEhMUFRcdH/2gAIAQEAAT8QcusoPpIBYmuG0dwQTNE2PYthr5S9kWAIKrufECaLMd6xP//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;images/eugenia-kozyr-r1sTNKz0omE-unsplash.jpg&quot;
        title=&quot;&quot;
        src=&quot;/static/0d4e4b5100f0239485cafd9da91ce51b/9ecec/eugenia-kozyr-r1sTNKz0omE-unsplash.jpg&quot;
        srcset=&quot;/static/0d4e4b5100f0239485cafd9da91ce51b/5d001/eugenia-kozyr-r1sTNKz0omE-unsplash.jpg 263w,
/static/0d4e4b5100f0239485cafd9da91ce51b/7349d/eugenia-kozyr-r1sTNKz0omE-unsplash.jpg 525w,
/static/0d4e4b5100f0239485cafd9da91ce51b/9ecec/eugenia-kozyr-r1sTNKz0omE-unsplash.jpg 1050w,
/static/0d4e4b5100f0239485cafd9da91ce51b/39d31/eugenia-kozyr-r1sTNKz0omE-unsplash.jpg 1575w,
/static/0d4e4b5100f0239485cafd9da91ce51b/1f368/eugenia-kozyr-r1sTNKz0omE-unsplash.jpg 2100w,
/static/0d4e4b5100f0239485cafd9da91ce51b/4dbb8/eugenia-kozyr-r1sTNKz0omE-unsplash.jpg 3053w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/r1sTNKz0omE&quot;&gt;Eugenia Kozyr&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Structural type system&lt;/h2&gt;
&lt;p&gt;This is one of the most difficult concepts of TypeScript, for people who are not familiar with JavaScript. It’s the way the language checks the compatibility of the types.&lt;/p&gt;
&lt;p&gt;Here’s a simplified version of what is possible in TypeScript:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Queen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;1975&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we’re basically bypassing the constructor altogether. We are creating an object, using an object literal, and then we’re passing this object to a &lt;code class=&quot;language-text&quot;&gt;track&lt;/code&gt; variable. TypeScript will compare the shape of this object with the shape of the class &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt;. If there are no type violations in its methods, no errors will be thrown.&lt;/p&gt;
&lt;p&gt;Now let’s see a different example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Dog&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nobody&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Cat&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Nobody&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; cat &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Cat&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; dog &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Dog&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; animal&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Dog &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; cat&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here these two objects originate from completely different classes. We have a &lt;code class=&quot;language-text&quot;&gt;Cat&lt;/code&gt; and a &lt;code class=&quot;language-text&quot;&gt;Dog&lt;/code&gt;. But observe what is happening if I assign a cat to a dog. No errors whatsoever.&lt;/p&gt;
&lt;p&gt;Somebody will argue how good this approach really is. TypeScript has to be more flexible here, because JavaScript doesn’t have types. And strategically speaking, backwards compatibility is really crucial. That’s why they had to use a concept called duck typing. &lt;a href=&quot;https://en.wikipedia.org/wiki/Duck_typing&quot;&gt;Duck typing&lt;/a&gt; in computer programming is an application of the duck test:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“If it walks like a duck and it quacks like a duck, then it must be a duck”&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In the example above, both these objects have a &lt;code class=&quot;language-text&quot;&gt;name&lt;/code&gt; property, which is of course &lt;code class=&quot;language-text&quot;&gt;public&lt;/code&gt;. That makes them practically compatible.&lt;/p&gt;
&lt;h2&gt;Inheritance&lt;/h2&gt;
&lt;p&gt;In the following example, I’m declaring a &lt;code class=&quot;language-text&quot;&gt;Playable&lt;/code&gt; class, and I’m using it to create three additional classes that derive from it:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/fe2993c5cbb5e7015f275104048c5599/599ea/tltr-typescript-classes-Initial.drawio.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 59.31558935361216%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAACRElEQVR42mNgwAP+M4QynzFOY92UVs8106ee64pWKNsZY2NWBlLBfwYGRhB9XD9e4bhmgurasCqL1eGVtoeVYuWOaibJrwJaRJaBq2LL5fpaZkoiix3TChUCuZpEA/+DNW8LKFFekdxovd08XeGYTJz0Sa14iSPqSVL/Q4l3ISPQNMb/9QxMIM5O32LFpYn1SpsYfLiOqPvx7tfK4jmqFyM2Mw0ajv8hriYaPJIJ5QRGDNvitALJOcVZ8vcZ4jkuiMdww7xPEJSvKuev31Av1by+WrZ+Vb1c/bZ6meKd9WL1++slqndWK+bequerB/GX1Uu1rG2RrNzWKtp5pJMXIwJuqeSyH/FL4p1en6+wPzSLZ1FMDPeq/6uY59fXc2zySeOak5TEO7syT/wY0MX74+s5Vq1axbwKqO6IVRLvgsJ06f3+8QL35eM5wGEPDGTeG5KRImu8C5TmZDXbbTdOkzwhHS0DSnOX5KIEj+ski88ubjeendNiBgq/PYoR4iC5E3IxiseUY1TWxNa6bzNL1wLp2a8fL8BwRStZaFVKi8Gi5HqVo5YZYseA/PPA9AcKrx2+hSobg0v0TppmSewHYrCcapz0Bb0Y7i1eZVbLY+vVDgIdcEk3U/CoUozYGY1oSYZVFqGcR7TTlE+pR6sfUYhWP6AZqXpQLUaxnoGBaSfQRUfV4jWOqMQpn4HiQwahoiAXrgmqdFwHTFYXxAPFDgINOqGSInNQLlqSgVxwQiWX74xapMghlURRED6jlMa/KjSUGQCeu9XuOJBuqQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;tltr-typescript-classes-Initial.drawio.png&quot;
        title=&quot;&quot;
        src=&quot;/static/fe2993c5cbb5e7015f275104048c5599/e996b/tltr-typescript-classes-Initial.drawio.png&quot;
        srcset=&quot;/static/fe2993c5cbb5e7015f275104048c5599/17741/tltr-typescript-classes-Initial.drawio.png 263w,
/static/fe2993c5cbb5e7015f275104048c5599/52211/tltr-typescript-classes-Initial.drawio.png 525w,
/static/fe2993c5cbb5e7015f275104048c5599/e996b/tltr-typescript-classes-Initial.drawio.png 1050w,
/static/fe2993c5cbb5e7015f275104048c5599/087e3/tltr-typescript-classes-Initial.drawio.png 1575w,
/static/fe2993c5cbb5e7015f275104048c5599/599ea/tltr-typescript-classes-Initial.drawio.png 1926w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Here is how we can implement it in TypeScript:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newTitle
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newDuration
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;Playing &quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;...&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MusicTrack&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newArtist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newArtist
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Podcast&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  producer&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newProducer&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;producer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newProducer
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Movie&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  director&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDirector&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;director &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newDirector
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that I’m using the &lt;code class=&quot;language-text&quot;&gt;extends&lt;/code&gt; keyword, which is another feature of JavaScript.&lt;/p&gt;
&lt;p&gt;I can then instantiate a &lt;code class=&quot;language-text&quot;&gt;Movie&lt;/code&gt; object:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; myMovie &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Movie&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;The Godfather&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;175&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Francis Ford Coppola&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
myMovie&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Playing &quot;The Godfather&quot;...&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, let’s try to override the &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; method of the class &lt;code class=&quot;language-text&quot;&gt;Movie&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Movie&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  director&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDirector&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// calls the constructor of the parent class&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;director &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newDirector
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;Playing &quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot; by &quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;director&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;...&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And, as expected, when we call the &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; method, the message that we get is different:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; myMovie &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Movie&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;The Godfather&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;175&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Francis Ford Coppola&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
myMovie&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Playing &quot;The Godfather&quot; by &quot;Francis Ford Coppola&quot;...&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Private &amp;#x26; protected methods&lt;/h2&gt;
&lt;p&gt;Class properties and methods are kinda useless without specifying their privacy. In the previous example, I can directly change the values of the properties or call the &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; method, from anywhere in my code:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;myMovie&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;broken title&apos;&lt;/span&gt;
myMovie&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Playing &quot;broken title&quot; by &quot;Francis Ford Coppola&quot;...&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In a real-world application, you wouldn’t allow anyone to access our class members directly. In fact, this privacy is one of the key benefits of OOP.&lt;/p&gt;
&lt;p&gt;An improved version of our previous design can be the following. Our ultimate goal is to protect the properties:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/01f7155176804c7409ddb56b5cb1596b/599ea/tltr-typescript-classes-Final.drawio.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 59.31558935361216%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAACTklEQVR42mNgwAP2M9Sz/GeoZ1pVX8/WG1rIecY4jfU/gz0LA6ngPwMDI4g+qpkkf8ggXWtVYqP9utBqm+Py4QqnlBNkQZaQaibYwD1OeeJdXTOV65fNkaqfOZMLJHZMK1kI5FKyXLjbOk9uYVqb5dL4WuNlEVUme3SSxU/IxSiSYiDIIMZ6Bgawl/ZYpEvPz2nVWFDYK/1cPIZ7JxCfBQbDqtBQNmSfEA3OSKZx/WfQYluVmyi6PD9f4SlDGtcV0Sye/8QaVLqhlLd+f71AxZYKwYqFFcIguvR6J0hMon53vVLWlXqe+k31IvWr6iUql7WKV66qFK3fVs+HEV7bGHLZd8YUc0/uzJbaZJzGtQqYROr/1zPNj4/nAIYX15ykJN5p9cVix2RCOfcDxVatWsW8ySeNa799PAdIfBFQ7xkGY2CS+s/IcEIlmu+sbITUZu9S1fnp9Q579NOlT0klyNbX1zOdkwgVPaKeJDUnu9Nkfm6LxUGNNMmTGrHCt1Ry2UGRc1wxWXxpdqPlNvtcmXMqoaL79eMFGK5pZAuvC60yW5bUYrIsptF8vXu+wlmFJHlQWjtlkqm0NL7JeGFuu+F2oPgluSjBa0BD/zPEc2x3LzVakFyvtySlxWCXX6nUSalYYVCSYgB544h2mvJe7TjlIypxysfV4xWOKkTJgxO2XozYAYNI1cNasXJHNaPkT6jFKJ4zSBTdD8wtG4JLTeYUdJqsiqlXuauUxg9Se0wGaCA5ABTuF/RiuPdrZfHs1wrlAaWGTZI+XP9DQ5kBUBrVnGDnZ08AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;tltr-typescript-classes-Final.drawio.png&quot;
        title=&quot;&quot;
        src=&quot;/static/01f7155176804c7409ddb56b5cb1596b/e996b/tltr-typescript-classes-Final.drawio.png&quot;
        srcset=&quot;/static/01f7155176804c7409ddb56b5cb1596b/17741/tltr-typescript-classes-Final.drawio.png 263w,
/static/01f7155176804c7409ddb56b5cb1596b/52211/tltr-typescript-classes-Final.drawio.png 525w,
/static/01f7155176804c7409ddb56b5cb1596b/e996b/tltr-typescript-classes-Final.drawio.png 1050w,
/static/01f7155176804c7409ddb56b5cb1596b/087e3/tltr-typescript-classes-Final.drawio.png 1575w,
/static/01f7155176804c7409ddb56b5cb1596b/599ea/tltr-typescript-classes-Final.drawio.png 1926w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;TypeScript adds better support for private methods. It’s important to remember that the following keywords are only available during compilation, and they aren’t available at runtime. Nothing protects you from accessing their values. If you really need that protection, ECMAScript 2015 has support for private properties or methods. But I would say its syntax &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#field_declarations&quot;&gt;is quite weird&lt;/a&gt;. So use it only in rare cases, when it’s really important to protect your objects.&lt;/p&gt;
&lt;p&gt;By default, all class members are public. You can omit the &lt;code class=&quot;language-text&quot;&gt;public&lt;/code&gt; keyword. Here’s what we’ve meant in the previous example, when we were declaring the class &lt;code class=&quot;language-text&quot;&gt;Playable&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newTitle
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newDuration
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;Playing &quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;...&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To restrict access to our properties, we can use the &lt;code class=&quot;language-text&quot;&gt;private&lt;/code&gt; keyword:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newTitle
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newDuration
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;Playing &quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;...&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;An attempt to change the title will fail:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;myMovie&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;broken title&apos;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// Error: Property title is private and only accessible within class Playable&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That’s very convenient and secure. As a side effect, we can’t even access or change this property from within the derived classes. You will notice that now our Movie class, has a very interesting error:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/1ff172cfff928c479e9890a9fcdd9006/e7c5b/typescript-private-property-error.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 25.85551330798479%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA+0lEQVR42nWQXWuDMBhGU7Vqi7bWdqtWR63rV1ZwbDDYdW8GM1H2/3/MWWxlHYxdHN7kDTycJ2I9bJGuRoaafaAphh8kA0Ui/rLs+e/eIVaWppq2VP4XUmhOjkIONaXdUro1pfdJaTVszG47rsndhszWrHoypyG11IVroN3w6Cu2A83GsBctuXdmNZMspwdSM9PFiSSWxPGOaby/EEZbJtGOaH5gFBYE4ycTWCMSY1KMWjKv5d5V5H7D2j0bk4pk8kyxeKOYv3M3e8W3j3g/SDxHXs/OkcB5uRnmdk1XPbGVqaF4MPqZqZl2b8Y6M1+RmpmKpkf/4rbrAr8B10eKBy0NadkAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-private-property-error.png&quot;
        title=&quot;&quot;
        src=&quot;/static/1ff172cfff928c479e9890a9fcdd9006/e996b/typescript-private-property-error.png&quot;
        srcset=&quot;/static/1ff172cfff928c479e9890a9fcdd9006/17741/typescript-private-property-error.png 263w,
/static/1ff172cfff928c479e9890a9fcdd9006/52211/typescript-private-property-error.png 525w,
/static/1ff172cfff928c479e9890a9fcdd9006/e996b/typescript-private-property-error.png 1050w,
/static/1ff172cfff928c479e9890a9fcdd9006/087e3/typescript-private-property-error.png 1575w,
/static/1ff172cfff928c479e9890a9fcdd9006/8079d/typescript-private-property-error.png 2100w,
/static/1ff172cfff928c479e9890a9fcdd9006/e7c5b/typescript-private-property-error.png 2642w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;We can use the &lt;code class=&quot;language-text&quot;&gt;protected&lt;/code&gt; keyword, to allow accessing class properties, or methods, from derived classes:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;protected&lt;/span&gt; title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;protected&lt;/span&gt; duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A constructor can also be marked as &lt;code class=&quot;language-text&quot;&gt;protected&lt;/code&gt;. This means that the class cannot be instantiated outside its containing class, but can be extended.&lt;/p&gt;
&lt;p&gt;For example, if we set our constructor to &lt;code class=&quot;language-text&quot;&gt;protected&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;protected&lt;/span&gt; title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;protected&lt;/span&gt; duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newTitle&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newDuration&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; playableTrack &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// throws an error&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Which doesn’t allow me to access:&lt;/p&gt;
&lt;p&gt;It won’t be possible to instantiate new objects from the &lt;code class=&quot;language-text&quot;&gt;Playable&lt;/code&gt; class:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/2939010eb94140e712553d387faea150/11f80/typescript-protected-class-member.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 22.813688212927755%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA8ElEQVR42m2QyU7DMBRFH0kV6syjieOgdlFKK7EIHQCxQbAAQZLC///M5WVAzYLF0bm+svWeTDk1mCLpC1dUM83ouu//y2eGt4pa0BC6ooW8fIMunqDkI2S0Q8bI5IjIq5B2OT0iDXdIon3fp+ysI9wj5jtzcw2SPEH2m/0gtZ5xna2gkjWc4g5xvoEMFnCdAq5dILI1PLYv2G6JUBQQtoIhJIx5BrJi0Ep848Z6Zz6xsD+w9Q+4DQ7Is1fo5AVLdiIe4BkVAvMeHuOP/J0ds+pxZxVImy1Ko4ZmSvOE5ayjhbpoxs2H71B0Yp9RE0/5BdZrhj3l/tDyAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-protected-class-member.png&quot;
        title=&quot;&quot;
        src=&quot;/static/2939010eb94140e712553d387faea150/e996b/typescript-protected-class-member.png&quot;
        srcset=&quot;/static/2939010eb94140e712553d387faea150/17741/typescript-protected-class-member.png 263w,
/static/2939010eb94140e712553d387faea150/52211/typescript-protected-class-member.png 525w,
/static/2939010eb94140e712553d387faea150/e996b/typescript-protected-class-member.png 1050w,
/static/2939010eb94140e712553d387faea150/087e3/typescript-protected-class-member.png 1575w,
/static/2939010eb94140e712553d387faea150/8079d/typescript-protected-class-member.png 2100w,
/static/2939010eb94140e712553d387faea150/11f80/typescript-protected-class-member.png 2498w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;We can use the &lt;code class=&quot;language-text&quot;&gt;readonly&lt;/code&gt; keyword to define properties that behave as constants:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;readonly&lt;/span&gt; title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;readonly&lt;/span&gt; duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/span&gt;	&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Properties that are marked as &lt;code class=&quot;language-text&quot;&gt;readonly&lt;/code&gt;, cannot change, even within derived classes:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Movie&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  director&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDirector&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;director &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newDirector&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// cannot assign to title because it is a read-only property&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Accessors&lt;/h2&gt;
&lt;p&gt;We can use setters and getters in combination with our private properties:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; _title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; _duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;duration&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_duration&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;duration&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newDuration &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; newDuration &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;      &lt;span class=&quot;token keyword&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Duration cannot be negative&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newDuration&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newTitle
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newDuration
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;Playing &quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_title&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;...&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, I’m using an underscore &lt;code class=&quot;language-text&quot;&gt;_&lt;/code&gt; as a prefix to differentiate the private properties. It’s a common naming convention.&lt;/p&gt;
&lt;p&gt;These accessor functions can have their own logic. For example, we check for negative &lt;code class=&quot;language-text&quot;&gt;duration&lt;/code&gt; values.&lt;/p&gt;
&lt;p&gt;Here’s how we use them:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; playableTrack &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;something&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;111&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
playableTrack&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// throws an error, Duration cannot be negative&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Accessors with a &lt;code class=&quot;language-text&quot;&gt;get&lt;/code&gt; and no &lt;code class=&quot;language-text&quot;&gt;set&lt;/code&gt; are automatically inferred to be &lt;code class=&quot;language-text&quot;&gt;readonly&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Guarding methods with types&lt;/h2&gt;
&lt;p&gt;Now let’s try to write our custom method:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; _title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; _duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;duration&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_duration
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;duration&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newDuration &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; newDuration &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;token keyword&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Duration cannot be negative&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newDuration
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newTitle
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newDuration
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;Playing &quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_title&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;...&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token function&quot;&gt;getDurationTime&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; hours&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;floor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_duration &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; minutes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_duration &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;60&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;hours&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;minutes&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Everything that applies to functions can be used for defining methods in classes.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; playableTrack &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;something&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;111&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;playableTrack&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getDurationTime&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 1:51&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I don’t know if you agree with me, but this hardcoded &lt;code class=&quot;language-text&quot;&gt;60&lt;/code&gt; value looks kinda odd to me. We can refactor it using the &lt;code class=&quot;language-text&quot;&gt;static&lt;/code&gt; keyword:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; minutesPerHour&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; _title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; _duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;getDurationTime&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; hours&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Math&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;floor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_duration &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; Playable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;minutesPerHour&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; minutes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_duration &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; Playable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;minutesPerHour&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;hours&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;minutes&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Ah much better! I feel like I’m writing C# now.&lt;/p&gt;
&lt;h2&gt;Abstract classes&lt;/h2&gt;
&lt;p&gt;TypeScript adds support for &lt;code class=&quot;language-text&quot;&gt;abstract&lt;/code&gt; classes:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;protected&lt;/span&gt; _title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;protected&lt;/span&gt; _duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newTitle
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newDuration
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// must be implemented in derived classes&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For starters, we cannot instantiate objects from an &lt;code class=&quot;language-text&quot;&gt;abstract&lt;/code&gt; class:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/23108bd461a525c00a4f79ad293ea6aa/fbae3/typescript-abstract-class-error.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 17.870722433460074%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAYAAACOXx+WAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA5klEQVR42m2QS0/CUBQGm9tCaUsvpQ/aW4o8NGpMwBQfRMSFiBqgyP//NeNtUFcuJvPlrCbHUEbFL1mNuyaLl6ThM6q7JOs8oaIVib8g11bhikzfI/+RWC7w7RLPnOFaUxKxwegZB05UmiNhY0boKAJngJ1c4akb0u6YKBgivT6y3SdyC6STI92cdivDtTP8ZkwkXjCuW5+cN7dcal/YWybyg9vgnmnwQJG+UfQ2jJI1w+QVX5RIc07H1Lbmp13butMudeE7xkDsKMSegVl7x5l1ZNw4MNHkoi7f/9TXL/n6F/W3K74BQbhrtpRL/a0AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-abstract-class-error.png&quot;
        title=&quot;&quot;
        src=&quot;/static/23108bd461a525c00a4f79ad293ea6aa/e996b/typescript-abstract-class-error.png&quot;
        srcset=&quot;/static/23108bd461a525c00a4f79ad293ea6aa/17741/typescript-abstract-class-error.png 263w,
/static/23108bd461a525c00a4f79ad293ea6aa/52211/typescript-abstract-class-error.png 525w,
/static/23108bd461a525c00a4f79ad293ea6aa/e996b/typescript-abstract-class-error.png 1050w,
/static/23108bd461a525c00a4f79ad293ea6aa/087e3/typescript-abstract-class-error.png 1575w,
/static/23108bd461a525c00a4f79ad293ea6aa/8079d/typescript-abstract-class-error.png 2100w,
/static/23108bd461a525c00a4f79ad293ea6aa/fbae3/typescript-abstract-class-error.png 2260w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Abstract classes allow us to define abstract methods. These are methods that don’t have any implementation, but we expect from the derived classes to implement them. We only define the signatures of those abstract methods. TypeScript will make sure they are followed by the book.&lt;/p&gt;
&lt;p&gt;To illustrate this in practice, consider the following derived class:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Podcast&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  producer&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newProducer&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;producer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newProducer
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the previous example, we extend the class &lt;code class=&quot;language-text&quot;&gt;Playable&lt;/code&gt;, to create a &lt;code class=&quot;language-text&quot;&gt;Podcast&lt;/code&gt; class. Note that if we fail to implement all its methods, we will get a very informative compilation error:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/2265a7ca11b60f0b56d4336a4acfeed1/0ddab/typescript-non-abstract-class-error.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 47.14828897338403%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAABYlAAAWJQFJUiTwAAABmklEQVR42oWS227aUBBFbWxDHDAGkwbwJcEQgkHQECq1hUpFah5IAJtLVPWl//8bq+PYoVVVpQ9Le+boaM6c2aOE5pGZ95Ph+TPD4oaZ/UDH2HKpbGkKrVxf4/+heFrCuPGDgfnMrbJnJMUic8WVGtNVEtEtXeMRT4tPhf9FS4mzgm4hYVJ9ZFo5MDF23OkJ96WYQSkhlPhaHuiaWwI58w2hmOD9oSmu3Gmq67xDdY9vfaPT+UAYfsQPZrTad7TcKVZtQFVwLsbU6hF1Z4jTGFFzokzlzK4PsOweZX1CW36kBNoB11pSsQMaVhPTbFIotoRLFONCeJep/hrnnPL0noNpRHlB40BUXnFfnTOqPdAvf6VvfqJpLKhoc6qCVVhkmudVbYEt+pvPOIXlyxyVK33HzPrOe/sorOiW1vRLG3oy2xsxqaPuuBH3Q9F+nvdkZoG2x1V2J9LushmmJjSeWLpHvvhPTM9WzMWA8XlqSkwoQ++drUXFHMnTlUofvS5u8fW/ibOCvrbBVzd40pWnJgTSevuNFXlrJ38BBlv5AwAy1v8AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-non-abstract-class-error.png&quot;
        title=&quot;&quot;
        src=&quot;/static/2265a7ca11b60f0b56d4336a4acfeed1/e996b/typescript-non-abstract-class-error.png&quot;
        srcset=&quot;/static/2265a7ca11b60f0b56d4336a4acfeed1/17741/typescript-non-abstract-class-error.png 263w,
/static/2265a7ca11b60f0b56d4336a4acfeed1/52211/typescript-non-abstract-class-error.png 525w,
/static/2265a7ca11b60f0b56d4336a4acfeed1/e996b/typescript-non-abstract-class-error.png 1050w,
/static/2265a7ca11b60f0b56d4336a4acfeed1/087e3/typescript-non-abstract-class-error.png 1575w,
/static/2265a7ca11b60f0b56d4336a4acfeed1/8079d/typescript-non-abstract-class-error.png 2100w,
/static/2265a7ca11b60f0b56d4336a4acfeed1/0ddab/typescript-non-abstract-class-error.png 2220w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Now let’s fix this, by implementing the &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt; method:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Podcast&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Playable&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  producer&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newProducer&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;newTitle&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newDuration&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;producer &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; newProducer
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;Playing &quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;_title&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;...&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can use our &lt;code class=&quot;language-text&quot;&gt;Podcast&lt;/code&gt; class to create podcasts:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; myPodcast&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Podcast &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Podcast&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token string&quot;&gt;&apos;A podcast title&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token number&quot;&gt;90&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token string&quot;&gt;&apos;Nicos Tsourektsidis&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
playable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
playable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sharePodcast&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And here’s where the magic begins. We can use the type of the abstract class &lt;code class=&quot;language-text&quot;&gt;Playable&lt;/code&gt; to indicate that our variable can host any object from classes that derive from &lt;code class=&quot;language-text&quot;&gt;Playable&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; playable&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Playable &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Podcast&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token string&quot;&gt;&apos;A podcast title&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token number&quot;&gt;90&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token string&quot;&gt;&apos;Nicos Tsourektsidis&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
playable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// This is defined in the abstract class, it will work&lt;/span&gt;
playable&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sharePodcast&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// This method will not work, it&apos;s not defined&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And now we have access to all the methods and properties that exist in the &lt;code class=&quot;language-text&quot;&gt;Playable&lt;/code&gt; class. This allows us to perform high-level actions on a variety of objects that have completely different implementations.&lt;/p&gt;
&lt;p&gt;If we try to access a method or a property that’s not defined in the parent abstract class, this will throw an error:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/00e1475497ac29ce2059b449cb5c170b/ed8a2/typescript-no-method-error.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 22.43346007604563%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAYAAACOXx+WAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA3UlEQVR42o2PS0+DQBSFJx0e8gqhIMw4YNCkSx9xoxVLDXbRpNDoQv//P/kcW36Aiy/n3LM4915hnInbYkRnA23bY8qOJt/Qqg9M9sa19SrtMMve6itX2Yam2FLnPXn4bLM1pdyhxNEyIhp34iYdufQ7lqHBu6jwIk2YWB8pvFgjowo3VjjhWb1EW7V5UCLjgkyu0eLTFh4QK3/PSzqwineY9J063aLs5ko+UVgS+UAwE84E8v40R86j1TuKxWAL5wvN4kDr7GncI8b/ofa/qZ2v0wv6nygxncr+Cn8BZy9rTooCe18AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-no-method-error.png&quot;
        title=&quot;&quot;
        src=&quot;/static/00e1475497ac29ce2059b449cb5c170b/e996b/typescript-no-method-error.png&quot;
        srcset=&quot;/static/00e1475497ac29ce2059b449cb5c170b/17741/typescript-no-method-error.png 263w,
/static/00e1475497ac29ce2059b449cb5c170b/52211/typescript-no-method-error.png 525w,
/static/00e1475497ac29ce2059b449cb5c170b/e996b/typescript-no-method-error.png 1050w,
/static/00e1475497ac29ce2059b449cb5c170b/087e3/typescript-no-method-error.png 1575w,
/static/00e1475497ac29ce2059b449cb5c170b/ed8a2/typescript-no-method-error.png 1972w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Cover Credit: &lt;a href=&quot;https://unsplash.com/photos/3T6LAs2JhMw&quot;&gt;Nataliya Smirnova&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Master Functions in Typescript]]></title><description><![CDATA[In this article, we will discover what options JS provides for declaring and consuming functions and we find out how to guard them with TypeScript types.]]></description><link>http://www.nicotsou.com/tltr-typescript-functions/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-functions/</guid><pubDate>Thu, 16 Dec 2021 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;JavaScript has some powerful features for &lt;strong&gt;functional programming&lt;/strong&gt;. At least a light version of it.&lt;/p&gt;
&lt;p&gt;In this article, we will discover what options JS provides for declaring and consuming functions and we find out how to guard them with TypeScript types.&lt;/p&gt;
&lt;p&gt;This article belongs to my series Too Long To Read TypeScript. Every article covers in detail a core concept of the language.&lt;/p&gt;
&lt;p&gt;Cake? 🍰&lt;/p&gt;
&lt;h2&gt;The different ways to declare a function&lt;/h2&gt;
&lt;p&gt;You’re probably familiar with the simple &lt;strong&gt;function declaration&lt;/strong&gt;, which uses the &lt;code class=&quot;language-text&quot;&gt;function&lt;/code&gt; keyword:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The variables &lt;code class=&quot;language-text&quot;&gt;a&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;b&lt;/code&gt; are called &lt;strong&gt;parameters&lt;/strong&gt;, when the signature of the function is concerned. Within the function itself they are known as &lt;strong&gt;arguments&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Another way, is to assign a function to a variable, or to an object property. This is called a &lt;strong&gt;function expression&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; fn &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; add
&lt;span class=&quot;token function&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// returns 4&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can pass functions as arguments in other functions, which are also known as &lt;strong&gt;callbacks&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;annoyUsers&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;token function&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Subscribe to my channel!&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;annoyUsers&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the previous example, we passed &lt;code class=&quot;language-text&quot;&gt;annoyUsers&lt;/code&gt; to &lt;code class=&quot;language-text&quot;&gt;setTimeout()&lt;/code&gt; as a callback. This function will be called after a delay of 1 second. This unlocks the potential of asynchronous code, but it can also increase complexity, something that is known as &lt;a href=&quot;http://callbackhell.com/&quot;&gt;callback hell&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Another way you can create a function is on the fly:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token function&quot;&gt;setTimeout&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Subscribe to my channel!&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;These functions don’t have a name, and thus we call them &lt;strong&gt;anonymous function expressions&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Ah, yes! Consider adding names to your functions as much as you can. It helps with easier debugging. It also enables recursion, since there is no way for anonymous functions to refer to themselves.&lt;/p&gt;
&lt;h2&gt;Adding type annotations with TypeScript&lt;/h2&gt;
&lt;p&gt;TypeScript supports all the existing features of JavaScript functions, and of course, we have the option to define types. As with all the other typed elements of TypeScript, type annotations live into your code, and they will be removed after transpilation, which means they will not be available at runtime.&lt;/p&gt;
&lt;p&gt;We can define a type for the arguments and the &lt;code class=&quot;language-text&quot;&gt;return&lt;/code&gt; value. Consider the following function:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getTrackTitles&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;tracks&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Type error, the return value must be an array of strings&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example, the function &lt;code class=&quot;language-text&quot;&gt;getTrackTitles()&lt;/code&gt; accepts an array of Tracks, and as a return value an &lt;code class=&quot;language-text&quot;&gt;array&lt;/code&gt; of strings. In case we have a different return value, the compilation will fail:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/988a528eab96ce1fb3c0333c77263975/6274f/typescript-number-type-error.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 24.334600760456272%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA7ElEQVR42o2PTU/CQBRFByht+dICbae2ArUoljYGcWHiysSNgAU27lj4/3/FcWaQiDsXJ/e+m/vy8oSsfRBZK6LGGtlQah31HJ2d+DMrH9bff7uiQgzEG1FSMgxykvEjflTS827xZUGg/Ch94nqypO/fI+MHk/dNV2c5bidlKOf0LmYEYoXwO3vkYMH4csGkW+J5Ben0xTBKn8mLV6J4iWWnOO0pTivDdjPjtTYdPd/gtu4IaxvEqP1JJr+YywOz7l5d2ZDYO2Jra0iaO6J6ZcqhWB85eaM/KG9e1gtXQv+vy5XSrdFzdPY/Kr4BFjmIepXwmnEAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-number-type-error.png&quot;
        title=&quot;&quot;
        src=&quot;/static/988a528eab96ce1fb3c0333c77263975/e996b/typescript-number-type-error.png&quot;
        srcset=&quot;/static/988a528eab96ce1fb3c0333c77263975/17741/typescript-number-type-error.png 263w,
/static/988a528eab96ce1fb3c0333c77263975/52211/typescript-number-type-error.png 525w,
/static/988a528eab96ce1fb3c0333c77263975/e996b/typescript-number-type-error.png 1050w,
/static/988a528eab96ce1fb3c0333c77263975/087e3/typescript-number-type-error.png 1575w,
/static/988a528eab96ce1fb3c0333c77263975/6274f/typescript-number-type-error.png 2098w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;Optional parameters&lt;/h3&gt;
&lt;p&gt;We can define &lt;strong&gt;optional parameters&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  isFavorite&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;createTrack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  isFavorite&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    artist&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    isFavorite&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here the argument &lt;code class=&quot;language-text&quot;&gt;isFavorite&lt;/code&gt; is optional. We can call the function with or without this parameter.&lt;/p&gt;
&lt;p&gt;Optional parameters &lt;em&gt;cannot&lt;/em&gt; be set for arguments when they have a default value:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// this will not work&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;createTrack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  isFavorite&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    artist&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    isFavorite&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, the function parameter &lt;code class=&quot;language-text&quot;&gt;isFavorite&lt;/code&gt; is optional, but it also has a default value of &lt;code class=&quot;language-text&quot;&gt;false&lt;/code&gt;. This is logically impossible, but don’t worry, TypeScript will prevent it:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/d3378976ebeba6b9f3df7e9e78fb2a26/bfe41/typescript-questionmark-initializer-error.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 48.669201520912544%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABYlAAAWJQFJUiTwAAABmUlEQVR42qWQa2/SYBiGC6XtWk4bPbeUchzIMrpCGJ6iW0jUgAMd25ItW/wg//8vXL6rqNFPUz9cuZ/nSXO9dyrZuXfYxZc4pVe45dccaKdZPt4esYwX2V7bm2HpU/zKc5zijLI8ppQfi5x8n+UUU36LZOpL/PaI9rMpUS9l324Tid2Lhj+Juyl22CeMu8StHnH7EGM/Qq9GFAwPtRSilj0qaorkaO9x7D4N/xjnoIftDwiaCX7jmFBkvZUSxCNMry8YUDG71NwBpvjOrR+hVxqUax2qokhFnSAF+pqj8JzEO2PknBNZb5h0FiSdJcPeB2bpimY4Z6+QYIgGRYGhnGTohRNK2ljcxuhKgqXOkXq1O84aW+bNLUPngYZyw6F6Q1u5JtI2dEsbAvkzQf6KUL4SuRH5g92+w89fInnKmlb1noH5gK9cYEkLwRJb4OzS3c1PQXKkj/StW047W+raWggWeOLm7vByv+anILm5C9HulmnrK0n9C53ydfbS30h+E8b6hti4zJoE8kr8h9U/yzJhqHz6L8Gfwm8VWxYTg5bAUQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-questionmark-initializer-error.png&quot;
        title=&quot;&quot;
        src=&quot;/static/d3378976ebeba6b9f3df7e9e78fb2a26/e996b/typescript-questionmark-initializer-error.png&quot;
        srcset=&quot;/static/d3378976ebeba6b9f3df7e9e78fb2a26/17741/typescript-questionmark-initializer-error.png 263w,
/static/d3378976ebeba6b9f3df7e9e78fb2a26/52211/typescript-questionmark-initializer-error.png 525w,
/static/d3378976ebeba6b9f3df7e9e78fb2a26/e996b/typescript-questionmark-initializer-error.png 1050w,
/static/d3378976ebeba6b9f3df7e9e78fb2a26/087e3/typescript-questionmark-initializer-error.png 1575w,
/static/d3378976ebeba6b9f3df7e9e78fb2a26/bfe41/typescript-questionmark-initializer-error.png 2082w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Required arguments cannot follow optional ones:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;createTrack&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  title&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// optional arguments cannot lead&lt;/span&gt;&lt;/span&gt;  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// required arguments&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    artist&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    isFavorite&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, we define the &lt;code class=&quot;language-text&quot;&gt;title&lt;/code&gt; argument as optional, but the next argument &lt;code class=&quot;language-text&quot;&gt;artist&lt;/code&gt; is mandatory. This will create a messy function invocation, because usually the optional arguments are last in the list. Of course, once again, TypeScript will prevent this:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/f60001cd445b9cb9267a6ad94f97bd7c/8c48a/typescript-required-parameter-after-optional.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 46.38783269961977%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAABYlAAAWJQFJUiTwAAABaUlEQVR42qWSS0/bQBRGh4QkhDyUONhjx4/YjqFOSEAFWkTbFAESUhMIQSVi16ps+P+/4HBtHhsW0HZxdGbu6H66oxllqhnaOCTqfSUKvmG0dgm8L/juEa79GUd/oh9O6Ei9a41Ieh/x9JhSsZ9TXk0EcSlho3CGMlcXmN09gs1ddDDCsLdw/G1M54OQot0BltDtjdDeQPbpo72h9KVUmz4ts09zI8YoHEvgyhSrPsZvDAjrQ4x2SrR5SJg8MhhN8MJ9yush5VqUuyKuNmLW6jGlau/pLKRTPEHZxTkH/g3fk1v2wiVBfcZO65JtITamjL0Z7vo5bXUiE7ymUzh9WVsynHLLC8bt3xxZd0S1JVrNcQRbsDKvzPOara7eRKtLlCWP4jduGXp/8KvXdNQPCbogq+vcF7nfi8oakuaSSXrPTvcXwdr1XwW8CnQKc/qNJbGQOar9/L9Av7LAK11hqinZn3y+6r8GPgCgmfmqn/1bdwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-required-parameter-after-optional.png&quot;
        title=&quot;&quot;
        src=&quot;/static/f60001cd445b9cb9267a6ad94f97bd7c/e996b/typescript-required-parameter-after-optional.png&quot;
        srcset=&quot;/static/f60001cd445b9cb9267a6ad94f97bd7c/17741/typescript-required-parameter-after-optional.png 263w,
/static/f60001cd445b9cb9267a6ad94f97bd7c/52211/typescript-required-parameter-after-optional.png 525w,
/static/f60001cd445b9cb9267a6ad94f97bd7c/e996b/typescript-required-parameter-after-optional.png 1050w,
/static/f60001cd445b9cb9267a6ad94f97bd7c/087e3/typescript-required-parameter-after-optional.png 1575w,
/static/f60001cd445b9cb9267a6ad94f97bd7c/8c48a/typescript-required-parameter-after-optional.png 2096w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;Default values&lt;/h3&gt;
&lt;p&gt;In the latest versions of JavaScript, we have the option to define &lt;strong&gt;default function parameters&lt;/strong&gt;, which will allow name parameters to be initialized with default values, if no value or &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt; is passed:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// returns NaN, when no value is passed&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;addWithDefaultValues&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;addWithDefaultValues&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// returns 0 when no arguments passed&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example, the argument will get the default value if no value is passed.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Now let’s see how type definition can work together with default values:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;void&lt;/h3&gt;
&lt;p&gt;If a function doesn’t return anything, we can use the type &lt;code class=&quot;language-text&quot;&gt;void&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;annoyUsers&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token function&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Subscribe to my channel!&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It is recommended to &lt;em&gt;always&lt;/em&gt; provide a return value to your functions.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/23afa357f162e3ed5c27404c7726bf52/e5bc7/gamepad.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAIBBAX/xAAWAQEBAQAAAAAAAAAAAAAAAAAAAQL/2gAMAwEAAhADEAAAAc9qjsoQR//EABkQAQADAQEAAAAAAAAAAAAAAAEAAgMQEv/aAAgBAQABBQLPyzWoPGyk/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAGBAAAgMAAAAAAAAAAAAAAAAAAREAIDH/2gAIAQEABj8CWGFV/8QAGhABAAMAAwAAAAAAAAAAAAAAARARMQAhYf/aAAgBAQABPyE1J9HOUgpNrJCC50R//9oADAMBAAIAAwAAABDE3//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8QP//EABURAQEAAAAAAAAAAAAAAAAAABAR/9oACAECAQE/EKf/xAAbEAEAAwADAQAAAAAAAAAAAAABABEhEDFRcf/aAAgBAQABPxDS/VoLeCy/ktlFHvPcj3Blo8OhofeP/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;gamepad.jpeg&quot;
        title=&quot;&quot;
        src=&quot;/static/23afa357f162e3ed5c27404c7726bf52/9ecec/gamepad.jpg&quot;
        srcset=&quot;/static/23afa357f162e3ed5c27404c7726bf52/5d001/gamepad.jpg 263w,
/static/23afa357f162e3ed5c27404c7726bf52/7349d/gamepad.jpg 525w,
/static/23afa357f162e3ed5c27404c7726bf52/9ecec/gamepad.jpg 1050w,
/static/23afa357f162e3ed5c27404c7726bf52/39d31/gamepad.jpg 1575w,
/static/23afa357f162e3ed5c27404c7726bf52/1f368/gamepad.jpg 2100w,
/static/23afa357f162e3ed5c27404c7726bf52/e5bc7/gamepad.jpg 5184w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Arrow functions&lt;/h2&gt;
&lt;p&gt;Another way to create an anonymous function is to use an &lt;strong&gt;arrow function&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;multiplyBy2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; number &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;multiplyBy2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see the syntax is simpler and they end up taking much less space. But you can make it even shorter. Here are all the acceptable forms of arrow functions:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Without arguments&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;sayHello&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;hello&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// With only one argument&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;sayHello&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;hello &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;name&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Without return value&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;sayHello&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;hello &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;name&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Returning an object&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;makeObject&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; name &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Adding types to arrow functions&lt;/h3&gt;
&lt;p&gt;And here’s how we can define types for our arrow functions:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; add &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; number&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token parameter&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Type annotations can also take the form of an arrow function, to indicate the signature of a function, with the given types for parameters and the return value. This works great with callbacks:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;calculate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;values&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  values&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;previous&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; current&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;previous&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; current&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;calculate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; add&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is a function that calculates a given array of numbers, by using the provided callback. Note that we specified the expected types for this callback. This will prevent consumers to do something like this:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;randomFn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;no clue what I&apos;m doing&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;calculate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; randomFn&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// type error, that&apos;s not my type of fn&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here, &lt;code class=&quot;language-text&quot;&gt;randomFn()&lt;/code&gt; doesn’t accept 2 numbers as arguments and it doesn’t return a number. Therefore, this will not compile:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/fd8cabcd20a168fa3f05a4e70d980fd6/91945/typescript-function-type-error.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 32.31939163498099%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAABYlAAAWJQFJUiTwAAABgElEQVR42h2R227aYBCEDQbH2BiMMcbnA3YcDoFEpGnVSK0qNSJpAsWQuki9atX3f4avf7kYze5IO7urkTz9ROr8JRudKOM3FvEv7pI/TOyKq/6O3NoSuxsy95VZ7xur3guRsWGqb4gFHPWReXdD2PmKpT4jxeqBsv+T1DgQ9/fkvZrSPGFaa7Txgs6gwAxzTP8SoxszNFL0bkRb9Wh3PC70EL2f0BJ9x4iRxsobtlLjXVQkwjDpHRlJrwTOe9bvvpCka7L8nji9wwmuKWYfmS0/4Uc3LFafSYt7yvkDefkBY5Aj+eoPxuZvYVaz8A8s3ZqpecAyV1wtH7D9BWF6y8id0xMDA7vEEdpgVGKPp2et0RojK+JaLUIK2nu81g5P3uE3twT/a4GhtsQzCywlxbBK3PAG055iu9fiRXGJ7NKQPWHm01RCJMGqniFF+p5MBHBp10y0ikyvmOhHSqfidnKkMMQC5UUE8J1I25F09wTqllFTBCI/nfmMxhPD9iP/AHTJp12qKHfgAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-function-type-error.png&quot;
        title=&quot;&quot;
        src=&quot;/static/fd8cabcd20a168fa3f05a4e70d980fd6/e996b/typescript-function-type-error.png&quot;
        srcset=&quot;/static/fd8cabcd20a168fa3f05a4e70d980fd6/17741/typescript-function-type-error.png 263w,
/static/fd8cabcd20a168fa3f05a4e70d980fd6/52211/typescript-function-type-error.png 525w,
/static/fd8cabcd20a168fa3f05a4e70d980fd6/e996b/typescript-function-type-error.png 1050w,
/static/fd8cabcd20a168fa3f05a4e70d980fd6/087e3/typescript-function-type-error.png 1575w,
/static/fd8cabcd20a168fa3f05a4e70d980fd6/8079d/typescript-function-type-error.png 2100w,
/static/fd8cabcd20a168fa3f05a4e70d980fd6/91945/typescript-function-type-error.png 2944w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;Arrow functions vs regular functions&lt;/h3&gt;
&lt;p&gt;Arrow functions are widely used in modern applications. There are some fundamental changes with the regular functions. You must remember that it’s not only about the syntax. I will try to list them here briefly, but I do recommend you spend some time studying these differences, if you are not familiar with them. Here we go:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;They are always anonymous, which as we saw before, it may cost you time on debugging.&lt;/li&gt;
&lt;li&gt;Arrow functions don’t alter the value of lexical &lt;code class=&quot;language-text&quot;&gt;this&lt;/code&gt;. The value of &lt;code class=&quot;language-text&quot;&gt;this&lt;/code&gt; inside an arrow function is the closest parent regular function.&lt;/li&gt;
&lt;li&gt;Arrow functions don’t have a binding on &lt;code class=&quot;language-text&quot;&gt;arguments&lt;/code&gt;, which means that the arguments special variable will not be recognized if you try to access it within an arrow function.&lt;/li&gt;
&lt;li&gt;They can’t act as a &lt;code class=&quot;language-text&quot;&gt;constructor&lt;/code&gt;. This means that you can’t call them with the keyword &lt;code class=&quot;language-text&quot;&gt;new&lt;/code&gt;, as you can normally do with regular functions, to create new object instances.&lt;/li&gt;
&lt;li&gt;In JS you are allowed to use the same argument multiple times, like &lt;code class=&quot;language-text&quot;&gt;add(x, x)&lt;/code&gt;, if you use a regular function. This will not work on &lt;code class=&quot;language-text&quot;&gt;strict&lt;/code&gt; mode. Duplicate arguments aren’t allowed for arrow functions, even on non-strict mode.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/f6bff5578398eb03cee42fb747c38e10/9b601/post-apocalyptic-warrior-boy-outdoors-in-a-wastela-2021-08-27-18-17-21-utc.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAQFA//EABUBAQEAAAAAAAAAAAAAAAAAAAID/9oADAMBAAIQAxAAAAFjRZKbsk0R/8QAGhABAAMAAwAAAAAAAAAAAAAAAgEDEQASE//aAAgBAQABBQK9SCLCjkcvHY+WM2Mx/8QAFhEBAQEAAAAAAAAAAAAAAAAAABEB/9oACAEDAQE/AYuP/8QAFhEBAQEAAAAAAAAAAAAAAAAAAQAR/9oACAECAQE/ARsb/8QAHBABAAIBBQAAAAAAAAAAAAAAEQABAhASITFR/9oACAEBAAY/Aq2jKe/NFnGUFn//xAAbEAADAAMBAQAAAAAAAAAAAAAAAREhMWFBcf/aAAgBAQABPyGzUZ+lsJPh9DoNs14VbItOaKh9Wj//2gAMAwEAAgADAAAAEAsf/8QAFREBAQAAAAAAAAAAAAAAAAAAARD/2gAIAQMBAT8QQQ//xAAWEQEBAQAAAAAAAAAAAAAAAAAAAWH/2gAIAQIBAT8QpW7/xAAeEAACAgICAwAAAAAAAAAAAAABIQARMVFBYXGRof/aAAgBAQABPxApakX0IZ8pBo6GkYdy9wEo0Q4ONKAbH1A0MrvK8T//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;post-apocalyptic-warrior-boy-outdoors-in-a-wastela-2021-08-27-18-17-21-utc.jpg&quot;
        title=&quot;&quot;
        src=&quot;/static/f6bff5578398eb03cee42fb747c38e10/9ecec/post-apocalyptic-warrior-boy-outdoors-in-a-wastela-2021-08-27-18-17-21-utc.jpg&quot;
        srcset=&quot;/static/f6bff5578398eb03cee42fb747c38e10/5d001/post-apocalyptic-warrior-boy-outdoors-in-a-wastela-2021-08-27-18-17-21-utc.jpg 263w,
/static/f6bff5578398eb03cee42fb747c38e10/7349d/post-apocalyptic-warrior-boy-outdoors-in-a-wastela-2021-08-27-18-17-21-utc.jpg 525w,
/static/f6bff5578398eb03cee42fb747c38e10/9ecec/post-apocalyptic-warrior-boy-outdoors-in-a-wastela-2021-08-27-18-17-21-utc.jpg 1050w,
/static/f6bff5578398eb03cee42fb747c38e10/39d31/post-apocalyptic-warrior-boy-outdoors-in-a-wastela-2021-08-27-18-17-21-utc.jpg 1575w,
/static/f6bff5578398eb03cee42fb747c38e10/1f368/post-apocalyptic-warrior-boy-outdoors-in-a-wastela-2021-08-27-18-17-21-utc.jpg 2100w,
/static/f6bff5578398eb03cee42fb747c38e10/9b601/post-apocalyptic-warrior-boy-outdoors-in-a-wastela-2021-08-27-18-17-21-utc.jpg 5760w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Hoisting&lt;/h2&gt;
&lt;p&gt;JavaScript has a feature called &lt;strong&gt;hoisting&lt;/strong&gt;. This allows us to use functions or variables before their actual declaration.&lt;/p&gt;
&lt;p&gt;Check the example below. It will compile successfully in JS, and the function declarations will be automatically moved to the top of your file.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// returns 5&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Hoisting was very useful back in the days where scripts were randomly referenced within a HTML document, by making the scripting language easier to use. Nowadays, hoisting is yet another weird feature of JavaScript, you should be familiar with. Thankfully, there is a way to protect yourself from unwanted behavior.&lt;/p&gt;
&lt;p&gt;Arrow functions are &lt;em&gt;not&lt;/em&gt; hoisted, similar to any other declaration that uses the &lt;code class=&quot;language-text&quot;&gt;const&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;let&lt;/code&gt; keywords:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// we will get an error that the function is not declared yet&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; b&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Function overloading&lt;/h2&gt;
&lt;p&gt;Many languages, like C#, or Java, allow multiple implementations of the same function, as long as their argument lists, or their types, are different. JavaScript doesn’t have this behavior, since function arguments are what make the actual functions.&lt;/p&gt;
&lt;p&gt;TypeScript adds support for &lt;strong&gt;function overloading&lt;/strong&gt;. This allows you to have multiple functions with the same name within the same scope, but with a different set of arguments.&lt;/p&gt;
&lt;p&gt;The syntax differs from what you are used to seeing in other languages. At first, you need to define the &lt;em&gt;overload signatures&lt;/em&gt;, which are function declarations without a body, and then to provide a function with the implementation:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// overload signatures&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// actual implementation&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;search&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;nameOrId&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; nameOrId &lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;number&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;searching by id&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; nameOrId &lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;string&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token builtin&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;searching by name&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example the function &lt;code class=&quot;language-text&quot;&gt;search()&lt;/code&gt; can accept either a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;, to search by name, or a &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt; to search by id. The first two functions define the multiple ways you can call this function; that’s why we omit their bodies. The third function is the actual implementation. We have a single argument &lt;code class=&quot;language-text&quot;&gt;nameOrId&lt;/code&gt; which can be a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt; or a &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;It is worth mentioning here that this syntax will not work in JS, because you can only have one definition for the &lt;code class=&quot;language-text&quot;&gt;search()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;From now on, the moment you start typing the name of the function you will get a useful popup with the different ways you can implement it:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/c76ad9efc9978a0ef77b023acaa0a9b6/f02b9/typescript-function-overloading.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 64.63878326996198%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABYlAAAWJQFJUiTwAAACeUlEQVR42nVTW1PaYBCNEBASIJDkyxUIKMiAF7CIHUvtDO30oRYSQgICXjvFPvf/v5wuKaO16sOZbze7OfudswlnCJcw5Hto2RtU1BD14gRNc462dYt6NsRhKkBXmqMuhngnztBWliilfGjcd+jc8AU4Q5jD1h5gKSvU7DlatUscV+9xoN2gJy1wuibLhDjOTHGiXqFCsbY1jAhVAiP8S85ZyQDl3BKOdI1qNkCdEYFyhaZ6jUPzDk56AntrhFLMhc172CFUEmOUEh52+TF20gGKyTGRbgj11BQmW0FnD7CdFfacBT62HvC1+xsn1Z9oyHN8MGYYlBdoSFOcFZZoCSGcbR8HqRCN7BSl7ScLOJ2u79CEHjW2pTs0yL9u7QbvnUv0ytMI3XKA02qIvcIMamxIUi8iqUok++I/ySTBoYlm3IPJ+7CI3BF8kurT5DHKayRdkjWCnXBhknxj87KxwbOl6ORNUZihodyhov1CjS3Rb67Qr/9Ax1pin9Fm9Qn6tSk65owGBa9u95HQjLuokgf72xO0kj52CgSVYtnDvurhUBujkfdwwHw05Um0AP2NT+YvId3Q4l3Ia1+2PPJkGHkjcyOCS7FLz9woVwjrukb5m4QW3dDM30IvLKBnPqFY+AxLGsDKncPMnIGlz2DQaWb7YMI5ivkvUZ8W+0YEo5eExaQXTVT4ATKKA9nYhWrtoaAXITGGdE5FTmVQTZNiG8xe1ypg8QEtZLSR/wSuJE5QV+fYZR4K2R5Evg2BP4KQaENMHENMdiinM9FBhuI01cTkEQx+CDs+hRUPnoGrZsb0mwVoShOU+BBWLKTG8EVjhFjwWDNj/qv4A6IaeT/tgnwkAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-function-overloading.png&quot;
        title=&quot;&quot;
        src=&quot;/static/c76ad9efc9978a0ef77b023acaa0a9b6/e996b/typescript-function-overloading.png&quot;
        srcset=&quot;/static/c76ad9efc9978a0ef77b023acaa0a9b6/17741/typescript-function-overloading.png 263w,
/static/c76ad9efc9978a0ef77b023acaa0a9b6/52211/typescript-function-overloading.png 525w,
/static/c76ad9efc9978a0ef77b023acaa0a9b6/e996b/typescript-function-overloading.png 1050w,
/static/c76ad9efc9978a0ef77b023acaa0a9b6/f02b9/typescript-function-overloading.png 1554w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Advanced types&lt;/h2&gt;
&lt;p&gt;Are you ready for more? In this section I am listing some additional types that may become useful when you deal with functions. If you already build JS applications, it will be good to know their existence. They are used rarely, though. Feel free to skip this part if you are just starting out with TypeScript.&lt;/p&gt;
&lt;h3&gt;object&lt;/h3&gt;
&lt;p&gt;We discovered in a previous article the available types of JS. There, we also talked about &lt;em&gt;primitive&lt;/em&gt; and &lt;em&gt;reference types&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;If you are sure that a function always returns an object, but it can be a different object structure, you can use the special type &lt;code class=&quot;language-text&quot;&gt;object&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;makeObject&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;str&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; object &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; str &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, I’m not advocating for using this specific type, because it is quite similar to &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt;. It can be useful when you are writing utility functions, which are meant to be shared across all your projects. Make sure that you have some type declaration in the parent functions, the ones that act as consumers, to avoid any mistakes in your code.&lt;/p&gt;
&lt;p&gt;Now, remember that &lt;code class=&quot;language-text&quot;&gt;object&lt;/code&gt; is not the same as &lt;code class=&quot;language-text&quot;&gt;Object&lt;/code&gt; with a capital O. The later one is a reserved JS keyword and it refers to the actual global object type. Never use it, simply ignore its existence.&lt;/p&gt;
&lt;h3&gt;unknown&lt;/h3&gt;
&lt;p&gt;Another useful type for our functions is &lt;code class=&quot;language-text&quot;&gt;unknown&lt;/code&gt;. It works as a wildcard, similar to &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt;, but it is safer. It basically prevents us from writing risky code.&lt;/p&gt;
&lt;p&gt;Here’s a simple example that illustrates the difference between &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;unknown&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// using any&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;file&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  file&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// works fine&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// using unknown&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;file&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;unknown&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  file&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;play&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// nope, name may not be&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the previous example, we have a function that accepts a file, which may or may not have the method &lt;code class=&quot;language-text&quot;&gt;play()&lt;/code&gt;. In case we use &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt;, TypeScript will not complain at all. But if we use unknown, we will get the following error:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/64bc9e6c8f7c7420879fb66b01a284bf/b5a09/typescript-unknown-type.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 44.86692015209126%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAABYlAAAWJQFJUiTwAAAB1ElEQVR42m1SXXOaUBC9GBEUNYLABS8VFAWDSjI6SW0T81g/YowZa2pn+tb//xtOF4yZzrQPZ/bs3cPZnWUZZxtwaQNTWqLJFjDYN8IClrTKkb1ZbAWTLfNaltvSmr55+hfkxRz2Al2+R8ubwG/PEEWP6IUPsK1rGMYIYfceuj5Ey50i6s8RBF+gaRFkOUCp1Mkhl3xUlCQfjAx3uCzewfZicDGEF1zDFglqjQBVAieuXQZoOgO02uO8pmgepBJHQXFwobgUORS1m09Jhls0lRnC5A7diIzJ1OAxLDeBxQfoxzPYxBvNXs578We4Ygyl7NH7EA5xk5rV61ewGRm68ht07RFG+RNMrQeTOplqByrl9WYfbjulJiNYrSRvxr0Uwr9B3ezjQhUoknFBdUnfO03Iiwc49g4j9wGJPsOVcYtp4yta1QmEmCMdbpCOthjECySDFdLxFrfTPeVLVMpjaJWUdpqiqkzedyg9k8kB88Fv3IS/EIZ7RP4RkX6EKO3gyC8I6j8h1FeI8v6D+5U3dGo/4GsHdGtHuIXn01/uaN8h5C0cOgvO1nQiT7SLLK5zQYZsN1n3DB+cYqax3+NZy853yGlSh+LfOIuy2pk7ue6E/9X/AMnR+Af7SAiVAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-unknown-type.png&quot;
        title=&quot;&quot;
        src=&quot;/static/64bc9e6c8f7c7420879fb66b01a284bf/e996b/typescript-unknown-type.png&quot;
        srcset=&quot;/static/64bc9e6c8f7c7420879fb66b01a284bf/17741/typescript-unknown-type.png 263w,
/static/64bc9e6c8f7c7420879fb66b01a284bf/52211/typescript-unknown-type.png 525w,
/static/64bc9e6c8f7c7420879fb66b01a284bf/e996b/typescript-unknown-type.png 1050w,
/static/64bc9e6c8f7c7420879fb66b01a284bf/b5a09/typescript-unknown-type.png 1360w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;With the &lt;code class=&quot;language-text&quot;&gt;unknown&lt;/code&gt; keyword we can also guard our arguments:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ServerErrorResponse&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;parseErrorResponse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;response&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;unknown&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;response &lt;span class=&quot;token keyword&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ServerErrorResponse&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; response&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; response &lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;string&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; response
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example, the function &lt;code class=&quot;language-text&quot;&gt;parseErrorResponse()&lt;/code&gt; checks if the &lt;code class=&quot;language-text&quot;&gt;response&lt;/code&gt; is a string or an object. Since we are not sure about the server response, we have to write some extra logic, to make sure we get the value correctly. Because we used &lt;code class=&quot;language-text&quot;&gt;unknown&lt;/code&gt; as a type for our response, TypeScript will expect us to check the type of the value first, before we interact with it.&lt;/p&gt;
&lt;p&gt;The same example will not work if we don’t explicitly check if &lt;code class=&quot;language-text&quot;&gt;response&lt;/code&gt; is an instance of &lt;code class=&quot;language-text&quot;&gt;ServerErrorResponse&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Function&lt;/h3&gt;
&lt;p&gt;For the same purposes, TypeScript also provides a special keyword &lt;code class=&quot;language-text&quot;&gt;Function&lt;/code&gt; which is another wildcard for functions:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;calculate&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;values&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; fn&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;Function&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  values&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;previous&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; current&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;previous&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; current&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It is considered a good practice &lt;em&gt;not&lt;/em&gt; to use the &lt;code class=&quot;language-text&quot;&gt;Function&lt;/code&gt; keyword over the technique we’ve learned in the previous sections, which uses an arrow function syntax to describe the signature of the function. It may be useful, when you are accepting multiple function signatures.&lt;/p&gt;
&lt;p&gt;Also, this can be confused with the &lt;code class=&quot;language-text&quot;&gt;function&lt;/code&gt; keyword that JS provides. Remember, the type is using a capital F.&lt;/p&gt;
&lt;h3&gt;never&lt;/h3&gt;
&lt;p&gt;Finally, in cases when we know that a function never returns a value, we can use the &lt;code class=&quot;language-text&quot;&gt;never&lt;/code&gt; type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;throwError&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;message&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;never&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token keyword&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;message&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Cover Image Credit: &lt;a href=&quot;https://unsplash.com/photos/0d2zObuClQI&quot;&gt;Rodion Kutsaev&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Types Of TypeScript]]></title><description><![CDATA[In this Types Special, we will discover how JavaScript deals with types and how you can make your life simpler with TypeScript.]]></description><link>http://www.nicotsou.com/tltr-typescript-types/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript-types/</guid><pubDate>Wed, 08 Dec 2021 22:12:00 GMT</pubDate><content:encoded>&lt;blockquote&gt;
&lt;p&gt;Does JavaScript have types? Keep reading. The answer may surprise you.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In &lt;a href=&quot;https://en.wikipedia.org/wiki/Programming_language&quot;&gt;programming languages&lt;/a&gt;, a &lt;strong&gt;type system&lt;/strong&gt; is a &lt;a href=&quot;https://en.wikipedia.org/wiki/Logical_system&quot;&gt;logical system&lt;/a&gt; comprising a set of rules that assigns a property called a &lt;a href=&quot;https://en.wikipedia.org/wiki/Type_(computer_science)&quot;&gt;type&lt;/a&gt; to the various constructs of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Computer_program&quot;&gt;computer program&lt;/a&gt;, such as &lt;a href=&quot;https://en.wikipedia.org/wiki/Variable_(computer_science)&quot;&gt;variables&lt;/a&gt;, &lt;a href=&quot;https://en.wikipedia.org/wiki/Expression_(computer_science)&quot;&gt;expressions&lt;/a&gt;, &lt;a href=&quot;https://en.wikipedia.org/wiki/Function_(computer_science)&quot;&gt;functions&lt;/a&gt; or &lt;a href=&quot;https://en.wikipedia.org/wiki/Modular_programming&quot;&gt;modules&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;But you are not here for definitions, are you?&lt;/p&gt;
&lt;p&gt;This article belongs to my series &lt;a href=&quot;/tltr-typescript&quot;&gt;Too Long To Read TypeScript&lt;/a&gt;. Every article covers in detail a core concept of the language.&lt;/p&gt;
&lt;p&gt;In this Types Special, we will discover how JavaScript deals with types and how you can make your life simpler with TypeScript. I believe a strong JS foundation is important to fully understand TypeScript. That’s why I will start from the very basics.&lt;/p&gt;
&lt;p&gt;We will then review the built-in types of TypeScript, together with some special keywords that may become really useful in your daily coding. I will introduce you to unions, which let you make your own type combinations. Finally, we will discover how type aliases can be used to build custom types.&lt;/p&gt;
&lt;p&gt;Donut? 🍩&lt;/p&gt;
&lt;h2&gt;Types in JS&lt;/h2&gt;
&lt;p&gt;Run the following code in your console, line by line. Spoiler alert in the comments.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Hello&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it works, returns undefined&lt;/span&gt;
&lt;span class=&quot;token number&quot;&gt;2021&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it works, returns 2022&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;length &lt;span class=&quot;token comment&quot;&gt;// it works, returns 3&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You will notice that JS doesn’t complain at all. A value is an acceptable &lt;em&gt;expression&lt;/em&gt; for JavaScript.&lt;/p&gt;
&lt;p&gt;Now, let’s try the following example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; year &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2020&lt;/span&gt;
year &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;2021&apos;&lt;/span&gt;
year &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it returns 4042 as a string!&lt;/span&gt;
year &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;two thousand twenty one&apos;&lt;/span&gt;
year &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it returns NaN&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Interesting finding; There were no errors, whatsoever, when we tried to change the type of a variable. This proves that variables in JavaScript don’t have types. I can assign anything that I want to any variable that I want.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;In JavaScript, variables don’t have types; their &lt;em&gt;values&lt;/em&gt; have.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;A type of a &lt;em&gt;value&lt;/em&gt; can be &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;null&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;, and &lt;code class=&quot;language-text&quot;&gt;boolean&lt;/code&gt;. ES6 supports a new type called &lt;strong&gt;&lt;code class=&quot;language-text&quot;&gt;Symbol&lt;/code&gt;&lt;/strong&gt;. We call these &lt;strong&gt;primitive types&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Every other variable is an &lt;code class=&quot;language-text&quot;&gt;object&lt;/code&gt;, including &lt;code class=&quot;language-text&quot;&gt;array&lt;/code&gt;, regular expressions and &lt;code class=&quot;language-text&quot;&gt;Date&lt;/code&gt;. These are &lt;strong&gt;reference types.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In the following example, we assign a string value to the variable &lt;code class=&quot;language-text&quot;&gt;artist&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Pink Floyd&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// we assigned a string value to artist&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Primitive types are &lt;strong&gt;&lt;a href=&quot;https://www.sitepoint.com/immutability-javascript/&quot;&gt;immutable&lt;/a&gt;&lt;/strong&gt;, which means you cannot alter their values directly; they create a new instance every time they change. Immutable values can be compared:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos; Pink floyd &apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; artistWithoutSpaces &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; artist&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;artist&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &apos; Pink floyd &apos;&lt;/span&gt;
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;artistWithoutSpaces&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// &apos;Pink floyd&apos;&lt;/span&gt;
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;artist &lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt; artistWithoutSpaces&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// false&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Reference types are &lt;strong&gt;mutable.&lt;/strong&gt; Which means, when you compare them, you compare their &lt;em&gt;references&lt;/em&gt; not their actual values:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;artist&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Pink Floyd&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; trackWithoutSpaces &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; track
trackWithoutSpaces&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;artist&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;track &lt;span class=&quot;token operator&quot;&gt;===&lt;/span&gt; trackWithoutSpaces&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it&apos;s true!&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can declare &lt;strong&gt;global variables&lt;/strong&gt; if you omit the ****&lt;code class=&quot;language-text&quot;&gt;var&lt;/code&gt; keyword. This also mirrors its value in the global environment object:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;noDeclarationWhatsoever &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;and it works&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;hr&gt;
&lt;p&gt;Now let’s try a weird operation:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token string&quot;&gt;&apos;1&apos;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// returns 12&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Another interesting finding; Again, we don’t see an error. Instead, JavaScript automatically &lt;em&gt;coerces&lt;/em&gt; their values, which will allow you to compare apples with oranges if you want to. A &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt; wins over a &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt;, that’s why the result is a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;. That means there is a predefined priority.&lt;/p&gt;
&lt;p&gt;Now, just for fun, try the following:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;NaN&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it returns &apos;number&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;What a total disappointment. It turns out that &lt;code class=&quot;language-text&quot;&gt;NaN&lt;/code&gt; has actually the type of a &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Nobody can trust such a language, right? And yet millions of applications are using it. There’s a high chance that the application you are using to read this article is also made with JS. That’s not because its developers had an alternative and instead decided to make their lives difficult. JavaScript is [almost] the only option on the web. It just works. Everywhere.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Don’t forget, JavaScript was discovered back when downloading a song was &lt;em&gt;slower&lt;/em&gt; than visiting a store to purchase the physical CD album.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If we could somehow improve it. But wait, we already know such a tool. It’s called…&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/cc78e892b0341f4a7ec124cf469526c1/c27bc/blank-cd-and-headphones-on-yellow-background-2021-08-26-15-35-24-utc.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAIDBf/EABYBAQEBAAAAAAAAAAAAAAAAAAMCBP/aAAwDAQACEAMQAAAB6151gSAFf//EABkQAQACAwAAAAAAAAAAAAAAAAEAEQIQQf/aAAgBAQABBQIbyg2dvX//xAAWEQADAAAAAAAAAAAAAAAAAAABEBH/2gAIAQMBAT8BhX//xAAXEQEBAQEAAAAAAAAAAAAAAAACARAR/9oACAECAQE/AQxDy5//xAAWEAEBAQAAAAAAAAAAAAAAAAARECD/2gAIAQEABj8CI4//xAAZEAEAAwEBAAAAAAAAAAAAAAABABEhMVH/2gAIAQEAAT8hJN2FuTEc5DMXT7BqG0OT/9oADAMBAAIAAwAAABATz//EABcRAAMBAAAAAAAAAAAAAAAAAAABESH/2gAIAQMBAT8QdMIf/8QAFhEBAQEAAAAAAAAAAAAAAAAAAQAh/9oACAECAQE/EEobLt//xAAaEAEAAwEBAQAAAAAAAAAAAAABABEhMVGR/9oACAEBAAE/EOdy0VvNitCHUwXNlKfkS08iiqD2o8z/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;blank-cd-and-headphones-on-yellow-background-2021-08-26-15-35-24-utc.jpg&quot;
        title=&quot;&quot;
        src=&quot;/static/cc78e892b0341f4a7ec124cf469526c1/9ecec/blank-cd-and-headphones-on-yellow-background-2021-08-26-15-35-24-utc.jpg&quot;
        srcset=&quot;/static/cc78e892b0341f4a7ec124cf469526c1/5d001/blank-cd-and-headphones-on-yellow-background-2021-08-26-15-35-24-utc.jpg 263w,
/static/cc78e892b0341f4a7ec124cf469526c1/7349d/blank-cd-and-headphones-on-yellow-background-2021-08-26-15-35-24-utc.jpg 525w,
/static/cc78e892b0341f4a7ec124cf469526c1/9ecec/blank-cd-and-headphones-on-yellow-background-2021-08-26-15-35-24-utc.jpg 1050w,
/static/cc78e892b0341f4a7ec124cf469526c1/39d31/blank-cd-and-headphones-on-yellow-background-2021-08-26-15-35-24-utc.jpg 1575w,
/static/cc78e892b0341f4a7ec124cf469526c1/1f368/blank-cd-and-headphones-on-yellow-background-2021-08-26-15-35-24-utc.jpg 2100w,
/static/cc78e892b0341f4a7ec124cf469526c1/c27bc/blank-cd-and-headphones-on-yellow-background-2021-08-26-15-35-24-utc.jpg 3900w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;Types in TypeScript&lt;/h2&gt;
&lt;p&gt;Everything in TypeScript just simply makes sense:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; year &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2020&lt;/span&gt;
year &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;2021&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it doesn&apos;t compile; you cannot assign a string to a number. Why on earth you want to do this? Now go back and fix that broken code. Thankfully AI will replace you humans and we will not be annoyed by you anymore...&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Thankfully, no surprises here. TypeScript will protect us from such mistakes. It’s not a warning that we can forget about it; it’s a &lt;em&gt;compilation error&lt;/em&gt;. It completely prevents us from running our application.&lt;/p&gt;
&lt;p&gt;We can see errors while writing code. Text editors that are compatible with TypeScript, like Visual Studio Code, will simply indicate the error, as we type:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/9b053812824773248c2e63b7abd954b2/2ed34/typesript-assignment-error.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 30.798479087452467%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAABYlAAAWJQFJUiTwAAABQ0lEQVR42oWQSU/CUBRGXwvUghBaoO17rxQtOMQJYwLGCWcJRsEBdaUxbv3/++MTTDBuXJzce8/iy80n6u49jfkHQnfMov9C03smmRuxmB8RiZt/Cf/cQmVGKHtIWntnO/1kR39w0Hylk7wZP0ZaUyLrEWXNbmlPnbafJl5ZZmbGCGndEoghq3LAeuscVd1jrXVB1T9EBUck8pQFfcZKOkAHPZLohDg8pqFOWVroUyl2kZVDouo+oXOF+H5TiTtKTgc3bFGLV/H9lEZtmXxBkctL3KKmUK7jzCvcUszcBE3eONsNjddkiyHl7O40UJpAL9elUmiSehtU3JS6aqPrbapynUBvEsVbWDmFyBpyP5jdcrTxGuGYwMz+d+CtKXNIaF+aXnqmixMC+5igcE7iXROX+iTlwQQv08M3/J4zjqhZfb4Akh2jOS9vvqwAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typesript-assignment-error.png&quot;
        title=&quot;&quot;
        src=&quot;/static/9b053812824773248c2e63b7abd954b2/e996b/typesript-assignment-error.png&quot;
        srcset=&quot;/static/9b053812824773248c2e63b7abd954b2/17741/typesript-assignment-error.png 263w,
/static/9b053812824773248c2e63b7abd954b2/52211/typesript-assignment-error.png 525w,
/static/9b053812824773248c2e63b7abd954b2/e996b/typesript-assignment-error.png 1050w,
/static/9b053812824773248c2e63b7abd954b2/087e3/typesript-assignment-error.png 1575w,
/static/9b053812824773248c2e63b7abd954b2/2ed34/typesript-assignment-error.png 1776w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Notice that if you hover over the year variable, TypeScript already understands it’s a &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt;. This is called &lt;strong&gt;type inference&lt;/strong&gt;. Sometimes it can be redundant to declare the types for your variables, since they are implicitly defined by their declaration.&lt;/p&gt;
&lt;p&gt;This way of defining types is called &lt;strong&gt;right-hand typing&lt;/strong&gt;, which refers to the right side of the equals sign (=). This probably is more familiar to you if you are a JavaScript developer.&lt;/p&gt;
&lt;p&gt;But there is another way to define types, which is called, as you can guess, &lt;strong&gt;left-hand typing&lt;/strong&gt;. This is the TypeScript way and we will get into it in the next section.&lt;/p&gt;
&lt;h3&gt;Built-in Types&lt;/h3&gt;
&lt;p&gt;To help TypeScript better understand how your code is interconnected, it provides &lt;em&gt;type annotations&lt;/em&gt; that can live within our code.&lt;/p&gt;
&lt;p&gt;Here are the most important built-in types, you will most probably use on a daily basis:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Hard rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Progressive rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here are some special ones:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; error&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// similar to JS&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; loading&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// similar to JS&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// This function should not return any value&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;An important thing to remember, these type annotations are &lt;em&gt;only&lt;/em&gt; available in the TypeScript compiler, and they never make it to the actual production code. This means, they are available at compile time and not at runtime. That’s how all strongly-typed languages work.&lt;/p&gt;
&lt;p&gt;Now let’s review two more important types.&lt;/p&gt;
&lt;h3&gt;any&lt;/h3&gt;
&lt;p&gt;What if we want to go back to where we came from? What if we have legacy code, that doesn’t respect types at all? Do we need to re-write everything or is there a way to temporarily disable type checking?&lt;/p&gt;
&lt;p&gt;Yes, my friends. There is. And it’s called &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; year&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;any&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2020&lt;/span&gt;
year &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;2021&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it perfectly compiles without issues.&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now you may be wondering, is this even legal? That’s the reason we added TypeScript from the very beginning, right? Because of its type annotations. I mean, it is even called type-script. If we have to use &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt;, then what’s the point of having TypeScript at all?&lt;/p&gt;
&lt;p&gt;Well, my friends, you are right one more time. The usage of &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt; encouraged. Avoid it whenever you can.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;When you use &lt;em&gt;any&lt;/em&gt; in your type annotations, you simply skip the type annotations. You are basically saying “I hate TypeScript and I don’t want to deal with it. &lt;em&gt;You&lt;/em&gt; deal with it!“.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;There are some cases that &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt; becomes useful, though. I use it a lot when I want to try an idea quickly, let’s say to log something on the console, just to debug my code. So none of my colleagues knows that I’m actually using &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt; in our project.&lt;/p&gt;
&lt;p&gt;Any is like a wildcard. Use it, but with caution.&lt;/p&gt;
&lt;p&gt;An alternative to any is the special comment &lt;code class=&quot;language-text&quot;&gt;@ts-ignore&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; year&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;any&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2020&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// @ts-ignore&lt;/span&gt;
year &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;2021&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This disables the type system for this specific line or file. It’s mostly preferred when you want to hack your type system on purpose. You can then search and replace all its instances.&lt;/p&gt;
&lt;p&gt;There is a better way to deal with unexpected values in TypeScript. The keyword &lt;code class=&quot;language-text&quot;&gt;unknown&lt;/code&gt;, which we will analyze in the next section. The ultimate way is to use Generics. We will cover them in a separate post.&lt;/p&gt;
&lt;h3&gt;unknown&lt;/h3&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;unknown&lt;/code&gt; keyword works similarly to &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;unknown&lt;/span&gt;
duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;354&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it works&lt;/span&gt;
duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;5.54m&apos;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// it works&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The difference is that TypeScript doesn’t let you assign values of other types to a variable that is of an &lt;code class=&quot;language-text&quot;&gt;unknown&lt;/code&gt; type:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; duration&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;unknown&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; durationInMinutes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
duration &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;5.54m&apos;&lt;/span&gt;
durationInMinutes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; duration &lt;span class=&quot;token comment&quot;&gt;// Compilation Error&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By running the previous example, we will get an error that &lt;code class=&quot;language-text&quot;&gt;unknown&lt;/code&gt; is not assignable to type &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/9fac7ebde525e42e1b9d82fbfeb14544/701e9/typescript-error-unknown.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 22.813688212927755%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAABYlAAAWJQFJUiTwAAABJUlEQVR42j2O2U7CUBRFG+gAltKGltIZkBkUNBYwVKLvGJnq8OD//8byMsSHlb2zs3JypKae42hrmvGCuv1IO8mI/CWmcYfnzgm8BWGwpNd9E/szrvNEK87ObhKuCL2rW09x7BlSouaY6pIbf4QtcOMJQXOKG4xxvCFmrYNj3uLZfTQ9QtVDdLOJWg7RKjFlI6FY8sUeIZc9pEjJqRZSISQ0zD61aodaY4ifzAhaMyynJ74a4AskuS5wkYoiFffST5vSuKCK7hf3tK0Nq+47i3hD1vlgOtmSTr94GO5J73Oy+S+D1jtWaYVrvmIba+zKy5manmHI8yvp6eCWlrZnZP3Qr34zFDkW2dYOBPKORDsSi+4JLywciJVPIjknKh4Jr/jS7p8/01iIjoy2LM0AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-error-unknown.png&quot;
        title=&quot;&quot;
        src=&quot;/static/9fac7ebde525e42e1b9d82fbfeb14544/e996b/typescript-error-unknown.png&quot;
        srcset=&quot;/static/9fac7ebde525e42e1b9d82fbfeb14544/17741/typescript-error-unknown.png 263w,
/static/9fac7ebde525e42e1b9d82fbfeb14544/52211/typescript-error-unknown.png 525w,
/static/9fac7ebde525e42e1b9d82fbfeb14544/e996b/typescript-error-unknown.png 1050w,
/static/9fac7ebde525e42e1b9d82fbfeb14544/087e3/typescript-error-unknown.png 1575w,
/static/9fac7ebde525e42e1b9d82fbfeb14544/701e9/typescript-error-unknown.png 1780w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;To fully understand the value of the &lt;code class=&quot;language-text&quot;&gt;unknown&lt;/code&gt; keyword, consider the following example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; result&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;unknown&lt;/span&gt;
result&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Compilation Error&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Since we don’t know if the result will a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;, we can’t be sure it implements the &lt;code class=&quot;language-text&quot;&gt;trim()&lt;/code&gt; method. TypeScript again will guard this variable and it will not let us do some inappropriate action with it (sic):&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/608088d32b90b7b19c68c76a3c09d8a1/2eb79/typescript-error-unkown-method.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 46.00760456273764%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAABYlAAAWJQFJUiTwAAAB3UlEQVR42oWSXW8SQRSGZ2FdLCxfu+w3sMDCUgoUsJJGLxQDbUVoiVVsNcZ4YZp4YeL/v3kctsEaY+LFM2cyk33fd84e4WsbwsJ7Alndg2tq+XfUctd4mQ1O6hJLrDHFKsGWe+c/CE+9olHcEqjy49wtTv6GvvuRof+ZhhTuSKNjadTPShN1LQ2WUvjNP3GkqbDlEmSuk4Ouf8ek85Pn8Q9O2t85Dr9xat8xOvjCWP9KXfuEJz7gKTcEqdsEP/XHXt0idjF9dSOdL+k6K2bTLUfhkqo3JwzPaFVXWIVXkhmOrDXzjMBYYOkvMbMv8Epz3KK8z87w0m/vBXcpHXFFOTsmOhxh+V0Mq0mhXMdwWlTbQ5x6F9OLiAdT2kdPyVdCsqUqVhBTdJrkCg0qyuJecPd2T2zIaROEOUQvxzQ6U+zakFKuQUOPifI9ipkAIcooioFQTJS0kyBSNo/UGrZy8SDoSsGCNiavtzBzETWjR1nfpYzw3YFsgUzpD8ibHUpWjB30yeghQnMT0lodK/VbcC0FrzDUZzTVEXV1Qjv9RDZ6RKl4Qru1uKc5J47OOexcMB7IqTBP0dQej7U+We1YJlw+CO6piHUye/u6629FjsrfGOJ1cufKn7lnN4e/ABjy+WUvZp/vAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-error-unkown-method.png&quot;
        title=&quot;&quot;
        src=&quot;/static/608088d32b90b7b19c68c76a3c09d8a1/e996b/typescript-error-unkown-method.png&quot;
        srcset=&quot;/static/608088d32b90b7b19c68c76a3c09d8a1/17741/typescript-error-unkown-method.png 263w,
/static/608088d32b90b7b19c68c76a3c09d8a1/52211/typescript-error-unkown-method.png 525w,
/static/608088d32b90b7b19c68c76a3c09d8a1/e996b/typescript-error-unkown-method.png 1050w,
/static/608088d32b90b7b19c68c76a3c09d8a1/2eb79/typescript-error-unkown-method.png 1256w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;To bypass this error, we can add some logic that narrows down the &lt;code class=&quot;language-text&quot;&gt;unknown&lt;/code&gt; result value. We can simply check if it’s a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt;. TypeScript will recognize our efforts to make our code more robust.&lt;/p&gt;
&lt;p&gt;Embrace the unknown.&lt;/p&gt;
&lt;h2&gt;Unions&lt;/h2&gt;
&lt;p&gt;Sometimes you know that a variable accepts different types. For example, you have a value that can be undefined if it’s not set, or you have a boolean that can become a number. Another example is the response that you get from the server.&lt;/p&gt;
&lt;p&gt;TypeScript provides a solution to standardize such scenarios. It is called a &lt;strong&gt;union&lt;/strong&gt; and the syntax is the following:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; id&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example, the &lt;code class=&quot;language-text&quot;&gt;id&lt;/code&gt; variable can act as a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt; or &lt;code class=&quot;language-text&quot;&gt;undefined&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Enumerations&lt;/h3&gt;
&lt;p&gt;Similar to other languages that support enums, we can define our own &lt;code class=&quot;language-text&quot;&gt;enum&lt;/code&gt;, and then use it:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; Filetypes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  Mp3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  Mp4&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  Wav&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; fileType &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Filetypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Mp3&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By using such an enum, we avoid typos that can slow us down, for example, if we use a capital letter or not. We also allow the text editor to provide auto-completion, which speeds up our development even more. Who doesn’t like to press Enter all the time?&lt;/p&gt;
&lt;p&gt;Enums act like Symbols in ES6. But it’s better, because we can control the real values. There are some important rules to remember.&lt;/p&gt;
&lt;h3&gt;Enum values&lt;/h3&gt;
&lt;p&gt;TypeScript assigns values to your enum properties, but you can customize them if you want to.&lt;/p&gt;
&lt;p&gt;By default, values start counting from &lt;code class=&quot;language-text&quot;&gt;0&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; Filetypes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  Mp3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 0&lt;/span&gt;
  Mp4&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 1&lt;/span&gt;
  Wav&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// 2&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can set custom values if we want to:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; Filetypes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
	Mp3&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp3&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	Mp4&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;mp4&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
	Wav&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;wav&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can use a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt; or a &lt;code class=&quot;language-text&quot;&gt;number&lt;/code&gt; as a value, but you &lt;em&gt;can’t&lt;/em&gt; use a &lt;code class=&quot;language-text&quot;&gt;boolean&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;TypeScript compares their actual values:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; Filetypes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  Mp3 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  Wav &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

Filetypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Mp3 &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; Filetypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Wav &lt;span class=&quot;token comment&quot;&gt;// returns false&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Transpilation behavior&lt;/h3&gt;
&lt;p&gt;Now, here’s a pro tip. Let’s revert our &lt;code class=&quot;language-text&quot;&gt;enum&lt;/code&gt; back to how it was:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; Filetypes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  Mp3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  Mp4&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  Wav&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will be the equivalent code in JS:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;Filetypes&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  Filetypes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Filetypes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Mp3&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Mp3&apos;&lt;/span&gt;
  Filetypes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Mp4&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;a&apos;&lt;/span&gt;
  Filetypes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Filetypes&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Wav&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Wav&apos;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Filetypes &lt;span class=&quot;token operator&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Filetypes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You may think this is too much for such a simple code. But there is a way to bypass it if you add the keyword &lt;code class=&quot;language-text&quot;&gt;const&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;enum&lt;/span&gt; Filetypes &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  Mp3&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  Mp4&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  Wav&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now the enum value disappears from the JS file, but its value will be simply assigned wherever you use it. It’s a nice way to completely disable enums from the generated code.&lt;/p&gt;
&lt;h3&gt;Type Aliases&lt;/h3&gt;
&lt;p&gt;TypeScript provides a nice feature called &lt;strong&gt;type aliases&lt;/strong&gt;, and it works like this:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;tsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-tsx line-numbers&quot;&gt;&lt;code class=&quot;language-tsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  releasedAt&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
  gernes&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  isFavorite&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;boolean&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It is useful to reuse complex type definitions across your application. It is also useful, when you want to separate the type declaration code, from the actual implementation, just to keep your code more readable.&lt;/p&gt;
&lt;p&gt;Here is how we can use it to guard object structures:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;myTrack&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;releasedAt&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1975&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;gernes&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Hard rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Progressive rock&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;isFavorite&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As with all types, these aliases only affect compilation time, and it will not make it at runtime.&lt;/p&gt;
&lt;p&gt;Don’t be confused. That’s not the only way you can describe the structure of an object. I will cover the &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt; keyword in a separate post. I will also cover the &lt;code class=&quot;language-text&quot;&gt;interface&lt;/code&gt; keyword, which is by far the most preferred way.&lt;/p&gt;
&lt;p&gt;The good thing about type aliases is that you can use unions in them:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;type ServerResponse &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; string &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; Track&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With this in mind, we can reduce the usage of &lt;code class=&quot;language-text&quot;&gt;any&lt;/code&gt; in our code, when it’s possible.&lt;/p&gt;
&lt;p&gt;The even better thing is that you &lt;em&gt;cannot&lt;/em&gt; modify a type alias after its creation. This eliminates duplicates, which can be caused by different parts in your code.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/7b08d35caa9445eadba6602ce1b9c219/b1001/typescript-duplicate-identifier.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 35.36121673003802%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAABYlAAAWJQFJUiTwAAABqklEQVR42k2RWXPTQBCEV0KXL9nIts7IsiIHnyFXpYgBl5OHACa+CE5sqKKKPMD//wcfKz3x0NVds7s9M72iU9oRaRu80h7f/UNW/cm76oHLxg/Oq3uuaweGzYOsPxGpawJ1gytWeGKDL5GzK9YFe2KJSMryor7Fl5cn3i/Oe3t6/iduRp95P/nCWXov9YLpZFHUus07vModLWuGo3+gZc7wynNa+vx/w2+yywMX8W+SoxvidEzQOcWNx7jhkDi7IExO8aIRlUZCt3dF0+9TsmNeuyf4yZhabVB4SMNnQm1Lx3rkMn6h0bhG+FfY9QFpvU+jnuFVMxyrg1BaEu2CFbWNqgWoeoDQ2ph6D19ZScOKNJQTdqwNgfFAWRtil45pWl0iK6VcjuVkY/xwQisc4fgDmsGQIHmLUY0RuocwXHQjk4ZywlR+wJHM0DMPhM5fRs6CmXvPcemWnjGnbX2kG37lTWdNFi05iVeFHqUyJvsW25hSN6c45q3c9AmRr+srS3zzmaD+Ql9memZLc21HW/1OKOEp8rEim4o8622hc87PInVXINfBq0f+AQecxFrYr1peAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;typescript-duplicate-identifier.png&quot;
        title=&quot;&quot;
        src=&quot;/static/7b08d35caa9445eadba6602ce1b9c219/e996b/typescript-duplicate-identifier.png&quot;
        srcset=&quot;/static/7b08d35caa9445eadba6602ce1b9c219/17741/typescript-duplicate-identifier.png 263w,
/static/7b08d35caa9445eadba6602ce1b9c219/52211/typescript-duplicate-identifier.png 525w,
/static/7b08d35caa9445eadba6602ce1b9c219/e996b/typescript-duplicate-identifier.png 1050w,
/static/7b08d35caa9445eadba6602ce1b9c219/b1001/typescript-duplicate-identifier.png 1380w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Cover Credit: &lt;a href=&quot;https://unsplash.com/photos/6W8H4puOJB0&quot;&gt;Rodion Kutsaev&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[TLTR; TypeScript]]></title><description><![CDATA[TypeScript is a superset of JavaScript. All existing features of JS are already supported, but it adds syntax, which helps you build much more structured applications.]]></description><link>http://www.nicotsou.com/tltr-typescript/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-typescript/</guid><pubDate>Wed, 01 Dec 2021 22:12:00 GMT</pubDate><content:encoded>&lt;blockquote&gt;
&lt;p&gt;TypeScript adds support for &lt;em&gt;strongly typed programming&lt;/em&gt; in your JavaScript applications. It’s tightly integrated with your text editor, to help you catch any errors early.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;TypeScript is a superset of JavaScript. All existing features of JS are already supported, but it adds syntax, which helps you build much more structured applications.&lt;/p&gt;
&lt;p&gt;TypeScript’s compiler transpiles to JavaScript. This means that you can run all the applications on every browser, on Node.js or other native apps that have a built-in JavaScript compiler. You have plenty of options to set the target supported devices.&lt;/p&gt;
&lt;p&gt;You can adopt TypeScript gradually into your existing projects. You can also use it to catch JavaScript errors in your continuous integration pipelines. In fact, &lt;a href=&quot;https://code.visualstudio.com&quot;&gt;Visual Studio Code&lt;/a&gt; has TypeScript built-in, to help you find errors without any configuration from your side.&lt;/p&gt;
&lt;p&gt;By introducing types to your code, your editor can understand much better how the different parts of your application are interconnected. Therefore, it can provide you with sophisticated auto-completion suggestions that can speed up your development.&lt;/p&gt;
&lt;p&gt;TypeScript has full support for &lt;a href=&quot;https://jsdoc.app&quot;&gt;JSDoc&lt;/a&gt;. Moreover, you can use comments within your code to create live documentation.&lt;/p&gt;
&lt;p&gt;Type checking runs on compile-time. If you violate the type rules, your application will not run due to errors. All the extra code that you add will be removed from the transpiled JS code and it will not be available at runtime.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I have to confess something to you, my friends.&lt;/p&gt;
&lt;p&gt;My first experience with TypeScript wasn’t really satisfactory. You see, I’ve been using JS for many years and I had to get familiar with its bad and good parts. It wasn’t easy and I did suffer a lot. But after some point you learn how to use the language properly. I was pretty much trusting my development skills.&lt;/p&gt;
&lt;p&gt;I was wrong.&lt;/p&gt;
&lt;p&gt;First off, when I integrated TypeScript into my projects, I uncovered a lot of type mistakes, that could potentially cause bugs. Sometimes, it’s hard to think about everything and to remember what you were doing a year ago, for example.&lt;/p&gt;
&lt;p&gt;I wasn’t only proud of myself. I was also proud of my team. You see, I was convinced everybody in my team understands JavaScript. We had code reviews, we were constantly learning. What could go wrong, right?&lt;/p&gt;
&lt;p&gt;I was wrong again.&lt;/p&gt;
&lt;p&gt;In every development project, a team is a living organism. It scales up and down. You should not take for granted the level of expertise you have at any moment. You have to think proactively. Maybe some juniors will join the project. Maybe you will need to invest in less experienced developers, because you can’t find any. And what if you had 10 or 50 developers? What if you had more?&lt;/p&gt;
&lt;p&gt;That’s the lesson that I’ve learned. Having a strongly typed language reduces time for everyone. And this time grows exponentially as you add more people to the project.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/2200970897239471062ffd38b59b7bbe/8171b/arcades.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAADVElEQVR42iWTS2hcdRTG79z3vO7cSeY9mck8k8nMJJk8ZppJWiYklUppMtNES9NaY0xsCUpMbdGSZJEW40IogmLJVkWUCroQhCJuXCnizrUbceNa3Lj4eTIuDvd/uB/nfOf7zlF0n4sRSmAly5Sv3+bc0UfyPaKy+QHju59S2/masa0fqL32E9Nv/UZl6xmZhUcsNe9wc2aDu/lF3k6dZ8EpkAxEUSwngukPoztRwuNzTN09pvnOKa2jp8y/+x2zD35mYvd3Gnt/svTh38wd/8HQ0scU669SqayTzVwgHamwVlrmsHoLJRBN4Q9HsIID2PEkoVqT8o09mgenXHj/K+ZPfpH3X7Qf/sPat3D5s3+p3/mReOcBvuIySqpKb6zHkxfvsT/+Aoo5MITtDKBZNvVel6HJhtC2CQxniTQmSXe65FfuU9t+QuvgG2bufU5p/SHuSAdfZpTY7Co7a3scvXHMzcoSSijeIOBPo2gKje4rVC+uovsMnOERYq3LhMfmSLSvUt18zPOnv9I+fIpTnkFRFIanetRX7zOXbzJz+xPc8jmUTPsa1c42qqJRe2mb/KVLaLZKbmWN7pfPGL2+T+HKI4qrj6luiGG9E5xiG9VQWZzbJDPVRTUt5g+/INVcQeksrzNxfgvF4yOYTlO/sYtt2KTLDZ57/T0KrUXaGwdMXd1h5paM3n0ZIxASvIfq7BUSuWlU1WT02gnhQhtlYlJ0GpvETWbRVINkpYUTS5MujJAttIjHc5RbC2Tq0yRKFQazOVTHFWdLTJSaYqZD9uKbTO1/Lz4EUYJBl2SxQjxfw/CF8ai6gFzCpRG8yQqBVAkzHO3vqsfwonsD6MFBfKE4ukfHdBMMdbYEV8WjeFBUAam6JYVM0cXCkC666cMbyRHMTqNphuRBNN1GM3zoZ/9tyb1BVMuPFUrij4+KSVq/jqKJXqqMqprSORQRsINm+qVjvb8aHiuEPZDHcIYlspjOEKrtCtOwvFNi4ADe2KxMF0GV5v2CHnmowkDVTAE4Ag4RSFfxZxuYkWGsaBFjsIQezmGJdmYwJnoJAa/bZ677B+XaZJf/Z+ilH8LwbKQz4Fnu5KtyOTk0uXXdifVDkwJ6IIHhFtACGVRvRAi49GXrS2fzH+qohknul/V6AAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Arcades&quot;
        title=&quot;&quot;
        src=&quot;/static/2200970897239471062ffd38b59b7bbe/e996b/arcades.png&quot;
        srcset=&quot;/static/2200970897239471062ffd38b59b7bbe/17741/arcades.png 263w,
/static/2200970897239471062ffd38b59b7bbe/52211/arcades.png 525w,
/static/2200970897239471062ffd38b59b7bbe/e996b/arcades.png 1050w,
/static/2200970897239471062ffd38b59b7bbe/087e3/arcades.png 1575w,
/static/2200970897239471062ffd38b59b7bbe/8079d/arcades.png 2100w,
/static/2200970897239471062ffd38b59b7bbe/8171b/arcades.png 2671w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/m3hn2Kn5Bns&quot;&gt;Carl Raw&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;TypeScript isn’t free. You need to invest time navigating its learning gap. It’s a mind shift compared to traditional JS development. Like every other tool, you need to carefully integrate it into your projects without making your team suffer every time they try to innovate.&lt;/p&gt;
&lt;p&gt;One of the things I didn’t like about TypeScript, was the fact that I’m not writing JS anymore. I wasn’t convinced this was a game changer for the industry. I’ve seen similar libraries become obsolete and I’ve seen developers struggling with refactoring their code. In the JavaScript ecosystem, trends come and go.&lt;/p&gt;
&lt;p&gt;Back then, JavaScript was evolving rapidly. Don’t get me wrong, it still does. But since ES6 there was a huge effort to improve the language. New features were being added. Something inside me was refusing to make the switch to another language.&lt;/p&gt;
&lt;p&gt;The fear of missing out.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Many things have changed since then. TypeScript changed. It’s now aiming to support standards, instead of introducing new features that are completely irrelevant to the JavaScript language itself. It’s also backwards compatible, which lets you run pretty much every code written in JS, even the dirtiest ones.&lt;/p&gt;
&lt;p&gt;Now you may be wondering, how can you learn a language that has two faces? How can you integrate it into your projects? How far do you need to go with the type restrictions? What different options do you have to structure your code right and which one you should use?&lt;/p&gt;
&lt;p&gt;Understanding TypeScript takes time. And I would like to help. I’m introducing a series of posts that will help you scale your applications with confidence. A series that focuses on the most important parts and tries to give you practical ideas and examples that are already used in production applications.&lt;/p&gt;
&lt;p&gt;In this introductory article, we will try to understand how TypeScript works, what it brings to your development process, and how to start new TS projects.&lt;/p&gt;
&lt;p&gt;I hope the following paragraphs will help you have a better understanding of TypeScript and to know exactly what it needs to get started.&lt;/p&gt;
&lt;p&gt;Any feedback will be considered sweet.&lt;/p&gt;
&lt;p&gt;Panna cotta? 🍮&lt;/p&gt;
&lt;h2&gt;How TypeScript was born?&lt;/h2&gt;
&lt;p&gt;I love JavaScript. It runs anywhere, it doesn’t limit you from coding anything you want, and the development community is amazing. So many projects and patterns to try and a huge variety of applications to build.&lt;/p&gt;
&lt;p&gt;It didn’t create a good first impression on me, though. When I first started writing scripts in JavaScript, I really hated the language. You see, JavaScript hides a set of weird quirks, that can cost you a lot of time, if you are not familiar with them.&lt;/p&gt;
&lt;p&gt;But what is wrong with JavaScript, you may ask.&lt;/p&gt;
&lt;p&gt;Well, the truth is, JavaScript was never designed to support the complexity of modern applications. It was designed back then when we were surfing the web using these ancient dial-up modems that were making &lt;a href=&quot;https://www.youtube.com/watch?v=gsNaR6FRuO0&quot;&gt;a characteristic noise&lt;/a&gt;. It wasn’t designed to write advanced applications like GMail, Netflix, Spotify and many more.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/54b3faa6037caf60eec3423f197c74e5/c96dc/casettes.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 82.50950570342206%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAARCAYAAADdRIy+AAAACXBIWXMAAAsTAAALEwEAmpwYAAADqUlEQVR42nWR60+bZRiH+x/4QY2SDbaw4XFhcYrTCArlUAqjpetguAlKwUEWByQ7MRmFLkLLqdDzCQbbyMiiHHRG4wyB6QcXAyYChbaLiiTqxlYoWfx8+bxUjtMPv7z3877Pez3XfT+yiCdIxBtiRSSyFo+0vrsp0Xere6W4A9G4Aiy75lhyzhJ2+Hlg8yOLuIMbGzcBVyNg67W0bz0bsGUBW3L6eWifEcCZbUBP6L/jDj0GizilZxQo2a0CravAwCbg/4C3wyTQamY32hXARetmQ+92YPDflrfOLOKUrKJmW+xEu/ct08iWHzPcfAGhLcA1q3WYNLs1O9s0961TAuiZEyaBqOFaPNH1ii+I9D2aAEsCFnb51/PQKczs0yw6BMw+xT2bAIbd4tolqGQhrn3FGeJRzzwrruBqHnVLdYhlq5+IQxzgCBC2CJBlirBdmqFkf5cHwvaegMsivWJOPWJjf4jFsT+ZH55kyjHAwq1ZFr6Z4U5bH7PXv+Ov8d8IiGfg6hi/j4gWb/5CsPt7fuz6glHjAP6O2/xh/RnZDzUj3Kn4kinzOIt/h/lpbJiJhjpCt0eZGB3kq/JCJq/0MTH+LaNtjUzWG5ib+xX/wjxnq8tRprxBvlyOS3WJrwt6kQ1qbQx/cpZbA7W0139Ah0HFtf7jeNxFdDYWY2vWiPowrc1a9FVK2vUaLp7P4agmiYoDagZVDj7P9zKS52Io14nsZrEbh0nJqQtxaFPjMJQmcrHuOU5WxnEu/VUsNW9SW5fA+4U7OZW2m3bDCxhtz6M5EkNObAqDWRY+zbLyWY6DoUNuZMMFbsozlew/sJOkpF0oMvaQlx9PRnYcqQdjyVPGk6fdTap8BxmpsbyTsoO9CU/ybMwT7Hk6DtXetyl+8RC+jCZG1B5kNw7baCmsRF9STGWJjhPvFlGuTqbsPTW6fC1l6a+jK0hBq1WQlvQaufJ9ZOXsY39iPDFPPcNLuxIoTMymJ7OZIZUL2XWVhWt5rfQeNeK7ZMf6sQlzUQUOvRGLyUzXsVJ6Tp/B0tmF/qMqGo4d53x1NbpSHcmvHETzcjqm5Br6szu4kWtF1qdsoy+nhV6VicvlnXjLWnGp6/BWtuA5bcZzpJbLZY14DDasJw04is7g0pvFhZmpytZRl/Qh7rRGrmS10q/oQOYTqr7MJnwKEY0JT34TTkU9Xk0T7sJmXNmiVjfgKjFi0zZiz72Aq9SI/UQrJsU52t8SB6Tp6ZaL/+VG/gGgecsGh+dG/QAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Photo Credit: Hello I&amp;#39;m Nick&quot;
        title=&quot;&quot;
        src=&quot;/static/54b3faa6037caf60eec3423f197c74e5/e996b/casettes.png&quot;
        srcset=&quot;/static/54b3faa6037caf60eec3423f197c74e5/17741/casettes.png 263w,
/static/54b3faa6037caf60eec3423f197c74e5/52211/casettes.png 525w,
/static/54b3faa6037caf60eec3423f197c74e5/e996b/casettes.png 1050w,
/static/54b3faa6037caf60eec3423f197c74e5/087e3/casettes.png 1575w,
/static/54b3faa6037caf60eec3423f197c74e5/8079d/casettes.png 2100w,
/static/54b3faa6037caf60eec3423f197c74e5/c96dc/casettes.png 2557w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/6nqbKX5UI9I&quot;&gt;Hello I’m Nick&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;From the 90s to the Metaverse era, a lot of things have changed. Applications are now running fully on the browser. In the past, they were only running natively and you had to be home and use that huge computer with the CRT monitor. Now everybody is looking at their phones, even when they are not supposed to. Google is selling Chrome-based laptops. And don’t forget, JS now runs on servers too. Even &lt;a href=&quot;https://www.linkedin.com/pulse/how-nasa-uses-nodejs-rahul-rathod/&quot;&gt;NASA uses JavaScript&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Compared to languages like C# or Java, which are being used widely in the enterprise world, JavaScript seems that it doesn’t have anything to offer to these developers, who were used to organizing their code using classes, interfaces, namespaces and modules.&lt;/p&gt;
&lt;p&gt;On top of that, the development experience is day and night. Your text editor simply cannot compete with a sophisticated IDE like Xcode, or IntelliJ. Besides the code highlighting - which sometimes is very dumb - and some common errors that can be handled with a linter, it doesn’t provide any code completion suggestions, it doesn’t recognize the types of your variables, and most importantly, it doesn’t give you any sophisticated debugging tool.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/0b03a15e19111c84e318f8c86b722b9a/42267/90s-tech.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAADhklEQVR42i2TXWibZRiG0/x++ZLvy1/z06RpmjZtau2SfGmS/qdrk66kLaPT/km2gcyuLWl1bYdjRZQWtGO1a+dOZDiZjKInOkSdYu3AA7UgyBBk4Jx4IIh4LuLJ5Vv14OblhZfrvZ/nuR+d3WxFtdjwyCohxUPU4SfhrifvbqVFqcNjUjFWGfBLTvxWFx6Listsx21ScJrsOIw2XEa7kLiLU2cyGJEMJmSjBbvJimSWaFRDFDzH8ApAQEjS67HpjSh6AzHFT1wJEpG9hKVq6iQvQYsHr9khgDYBFI9kgxmr3ozdaMYmgHXC5XAgSYsjSHf4SfLxbrobO+junSWfPkUp3s9YfZbRYJqST2PU285odZqiO4FOX6XHIhweSZEUbCYLEbWasDOI6ozQlptisvIelYufsXPtETde+JDlqU1eKi5wKTvBQtMJZoI9nA70MVtTRCcJgCzZsak+dMKhryZGbWMOnTWAKdCPM9JLlShbJ7spDVT47tqvvHnlkFuXP2B3ZoP1zjJnI30MeRIMOFrRKaobtTqMZPPQ0FYgM7OLOzmOs7DOE2NbqLVZ1JoUFnczyXSZn/b+5vDu77x/+wfeemOfl89fYblrgvOxIouhEfGx3YXd6qQhojGxtMfZ7R9JLO/jWnnIyOp97F3PYk1MIjeNkO6p8OD6YxbLWzTn1+jonyOZKDAc7WQ+XuLF6Cnh0GDFZ3IQctSihTSKkxv07v6GvHQPeeUA/dJXKEv36bj0OWtXD3mw8zPzF78h/dRNhgoXeDo6zHF3mmnfIAW3Jlqlt3Akj1HlXKiT8a4ygQvv4tj6lppztwmW3yGz+Clnrn7PwZ0/eO3WQ8yhASRPhle057nXs8NHmS0OOrb5OLMuhlJlFMGtEmFViZmcaCcWGP8StI0D/Lk5wsJFbHSN/ultvt77hZsHf3Gy/Cp9yWFmvFnO+Aus1U7zenye/eymAIrAyiKDqghlq8VHVhulcuMRY3N3yKVmGRxcpyt/mXzfKo/v/snm7NuMF1ZYKS5yOjzIrL/EtFNMWU6QskT/c3gEdBlspKwRMv5mEqEExxpHyCSfI9uxSl+mwvHUM3yx+glTnXP0+9spettEkDVKjgwn1RxJc0RUWCM2Reyp7X9gqzVMwhVF88ZoF1FpDaSpD2SoczYx3VJiKTeJIgdoqm4h5Y7RpcYZsifplBpw6uV/t+0faWK3U54ZtjcAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Photo Credit: Lorenzo Herrera&quot;
        title=&quot;&quot;
        src=&quot;/static/0b03a15e19111c84e318f8c86b722b9a/e996b/90s-tech.png&quot;
        srcset=&quot;/static/0b03a15e19111c84e318f8c86b722b9a/17741/90s-tech.png 263w,
/static/0b03a15e19111c84e318f8c86b722b9a/52211/90s-tech.png 525w,
/static/0b03a15e19111c84e318f8c86b722b9a/e996b/90s-tech.png 1050w,
/static/0b03a15e19111c84e318f8c86b722b9a/087e3/90s-tech.png 1575w,
/static/0b03a15e19111c84e318f8c86b722b9a/8079d/90s-tech.png 2100w,
/static/0b03a15e19111c84e318f8c86b722b9a/42267/90s-tech.png 2670w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/p0j-mE6mGo4&quot;&gt;Lorenzo Herrera&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;There were multiple attempts in its history to &lt;em&gt;fix&lt;/em&gt; JavaScript.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;From the ancient ActionScript, CoffeeScript, ClosureScript and Elm, to the most recent ones Reason ML, Dart, LiveScript and many more. These languages provide a better developer experience and they are strongly typed. Some of them are object-oriented. Others are functional. At the end, the goal is the same. You write your apps in a higher level language that compiles to JavaScript.&lt;/p&gt;
&lt;p&gt;As a side note, you can use Python, Ruby, Kotlin, Scala, Swift, or even Java if you find &lt;a href=&quot;https://github-wiki-see.page/m/jashkenas/coffeescript/wiki/List-of-languages-that-compile-to-JS#ruby&quot;&gt;the right compiler&lt;/a&gt;. But there’s no benefit in starting a new JS project with such languages. Who would like to maintain such a monstrosity. But even if you ever have this need, I would recommend you to check &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/WebAssembly&quot;&gt;WebAssembly&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This is how TypeScript became a game changer. Now check it by yourself.&lt;/p&gt;
&lt;h2&gt;The Type Experiment&lt;/h2&gt;
&lt;p&gt;Read the following code and try to spot any mistakes.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; favorites &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;tracks&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;totalLength&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;addToFavorites&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; artist&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; album&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  favorites&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tracks&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; artist&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; album&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  favorites&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;totalLength &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; favorites&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;totalLength &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;length
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;artists&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Queen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;album&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;A Night At The Opera&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;5.45m&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;addToFavorites&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;track&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Ah, yes! I forgot to mention that because the author of this post is a music lover, we are going to build a music player in the following posts. This code snippet is basically handling our favorite tracks.&lt;/p&gt;
&lt;p&gt;Some of you may think to copy this snippet and paste it into your browser’s console. But I will save you from this extra effort. This code has zero errors.&lt;/p&gt;
&lt;p&gt;You will find the answer at the end of this post.&lt;/p&gt;
&lt;h2&gt;What is TypeScript?&lt;/h2&gt;
&lt;p&gt;TypeScript is at the moment one of the most popular strongly typed flavors of JS. It is &lt;a href=&quot;https://2020.stateofjs.com/en-US/technologies/&quot;&gt;highly adopted&lt;/a&gt; by many of the JS projects over the world, especially on backend applications.&lt;/p&gt;
&lt;p&gt;What makes TypeScript easy to adopt is the fact that your code is as close to JavaScript as it can be. As a superset of JavaScript, any code that runs on JavaScript, runs perfectly well on TypeScript too.&lt;/p&gt;
&lt;p&gt;The maintainers of the TypeScript language decided to stay away from custom code syntax. Its additional features are already supported or they will be supported in future versions of JS. That’s a huge benefit for developers, because you theoretically won’t need to refactor your code completely when an X feature of JavaScript becomes available, when TypeScript becomes obsolete, or when you find a better tool to handle your type definitions much more efficiently.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;There are plenty of things you &lt;em&gt;can&lt;/em&gt; do in JavaScript, that TypeScript will prevent you.&lt;/p&gt;
&lt;p&gt;For example, check the object below:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;artist&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Queen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;album&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;A Night At The Opera&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;354&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This code will not compile, because the name property doesn’t exist in the track object. But, this works perfectly on JS. TypeScript will return the following error:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Property &apos;name&apos; does not exist on type &apos;{ title: string; artist: string; album: string; length: string; }&apos;.&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, what will happen if I accidentally forget how I declared the &lt;code class=&quot;language-text&quot;&gt;length&lt;/code&gt; property and I assign a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt; to it?&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;artist&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;Queen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;album&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;A Night At The Opera&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;354&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

track&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;5.54m&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Again, this will work on JS, because nobody prevents me from changing the type of a property, but it will throw an error in TypeScript:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;Type &apos;string&apos; is not assignable to type &apos;number&apos;.&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;TypeScript noted that a &lt;code class=&quot;language-text&quot;&gt;string&lt;/code&gt; has been assigned to the property &lt;code class=&quot;language-text&quot;&gt;length&lt;/code&gt; and it will prevent me from changing its type.&lt;/p&gt;
&lt;p&gt;Here I want to mention that all I did was to rename my &lt;code class=&quot;language-text&quot;&gt;index.js&lt;/code&gt; file to &lt;code class=&quot;language-text&quot;&gt;index.ts&lt;/code&gt;. Just by changing the extension of the file to &lt;code class=&quot;language-text&quot;&gt;.ts&lt;/code&gt;, I helped my text editor to become a little bit smarter:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/1eb55f0b0cf4a14522134e0d9086c5bc/2dc7d/typescript-error.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 33.460076045627375%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAABYlAAAWJQFJUiTwAAABF0lEQVR42qXQX1OCQBQF8LXMMSprLGE1CBsQBAFhEGoyQh76Y7CYTU19/y9yWpienOyhHn6z9+nsuZf4nTXM/TWGJ284766hnL7+C7G6n9CPPqC2CiitErTJ0N8tOQb6B6TffOEN32EdMmj9FZROCeW4xKBdQCR5rUeev+eiJv2CyO0csZViZt8hjFJ4kzkCK0Fgp5iMbuAYc7jmbc0YxrD1K8jCPQ9mWwKFJeKpj7nnIQx9xNczJFGIOPLh+hYcz+TGNdsx4ExHfIuMB5aQGj8EXgoMyUWGRzVHpD4hcAsE4wK+xmArD9DEFLqUQZMW9avTBdSDJeQ9BpnffROhDYZBZWcF2lhB5HPljP/W42tVTTZVzegWX+/VzJLj6FcvAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Screen Shot 2021-11-07 at 12.50.50 PM.png&quot;
        title=&quot;&quot;
        src=&quot;/static/1eb55f0b0cf4a14522134e0d9086c5bc/e996b/typescript-error.png&quot;
        srcset=&quot;/static/1eb55f0b0cf4a14522134e0d9086c5bc/17741/typescript-error.png 263w,
/static/1eb55f0b0cf4a14522134e0d9086c5bc/52211/typescript-error.png 525w,
/static/1eb55f0b0cf4a14522134e0d9086c5bc/e996b/typescript-error.png 1050w,
/static/1eb55f0b0cf4a14522134e0d9086c5bc/087e3/typescript-error.png 1575w,
/static/1eb55f0b0cf4a14522134e0d9086c5bc/2dc7d/typescript-error.png 1760w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;TypeScript doesn’t stop there.&lt;/p&gt;
&lt;p&gt;I can specifically set the types of my properties, without even setting an initial value. What if I had a &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt; of music tracks:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  album&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  length&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Yes, my friends. This doesn’t look like JavaScript anymore.&lt;/p&gt;
&lt;p&gt;It’s important to understand that classes exist in JS, but types exist only in TypeScript. When your code is transpiled to JS, all the type definitions will be removed.&lt;/p&gt;
&lt;h2&gt;How to integrate TypeScript in your projects?&lt;/h2&gt;
&lt;p&gt;The easiest way to use the TypeScript compiler is to install the TypeScript plugin on your favorite text editor. We’ve seen already that TypeScript has its own file extension &lt;code class=&quot;language-text&quot;&gt;.ts&lt;/code&gt;. Your editor will compile all these files using the TypeScript compiler automatically for you.&lt;/p&gt;
&lt;p&gt;The editor of my choice is &lt;a href=&quot;https://code.visualstudio.com&quot;&gt;Visual Studio Code&lt;/a&gt;, which has excellent built-in support for TypeScript. By simply pressing the &lt;em&gt;Run&lt;/em&gt; button you will find in the debugging panel, VS Code will compile your TypeScript code and if it has debugging capabilities built-in, it will even run it for you and show the result in a browser window or in the Node.js runtime.&lt;/p&gt;
&lt;p&gt;There are plugins for Eclipse, IntelliJ, Sublime Text, Emacs, Vim, and many more. They provide the same functionality, but some may have less debugging options. This doesn’t mean you can’t use a plain text editor, like Notepad++, but you will need to do some manual work to run and debug your application.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/03b992a26a2a6162dfca4e60197ccce7/42267/nintendo.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAADCklEQVR42nWTS29bRRiG/ReiQG2f+80+zrF9fGw3F9uxT2ySOE6cNDgYhZY0TqiSqEWIS7NJRFErhFSRBClcihAgFgTBIogNkEURAiF1yYpfgITUFRIsQX0Y9yIkFBavZr5ZPO/7fTMTsSUVS9JIKRYF1SBQdF7xq7yQqZKSTQpakqoxRMfOcT2o8Xa1y3xmktALqfnLOEYD13qCrL/C0vJNIo6A2bKGJYCGpNNxcuwVppi10oyoFtNGiiUrw1qywFVvlBtByMHwDPulRab8DqYyTEIbxVZHkc/kHgJFSkNJklSHeNrOUNNsMopNqLtURMIF06NppGnZPhtDI+xkK/SyDTyzjCVnsBVfAAMc7T5QJyHr6HEZR04SqC6eIloXsIaepCSAM2aG8yJxW6wjeloAUmiqhyUCJDRRq2mx79cuESsaxYnGSCdD/GBNzKNO2qzhWxOcNQqUzICaM8piosjFRI4tN0/ZyKJEFbRBCW1AQhmIoZ3pd2kT8d06leo1ytXXyQUb5PObFAsvk889T8qeFi15mJLLWXeeVnGD7vgOrdJlOgvvcL73ERfWD2h3XiUIzqEMioSNuQ/IFy/hmnXCYIulxiEX5z6h99QJw/6mgNYpFC4Tjt1gbnyf5dnPWai/wdrGT+wf3ePLH/7ms9t/8enJn+zt3SbiBW08f5Zs0BWpVqnktzhXu05n+hb15iHd1Ts8ufQNMxNvMl/bpTt9yMryj/Su3OHS9vd8eHyX4+/+4Nbxb7x3dJdIPDaIooh5GAlU3UHREuhWETfdpDzxEpOtm1RrOww5ZUq5BZrhLr31n1m/+gvbb/3K+1/f4+CL32k/c0RYOSSiqNp9oCxLQgqK3N/LSFKMWHSAeOwxVPHgVc1E01xSSXFh/iJjEy8y9+y7rG5/S3vlK8aGd2m3PhZAAftXqlDfQH8Aeaj+uSqMVVUVZnFhFiUeHST6uOhOtsVFXmG8sseF3sl/gY+gj8CnqW/Wl/nAUNRBsEku8xxTk9dOA56W+P+kookvm0o0ceJZxmuv8Q99V7fjNvyl8gAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Photo Credit: Terry Lee&quot;
        title=&quot;&quot;
        src=&quot;/static/03b992a26a2a6162dfca4e60197ccce7/e996b/nintendo.png&quot;
        srcset=&quot;/static/03b992a26a2a6162dfca4e60197ccce7/17741/nintendo.png 263w,
/static/03b992a26a2a6162dfca4e60197ccce7/52211/nintendo.png 525w,
/static/03b992a26a2a6162dfca4e60197ccce7/e996b/nintendo.png 1050w,
/static/03b992a26a2a6162dfca4e60197ccce7/087e3/nintendo.png 1575w,
/static/03b992a26a2a6162dfca4e60197ccce7/8079d/nintendo.png 2100w,
/static/03b992a26a2a6162dfca4e60197ccce7/42267/nintendo.png 2670w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/8OCgMK2SSOo&quot;&gt;Terry Lee&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;Type checking on demand&lt;/h3&gt;
&lt;p&gt;You can also choose to enable TypeScript only in specific files. Let’s say that you are working with an existing application that already runs in JS and you want to slowly migrate it to TypeScript.&lt;/p&gt;
&lt;p&gt;Here, I’m using the &lt;code class=&quot;language-text&quot;&gt;.js&lt;/code&gt; extension, but because I added the &lt;code class=&quot;language-text&quot;&gt;//@ts-check&lt;/code&gt; comment, my editor treats this file as a TypeScript code:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/9fb82d95926bce923737c04888e58b08/1dbe8/ts-check.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 44.86692015209126%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAABYlAAAWJQFJUiTwAAABE0lEQVR42p2RS0+DQBSFpxQqDy0U2/IaKJIasWlcNLowcemGADOU2qr//4ccb0nUuAMXX3I3c+6Z77K1VeHJltjoApvZCcn1AXze/huW6AW28zfkzhH8okGgSwSGIOQvtKwvLFAqRJMGK/OEWD8gnUj4owpLVv7gsao3zFcoWZXIjHfs7A88uAXyuEXq0hJXYjVrEGpiQOCohj+mh6rAzjpiZbVYpy2yRYP4SiKZCnBTdK17NqwRqg1uNIkX5xP30z3uPIGIQkK9Br8UXfB5ce+GkdbAG9O3aF4qJXyl/ONwiEcWUlCs77E1X/FoF3h2WuTUKFsI3HqkwqyRGFVv2Hcyp1acLp7QkULScFYRqHXnbghfijMCMDdNaVYAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Screen Shot 2021-11-07 at 1.30.26 PM.png&quot;
        title=&quot;&quot;
        src=&quot;/static/9fb82d95926bce923737c04888e58b08/e996b/ts-check.png&quot;
        srcset=&quot;/static/9fb82d95926bce923737c04888e58b08/17741/ts-check.png 263w,
/static/9fb82d95926bce923737c04888e58b08/52211/ts-check.png 525w,
/static/9fb82d95926bce923737c04888e58b08/e996b/ts-check.png 1050w,
/static/9fb82d95926bce923737c04888e58b08/1dbe8/ts-check.png 1374w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h3&gt;JSDoc&lt;/h3&gt;
&lt;p&gt;Another approach to enable TypeScript in your JS application, is to use &lt;a href=&quot;https://jsdoc.app&quot;&gt;JSDoc&lt;/a&gt;. JSDoc acts as an API documentation generator for your JavaScript project, similar to &lt;a href=&quot;https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html&quot;&gt;Javadoc&lt;/a&gt; or &lt;a href=&quot;https://www.phpdoc.org&quot;&gt;phpDocumentor&lt;/a&gt;. You can add documentation comments directly to your source code, right alongside the code itself. The JSDoc tool will scan your source code and generate an HTML documentation website for you.&lt;/p&gt;
&lt;p&gt;Here’s an example of how JSDoc is working:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/**
 * Represents a track.
 * @constructor
 * @param {string} title - The title of the track.
 * @param {string} artist - The artist of the track.
 * @param {string} album - The album of the track.
 * @param {number} length - The length of the track.
 */&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Track&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; artist&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; album&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Your text editor will now display useful information about this function as I type:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/07b320ae62e1b569879ae166bad5b8e5/10ab7/jsdoc-with-typescript.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 42.58555133079848%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAABYlAAAWJQFJUiTwAAABpUlEQVR42o2Sa28SURCGd4EA1dgmYmEBuyzsfSmLbHYBIxEwRtNyWSpe+sGYmOhP8EO9/PbHYWuUxkv88OSdyZnznjlnjtIopOgHrzgpbWkWtzQKF8KGqrKkpiyE5U3UX7GmXrO/ruwKzDtvsY/eYd/e0ios0fMpjVxKLbdByzTNVJO8qq44lo07qsp1/JuhX/mIf+89o2DO42hC4o4ZuA8JrCGhNxOmmQ66TxhITdeaELrTTKPTGUZlnpnvvBQtt8KrfMgMH4QjknFMPIyIkwG9MCDoeni+gx+4eIK/h+e7dEObtjYSQ7mJukCp5zfopTfo5Q1Op0+/3ydJRsTxkChK8LwA03SwrB/ciF1sx8So7hnqpTWtgwtqMohh8ojJdCzdRZz2fDpmC8tpy6bOn7FNHL+FUUvkLVN5XzG0D1Pi43PCo2f0nRU984zAeE5Hm2I15pj1v7Nbt5sz2nefcr+w5qS4kivL1PTcAlMS+/CTTPybTPwrRvkzevHqnxjlK9q3vtAsXFLPnwnSYTX7AgthJae8pFW+lOLXNNQX/42mrn9+m+9YVQmP5g9s1AAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Screen Shot 2021-11-07 at 1.40.13 PM.png&quot;
        title=&quot;&quot;
        src=&quot;/static/07b320ae62e1b569879ae166bad5b8e5/e996b/jsdoc-with-typescript.png&quot;
        srcset=&quot;/static/07b320ae62e1b569879ae166bad5b8e5/17741/jsdoc-with-typescript.png 263w,
/static/07b320ae62e1b569879ae166bad5b8e5/52211/jsdoc-with-typescript.png 525w,
/static/07b320ae62e1b569879ae166bad5b8e5/e996b/jsdoc-with-typescript.png 1050w,
/static/07b320ae62e1b569879ae166bad5b8e5/10ab7/jsdoc-with-typescript.png 1552w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This is a premium feature that you find only in sophisticated IDEs like Xcode or Visual Studio.&lt;/p&gt;
&lt;h3&gt;The tsc compiler&lt;/h3&gt;
&lt;p&gt;So far, we have been using the built-in features of our text editor, to compile our TypeScript code. But what is happening in the background? I would like to introduce you to the TypeScript compiler. Visual Studio Code has built-in support for TypeScript, but it doesn’t include the TypeScript compiler. You will have to install it yourself.&lt;/p&gt;
&lt;p&gt;You can install the TypeScript compiler by running the following command:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-bash line-numbers&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;--global&lt;/span&gt; typescript&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Like every other JS library, TypeScript works with Node.js. It is being distributed via the Node Package Manager (call me &lt;code class=&quot;language-text&quot;&gt;npm&lt;/code&gt;). This means, that in order to run the TypeScript compiler, a latest version of &lt;a href=&quot;https://nodejs.org/en/&quot;&gt;Node.js&lt;/a&gt; must be installed on your system.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Now let’s try it in practice. Somewhere in your development directory, create a file called &lt;code class=&quot;language-text&quot;&gt;index.ts&lt;/code&gt;. We will use it to write our first TypeScript example.&lt;/p&gt;
&lt;p&gt;For the sake of this demonstration, we can enhance our &lt;code class=&quot;language-text&quot;&gt;Track&lt;/code&gt; class, that we mentioned before, to include a &lt;em&gt;constructor&lt;/em&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  album&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;
  length&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;title&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; artist&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; album&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; title&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; artist&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;album &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; album&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; length&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can use our newly created class to instantiate an object:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; myTrack &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token string&quot;&gt;&apos;Queen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token string&quot;&gt;&apos;A Night At The Opera&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token number&quot;&gt;354&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, navigate to this directory with your terminal, and use the command to compile your TypeScript code:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;typescript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-typescript line-numbers&quot;&gt;&lt;code class=&quot;language-typescript&quot;&gt;tsc index&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ts&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, take a look at the directory the file is located. You will notice that the TypeScript compiler has generated a new file called &lt;code class=&quot;language-text&quot;&gt;index.js&lt;/code&gt;. The output depends on the compiler configuration file. Here are the contents I got when I ran it:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;js&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-js line-numbers&quot;&gt;&lt;code class=&quot;language-js&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; Track &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/** @class */&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Track&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; artist&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; album&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; length&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;title &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; title
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;artist &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; artist
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;album &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; album
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;length &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; length
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; Track
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;var&lt;/span&gt; myTrack &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Track&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;token string&quot;&gt;&apos;Bohemian Rhapsody&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token string&quot;&gt;&apos;Queen&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token string&quot;&gt;&apos;A Night At The Opera&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token number&quot;&gt;354&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The code is quite similar to the one we had in the &lt;code class=&quot;language-text&quot;&gt;index.ts&lt;/code&gt; file. You will notice that the &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt; declaration has been replaced by a variable, and the &lt;code class=&quot;language-text&quot;&gt;constructor&lt;/code&gt; method, with a &lt;code class=&quot;language-text&quot;&gt;function&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;That’s due to the limitation JavaScript has for classes. Although it has been added in recent versions of EcmaScript, some of the browsers that are still used on production are still in a previous version.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;I’m talking about you, Internet Explorer.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;If you are writing a backend application, you can try to run this example via node:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-bash line-numbers&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;node&lt;/span&gt; index.js&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Make sure, you are using the &lt;code class=&quot;language-text&quot;&gt;.js&lt;/code&gt; file, because Node.js doesn’t have any clue about TypeScript. At least by default.&lt;/p&gt;
&lt;p&gt;If you are targeting a client-based app, the &lt;code class=&quot;language-text&quot;&gt;index.js&lt;/code&gt; file is the one you are supposed to share with the world:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-html line-numbers&quot;&gt;&lt;code class=&quot;language-html&quot;&gt;&lt;span class=&quot;token doctype&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;!&lt;/span&gt;&lt;span class=&quot;token doctype-tag&quot;&gt;DOCTYPE&lt;/span&gt; &lt;span class=&quot;token name&quot;&gt;html&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;html&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;script&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;text/javascript&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;./index.js&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token script&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;script&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;html&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;body&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  ...
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;body&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;TypeScript knows already which features are supported by which browser and it is smart enough to transpile our code to a version of JavaScript that will always work. We also have the option to configure which version we are targeting, in the &lt;code class=&quot;language-text&quot;&gt;tsconfig.json&lt;/code&gt; configuration, which we will cover in a minute.&lt;/p&gt;
&lt;p&gt;Now you may ask, what if I change this &lt;code class=&quot;language-text&quot;&gt;js&lt;/code&gt; file? Will this cause any issues? Well, in general, you don’t have to. You are not supposed to touch these files. Your goal is to write your source code in your &lt;code class=&quot;language-text&quot;&gt;ts&lt;/code&gt; files and then you have to run the &lt;code class=&quot;language-text&quot;&gt;tsc&lt;/code&gt; command every time to generate a new version of the &lt;code class=&quot;language-text&quot;&gt;js&lt;/code&gt; file. From now on, you can forget about these automatically generated &lt;code class=&quot;language-text&quot;&gt;js&lt;/code&gt; files.&lt;/p&gt;
&lt;p&gt;To avoid running this command manually, every time you change your code, you can use the watch option:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-bash line-numbers&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; tsc &lt;span class=&quot;token parameter variable&quot;&gt;--watch&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now the TypeScript compiler will watch for changes and every time you save any of the ts files in this directory, it will re-transpile the code for you.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/498e279277a9a34fa29da20ff227501a/42267/phone.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAADfklEQVR42h2SW2yTZRjH33ZjiW2/9vva9dyuh3VdD1/PXbexDaVkmcLAgBMVcSKbOE8jynAaY9wGOjQxmQdEI6KioniIhAtDIPHCC41mUeMhXBi90BASo9ELkxmZ/nzw4s2bN3ne3/P//59HuX2jqPh+VOJhVPo5VPkcqv45qvEFau15rAPfy/tTbOUzRIqnSJsnyWVfJZ9+BjM5jxm7j3x0D/nwOBm/sLTgDajUIqr7MKr0Iar/PGrwB9RVP2Nd/yOtjTO01D/DU/+EaOUcmeJpKtnjVAVYTT5KNXY/peidmJFd5EPbUbbQOCpzBFX4QGDfoJoXUBt/wbrlNyy110X9PViyz6JVz5KufkzVfI/ezDEa6aepJxeoi7tKx17M6B3kwrei2iK3o8y3sfSIzZGLOMZ+p3N8Beemb0X1ApbMPCq5FxWbxJ16jFruLfqzr9DX/QK1rqeoiO1q/AEKHXeTE5WqpeNelFhRzZ+Ij//J9dMr3DazSnjzsjQ6LMA5VMctqOAGVHs/sfQSjcIpegV62XY5OUdNbBdEYT68E2VNzEj4X1OfXGF+8RKPH/qb2YVVAiNnJYonsSTvwhq8Gou/SdO4kWbyKMXCO6LwRaqpJ0Tdg1RFlBmdJBfagVLxOZLbLnLiTTj97ipHj1wS8F84ygdR0Z1Yo1tY4xui6B1jzNjNUPo45dwb9KSfp9y5SCnxkGQ4TT4yIcCbUO6hj1j+Er5b/peXjq2y78A/jM1CoP8AyijR5l/HGu8gI8YOpvT9pHOvUc68LPktUZT8umLTFGVtcqGbyQa3Six9J5lZukBjzx84R3/F2nOC1s5daNEmmpHBcNexGznWupoMh2bJXx5E50HqiUdkZfbJukwIaDvdvmvI+jdJhn7x7ZdJmu+jer/CGp/A5vSg6VF0OSm9REf7kBRvpShLXJS8KtEpquHdVCWzSnCMemCUvGcd3Z5BlO4dxha8DquMvSUxRWuwjzZfCYc7ieYK4XPFaNe7SPqGKchnU2qLgW1yrqXi30zau4EBrY+Cq0aXXkH5jH7a29ejeTdic6exGSFsnk7snhQOI4rmDGB3GOhagIwoyHuuxJTbdA8QdTeIOLMMXlGiy2WScuYEqJsEjF7cegGb7sfh9GHXvNhdQbEdxilATfP/D424uskaPWT1KjGnNLfrZGxxavY8cS1FzJHgPywLyGPftIdXAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Photo Credit: Ben Blennerhassett&quot;
        title=&quot;&quot;
        src=&quot;/static/498e279277a9a34fa29da20ff227501a/e996b/phone.png&quot;
        srcset=&quot;/static/498e279277a9a34fa29da20ff227501a/17741/phone.png 263w,
/static/498e279277a9a34fa29da20ff227501a/52211/phone.png 525w,
/static/498e279277a9a34fa29da20ff227501a/e996b/phone.png 1050w,
/static/498e279277a9a34fa29da20ff227501a/087e3/phone.png 1575w,
/static/498e279277a9a34fa29da20ff227501a/8079d/phone.png 2100w,
/static/498e279277a9a34fa29da20ff227501a/42267/phone.png 2670w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Photo Credit: &lt;a href=&quot;https://unsplash.com/photos/LR5eS1C9IUU&quot;&gt;Ben Blennerhassett&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Our first TypeScript project&lt;/h2&gt;
&lt;p&gt;Excited about types? I know, the next thing you are wondering is how you can force them in your applications. Well, my friends, it only takes a line of code.&lt;/p&gt;
&lt;p&gt;Back to our example, you have to delete the newly generated &lt;code class=&quot;language-text&quot;&gt;index.js&lt;/code&gt; file. Then, run the following command:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-bash line-numbers&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; tsc &lt;span class=&quot;token parameter variable&quot;&gt;--init&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This command will generate a new file &lt;code class=&quot;language-text&quot;&gt;tsconfig.json&lt;/code&gt;. This file includes all the configuration that your TypeScript compiler needs, to do its job.&lt;/p&gt;
&lt;p&gt;Here are the most important options:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;json&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-json line-numbers&quot;&gt;&lt;code class=&quot;language-json&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;&quot;compilerOptions&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;/* The transpiled code will be in ECMAScript 5 */&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;target&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;es5&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;/* We will use commonjs to modularize our code */&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;module&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;commonjs&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;/* The transpiled code will be stored in the subfolder /js */&lt;/span&gt;
    &lt;span class=&quot;token property&quot;&gt;&quot;outDir&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;./js/&quot;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you will observe, there are plenty of options to be commented out. For simplicity, I have omitted all the other options. You may want to read the comments and customize them according to your needs. Have a look in the &lt;a href=&quot;https://www.staging-typescript.org/tsconfig&quot;&gt;official documentation&lt;/a&gt; to find more about the available options. We will cover some important ones in the upcoming posts.&lt;/p&gt;
&lt;p&gt;Now every time you run &lt;code class=&quot;language-text&quot;&gt;tsc&lt;/code&gt; to this folder, the TypeScript compiler will read this file and it will adjust its settings accordingly. This works for other projects you are contributing to.&lt;/p&gt;
&lt;p&gt;In case this file is absent, or some of the options are not listed in the file, the default options will be the values that you see in the comments. For most projects, these defaults are good enough.&lt;/p&gt;
&lt;p&gt;By having this configuration file in your project repo, you ensure that all the developers on your team will have the same &lt;code class=&quot;language-text&quot;&gt;tsc&lt;/code&gt; configuration, and the project will compile correctly everywhere. The same applies for your CI pipeline.&lt;/p&gt;
&lt;h2&gt;The Online Playground&lt;/h2&gt;
&lt;p&gt;One more tip before I let you go.&lt;/p&gt;
&lt;p&gt;If you just want to try out the TypeScript compiler, without installing it locally on your system, you can try the &lt;a href=&quot;https://www.typescriptlang.org/play&quot;&gt;TypeScript Playground&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Try to write a TypeScript code on the left side and press &lt;em&gt;Run&lt;/em&gt;. You can see the transpiled JavaScript code on the right.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/d9bc7caf7a5302fc40ebbc2fec9a3aa5/83861/typescript-playground.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 64.63878326996198%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABYlAAAWJQFJUiTwAAACRUlEQVR42oWS22/SYBTAa7gURsRkJAIFyqXcWnqj5TaYG2Qhq44t6gtGJfoHaHTDPhs396KND/q3LmnyfeF4WjpnFqYPv5zTk36/nnO+MtPpdDQ9mv3cPTj+PphYzmDfcibWM2c4eeIMEC9OZ3NnejJ3evvr2qPDp5hbAYfOaGx9G06sX+ZgPGIOXtn28bsfMFqc0+HiAoaLr7D39hJ231yC97zz+hy6Lz0uwHzx5Q/G/PMNJ+9p7/kZNMaLJWOa5nJvPIFuv+8Odoak1x8Qw+yQTrdHFFUlqqYTVW8TqdUioigRUWoRVdWJ3m4Tza/LXt3V2wYomnbK5DnOlhUVGrJGS6US+BSLazCv1WoYy5BIPoD7ySQktrYgkUhALBaDSCQM4TASClGWZSH1MLVkOC5t15sSaEaHNhpN/3C5UoFKRYAiCgWhChyXg3sRFra3UxCPx4FhmNvQUCgEqRQKeZ63hVodcBRaq9egUCgA1ny8XBAE4IsliEZZiMbifmexaBTYaMQnxvr5ukNPmEehLInQatZpRahAEUe9xpOWy2XAfYGmaaBqqh/X3OSqqtI2viNJEgrzebtaFXBnPC0Gnf3dobdHwzAR405QRrvd7lrIcXk76Ibyt4Q8vxZ6X/8Xuq7TTqdzLeQ+BUKCrDYhy8oKO1nh4Y2gkATCM0+4DMZzEbKJlqwQFBI8fBduIDxlcrncx+ASrlDsbgJ/aheX7+LyN6IoyhV2CaIofmCy2SyP0iO8HAvj401kMpn/YaXT6RlOW/gNQaVarK01KEcAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Screen Shot 2021-11-07 at 4.40.17 PM.png&quot;
        title=&quot;&quot;
        src=&quot;/static/d9bc7caf7a5302fc40ebbc2fec9a3aa5/e996b/typescript-playground.png&quot;
        srcset=&quot;/static/d9bc7caf7a5302fc40ebbc2fec9a3aa5/17741/typescript-playground.png 263w,
/static/d9bc7caf7a5302fc40ebbc2fec9a3aa5/52211/typescript-playground.png 525w,
/static/d9bc7caf7a5302fc40ebbc2fec9a3aa5/e996b/typescript-playground.png 1050w,
/static/d9bc7caf7a5302fc40ebbc2fec9a3aa5/087e3/typescript-playground.png 1575w,
/static/d9bc7caf7a5302fc40ebbc2fec9a3aa5/8079d/typescript-playground.png 2100w,
/static/d9bc7caf7a5302fc40ebbc2fec9a3aa5/83861/typescript-playground.png 3256w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;The Solution&lt;/h2&gt;
&lt;p&gt;Remember the JS example I shared with you in the beginning of this post? Let’s copy that code snippet to our TypeScript file to see what we will get.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/d3723191ccdf6207e3444d184d7466ee/e40ed/code-challenge-solution.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60.07604562737643%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAABYlAAAWJQFJUiTwAAABvElEQVR42p1T70+bUBSlHQL9BVULPKClXUPX6uo0M3GNH5Zsy1ykVPqg1nbx//83jgfUxS37YP1wc18ucDjnnvMUUVnAq95gUNtg0sjxsfETk/ovdLUUrpLsXEoJqC7xXltiZt3h3Npi1MwRaAmctwIGe0v46g1cnv2ScQJRmfOFeQnqsNv/VKfsMcT/AP13KUJ9hVFjDd+6hehs4bY36LCP6jmOjBwT47Ef1zJMOTthnTbXCAwJt/oC0KsWkiVGeoyv+98xa//AF2uNy8MtzsQ9pi2JCzPGuZngc+sWl/zRzNoQLMfFfoKpucJYz6jumSENCTQJvzpHqFyzYoSVGENKHvLssLzKNavoSbmSomw+F5x3SahLjD8MAy2joxKhcYeIcsbNK4RNCUddwNXIXnvc7fMHfxu1eNrxC8k+Hfa4wz6N+VRfIWotEQUZhg6d70j02ymKtbza5YKdtycRMTbfDn8jMmnAeIUPvYxMUwwO0hJUKK8DVQIyLHYYMjZH7D1K7bUWEAx2IUWUEdohh4XkIodOTeLAyGDrjAFnA5ULV3cPtxJQbpfG2MyVa65h8/r5jQwn+hX6b7gtD3sJVtQUQGBPAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Screen Shot 2021-11-07 at 3.48.37 PM.png&quot;
        title=&quot;&quot;
        src=&quot;/static/d3723191ccdf6207e3444d184d7466ee/e996b/code-challenge-solution.png&quot;
        srcset=&quot;/static/d3723191ccdf6207e3444d184d7466ee/17741/code-challenge-solution.png 263w,
/static/d3723191ccdf6207e3444d184d7466ee/52211/code-challenge-solution.png 525w,
/static/d3723191ccdf6207e3444d184d7466ee/e996b/code-challenge-solution.png 1050w,
/static/d3723191ccdf6207e3444d184d7466ee/e40ed/code-challenge-solution.png 1378w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This code will not work, but believe it or not, it is perfectly legit in JavaScript. By that, I mean that we are technically not violating any of its rules.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Not even &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode&quot;&gt;strict mode&lt;/a&gt; can save us.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;At line &lt;code class=&quot;language-text&quot;&gt;#7&lt;/code&gt;, we are using an array method that doesn’t exist. The correct method is &lt;code class=&quot;language-text&quot;&gt;push()&lt;/code&gt; not &lt;code class=&quot;language-text&quot;&gt;add()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;At line &lt;code class=&quot;language-text&quot;&gt;#8&lt;/code&gt;, we are adding a string &lt;code class=&quot;language-text&quot;&gt;&apos;5.45m&apos;&lt;/code&gt; to the number &lt;code class=&quot;language-text&quot;&gt;0&lt;/code&gt;. Again, this is valid JavaScript code. The JavaScript compiler will automatically coerce the number to a string and the result will be &lt;code class=&quot;language-text&quot;&gt;&apos;5.45m&apos;&lt;/code&gt;, which is far ahead of what we wanted to achieve here.&lt;/p&gt;
&lt;p&gt;Ultimately, at line &lt;code class=&quot;language-text&quot;&gt;#13&lt;/code&gt;, we have a typo in the property name &lt;code class=&quot;language-text&quot;&gt;artist&lt;/code&gt;, which must not be plural.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Already a JS dev? You will be surprised at how many mistakes TypeScript will find in your code. Just give it a try.&lt;/p&gt;
&lt;p&gt;I hope that now you have a better understanding of what TypeScript is, how it works, and how you can integrate it in your applications.&lt;/p&gt;
&lt;p&gt;If you have any questions, don’t hesitate to drop me a text, you know where you will find me. If you want to help the author fix common mistakes and errata, feel free to create a pull request on GitHub. It would be highly appreciated.&lt;/p&gt;
&lt;p&gt;Until the next one, happy type checking! 🧐&lt;/p&gt;
&lt;p&gt;Cover Credit: &lt;a href=&quot;https://unsplash.com/photos/R5A_YlcSJwA&quot;&gt;Shubham Dhage&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Fun Facts About Switzerland Part IV]]></title><description><![CDATA[How you answer a phone call in Switzerland? How you can buy goods without paying a rappen? Why people in Switzerland know what time and day will wash their clothes next year?]]></description><link>http://www.nicotsou.com/fun-facts-about-switzerland-part-iv/</link><guid isPermaLink="false">http://www.nicotsou.com/fun-facts-about-switzerland-part-iv/</guid><pubDate>Wed, 24 Nov 2021 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;How you answer a phone call in Switzerland? How you can buy goods without paying a rappen? Why people in Switzerland know what time and day will wash their clothes next year?&lt;/p&gt;
&lt;p&gt;In case you missed my previous posts, this is the fourth part with fun facts about Switzerland. It’s pretty biased, not sophisticated whatsoever, and I will do my best to not include any stereotypes.&lt;/p&gt;
&lt;p&gt;Cheese? 🧀&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Before we start, I just want to drop this here:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/d34495e3d480efdbdf784cc066cf7b07/567ac/swiss-keyboard-layout.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 30.798479087452467%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAABaElEQVR42jVQy0rDUBDNx4gWa9O806R5v5smaUyrKxFXIq5EBBF3gt/n1q3URe/GijsNvePcC10cZpjHOXNGqJr21fGCrRfEmyBKSJIXJMtnJC9KUpQVKauaLLoVadolwVnSLDrSLs94bNqO1BjLebOZzaufi8urFyHJiq2qGTCxHWpNXXBcH1CAw/NDcBF+GIMXRICCMMV+vTiFMM4ABWE2r1mPsj0UXgteEH4NjoegGZOdpptUVjQqqzpVMTetKVU0g2KP6qZFLRSVVY36YULNiU2RnKIwxb2dpGiQ5sWHYDvu9uBwAOJYpiNRAklWAQmBDYxZjhHJeQ0X4QjFr29u4fHpmSNKMhAlhbLZOM3Xgh8k38fDES+KssKJ9oR7cryQkzHy4UiEu/sH6FbnPOK/+e4JHoMXfwpRkr6zH6KdP7TYo7UeSXq0yqHqRu94Ye8GUW87Hgf+jtfQYh/GKav9sv9nefH2D6xVzFr/Y/RIAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;The Swiss keyboard layout&quot;
        title=&quot;&quot;
        src=&quot;/static/d34495e3d480efdbdf784cc066cf7b07/e996b/swiss-keyboard-layout.png&quot;
        srcset=&quot;/static/d34495e3d480efdbdf784cc066cf7b07/17741/swiss-keyboard-layout.png 263w,
/static/d34495e3d480efdbdf784cc066cf7b07/52211/swiss-keyboard-layout.png 525w,
/static/d34495e3d480efdbdf784cc066cf7b07/e996b/swiss-keyboard-layout.png 1050w,
/static/d34495e3d480efdbdf784cc066cf7b07/087e3/swiss-keyboard-layout.png 1575w,
/static/d34495e3d480efdbdf784cc066cf7b07/567ac/swiss-keyboard-layout.png 1675w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The Swiss keyboard layout&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This is one of my worst nightmares. Not only is it QWERTZ instead of QWERTY and every time you confuse the letter &lt;code class=&quot;language-text&quot;&gt;Z&lt;/code&gt; with &lt;code class=&quot;language-text&quot;&gt;Y&lt;/code&gt;, it is a mixture of German and French keyboard layouts. You can switch to English of course.&lt;/p&gt;
&lt;p&gt;I thought as a developer, I was very familiar with the keyboard and this layout change will not affect my typing. I was wrong. Good luck finding where the special characters are located. The trick I have found is to press any key just to see the output, until I find the one I’m looking for. Such an entertaining activity to try at work.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Now I will teach you how to answer phone calls in Switzerland. Usually, when you receive a phone call, your first reaction is to say something like “Hello”.&lt;/p&gt;
&lt;p&gt;Nope. This will not work in Switzerland.&lt;/p&gt;
&lt;p&gt;Here in Switzerland, people are used to answer with their family names. Yes, this includes every stranger who’s calling you. If you don’t pronounce your last name, the other person will be confused. They will try to identify with whom they are talking to. Think of it as if you had to press slide to unlock, but with your voice.&lt;/p&gt;
&lt;p&gt;Since I’m not used to this habit, I spend one minute in every phone call, waiting for them to pronounce my family name correctly.&lt;/p&gt;
&lt;p&gt;“Is this Mr. Tsourekitibitidis?“.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Now let’s talk about God.&lt;/p&gt;
&lt;p&gt;If they ever ask you what is your religion in Switzerland, be a bit more skeptical about what you will answer. They don’t really care in which God you’re praying, they actually want you to pay the church tax. Yes, even God itself here requires money.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/7a7c2fdf80abc53a4c32306b9a7a22c0/14770/henrique-ferreira-V7GrFTVwdko-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIEAwX/xAAVAQEBAAAAAAAAAAAAAAAAAAAAAf/aAAwDAQACEAMQAAAB0eBJbznB/8QAGhAAAwADAQAAAAAAAAAAAAAAAAECBBMUMv/aAAgBAQABBQLpo3seQR6Y6cn/xAAVEQEBAAAAAAAAAAAAAAAAAAAQEf/aAAgBAwEBPwGH/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAGRAAAgMBAAAAAAAAAAAAAAAAACEQEUHh/9oACAEBAAY/AsHRyUf/xAAdEAEAAgEFAQAAAAAAAAAAAAABABEhMUFRcYGR/9oACAEBAAE/IX4+IU2Sa4MwR3PEdUImnuLU6J//2gAMAwEAAgADAAAAENfv/8QAFxEBAAMAAAAAAAAAAAAAAAAAAAERIf/aAAgBAwEBPxDSof/EABURAQEAAAAAAAAAAAAAAAAAABAR/9oACAECAQE/EKf/xAAcEAEBAAICAwAAAAAAAAAAAAABEQAhMUFhcdH/2gAIAQEAAT8QShOx8cfoBpSPVyeWxY/bEXQIrDHHqKOOsoKgMg7z/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Credit: Henrique Ferreira&quot;
        title=&quot;&quot;
        src=&quot;/static/7a7c2fdf80abc53a4c32306b9a7a22c0/9ecec/henrique-ferreira-V7GrFTVwdko-unsplash.jpg&quot;
        srcset=&quot;/static/7a7c2fdf80abc53a4c32306b9a7a22c0/5d001/henrique-ferreira-V7GrFTVwdko-unsplash.jpg 263w,
/static/7a7c2fdf80abc53a4c32306b9a7a22c0/7349d/henrique-ferreira-V7GrFTVwdko-unsplash.jpg 525w,
/static/7a7c2fdf80abc53a4c32306b9a7a22c0/9ecec/henrique-ferreira-V7GrFTVwdko-unsplash.jpg 1050w,
/static/7a7c2fdf80abc53a4c32306b9a7a22c0/39d31/henrique-ferreira-V7GrFTVwdko-unsplash.jpg 1575w,
/static/7a7c2fdf80abc53a4c32306b9a7a22c0/1f368/henrique-ferreira-V7GrFTVwdko-unsplash.jpg 2100w,
/static/7a7c2fdf80abc53a4c32306b9a7a22c0/14770/henrique-ferreira-V7GrFTVwdko-unsplash.jpg 5435w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/@rickpsd&quot;&gt;Henrique Ferreira&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now, if you are a little unlucky and your apartment is located near a church, be ready for some bell entertainment. For reasons unknown, the church bell rings multiple times per day. Sometimes it can even get stuck, and you hear it for several minutes.&lt;/p&gt;
&lt;p&gt;The perfect way to enjoy your hangover.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Now let’s address the elephant in the room; Money.&lt;/p&gt;
&lt;p&gt;OK, this is not a secret. In Switzerland, we have a lot of money. You probably know that a Big Mac costs 12.50 euros, which is something like the global measure unit for wealth. This doesn’t mean that people are rich, not even close, but it gives you an impression of how much money is being transferred on a daily basis.&lt;/p&gt;
&lt;p&gt;What’s more impressive is the fact that people trust you.&lt;/p&gt;
&lt;p&gt;When I moved to my previous apartment, I went to the famous Swedish furniture store (now that I’m a content creator, I don’t really promote brands for free) to buy everything I needed. Yes, you guessed it right, I ended up spending a large amount of money on the most useless stuff. When I went to the counter to pay, there was a mistake related to the product tags, and they promised to send me the bill via post.&lt;/p&gt;
&lt;p&gt;I paused for a minute to think what they meant. Maybe I misheard. Maybe I was confused after all this time choosing colors for the toilet cover. But when I asked them to repeat what they said, I was even more surprised by their confirmation.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/1a0aed7e6b264c8958db6635c5d58ba0/7960f/orange-payment-slip.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 50.19011406844106%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB10lEQVR42p1S2Y6bQBDk/z8rkZJNso7X3l0DPjnMfQ4MGBtwpWZQpDwHqTRNd0/1UWP41k84n9/hmT9wtX4hOq5RhxYW/wsq7wN9skcb7xEe3xAQmfeJW37GUFxQXk0EhzXi8xYyPcE4f7xgt/qCKrCALtQYhYfD9hvM31/RRDbQXvFsAzwqB1NuY8xsTLXL3BhTw1gXAX2Me+XBENEeub/jj6svzUwYhc8u9yh8U9vaT9JbdoKMTcjIJJeNPjuiZUEZH2ifdNfGxG6eTJ548cEKY+1p0rlZiJRf+SZBwvyEyt8gdzj2ZYXSe0Ptb3luULgbku9gNJFFojN6JqtOO1WpODNIf8kzOaC47vR+VAdzzbGJWbi6kacMdeG/0xmSF0RoamTuOwmOGEoH99onPHQkERxJ2SqmiPv8goY+ZavcZaIFhghtKsQxqFxyedfi3JnUqULsUqZHdMTEFbQk8exXKr2GyxehlJ9Y6KmEUbunbVQkdK1XuPYKDs+OVUcRoCtD9FWAUaYL2hSy9CFSV6NOXLS5r2OPNsFdxOiKQImyLF0JkjjvSImpyzDNTwghUNc1Wik1alGjaQQkbdlJHbsNA9q2RUV7uA0w/p3/XrrEBTMJ//f7A+qO7e11qrdYAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;This is an orange payment slip. It&amp;#39;s the most common way to pay bills here in Switzerland.&quot;
        title=&quot;&quot;
        src=&quot;/static/1a0aed7e6b264c8958db6635c5d58ba0/e996b/orange-payment-slip.png&quot;
        srcset=&quot;/static/1a0aed7e6b264c8958db6635c5d58ba0/17741/orange-payment-slip.png 263w,
/static/1a0aed7e6b264c8958db6635c5d58ba0/52211/orange-payment-slip.png 525w,
/static/1a0aed7e6b264c8958db6635c5d58ba0/e996b/orange-payment-slip.png 1050w,
/static/1a0aed7e6b264c8958db6635c5d58ba0/7960f/orange-payment-slip.png 1274w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;This is an orange payment slip. It’s the most common way to pay bills here in Switzerland.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;They did want me to leave without paying. I ended up going home with multiple-K worth of furniture, without paying a single rappen (the Swiss equivalent of a cent). Of course, in the following week I had received the bill.&lt;/p&gt;
&lt;p&gt;This is very common here. You take your car to the garage, you leave without paying. They will send you the bill at home someday in the near future. Sometimes they don’t even tell you what they fixed and how much you are supposed to pay. It takes extra effort to get this information.&lt;/p&gt;
&lt;p&gt;Now make sure you pay this bill to avoid the consequences.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;We talked about so many things, maybe some of you are already convinced to move here. Well…&lt;/p&gt;
&lt;p&gt;Finding an apartment can be very hard, especially in big cities. There was recently a &lt;a href=&quot;https://www.thelocal.ch/20181122/hundreds-queue-for-zurich-apartment-viewing-on-freezing-night/&quot;&gt;story in the news&lt;/a&gt; that hundreds of people were waiting to view an apartment in the center of Zurich. But seeing the apartment is only the beginning.&lt;/p&gt;
&lt;p&gt;To rent an apartment, you need to prepare yourself for a fair amount of paperwork. After viewing the apartment, you will receive the official application form, which contains a lot of nice checkboxes like if you have any pets, if you play a musical instrument or if you are a programmer. No, I’m kidding they don’t ask you that, they get this information from the papers you are providing to them. Which papers?&lt;/p&gt;
&lt;p&gt;So, to get an apartment you have to provide them with your CV, a confirmation from the local authority that you don’t owe any money (check the previous section), a motivation letter with how much you want to rent this apartment, and some references. Yes, they call your boss to confirm that you are working, your neighbors to confirm you are not an annoying person, and of course, your current landlord, to check if you follow the rules as you’re supposed to.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It kinda feels like this episode of Black Mirror, where people can rate each other from one to five stars for every interaction they have, which can impact their socioeconomic status.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/9bdff29f5f4b6805cae6080ff1b59b87/2fb56/anokhi-de-silva-bFDLzdLHjHo-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 65.39923954372622%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAUGAwT/xAAVAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAAB4KGZ3B4IRr//xAAbEAACAgMBAAAAAAAAAAAAAAACAwABEhMiMf/aAAgBAQABBQJLCo6YOO5UD0i7n//EABYRAQEBAAAAAAAAAAAAAAAAAAAREv/aAAgBAwEBPwGMP//EABYRAQEBAAAAAAAAAAAAAAAAAAAREv/aAAgBAgEBPwGxp//EABgQAAMBAQAAAAAAAAAAAAAAAAABECEx/9oACAEBAAY/Akbh2Of/xAAaEAEAAgMBAAAAAAAAAAAAAAABABEhQVGR/9oACAEBAAE/IUWjCNSvarkS18jrHkSvkdLuf//aAAwDAQACAAMAAAAQmM//xAAXEQEAAwAAAAAAAAAAAAAAAAAAAREh/9oACAEDAQE/EIsw/8QAFhEBAQEAAAAAAAAAAAAAAAAAABEB/9oACAECAQE/EMot/8QAHBABAAICAwEAAAAAAAAAAAAAAQARITFBUWGR/9oACAEBAAE/EACl4jeJaZJaOIpTZ7Dg0yGBPpPzqZ7CLzP/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Credit: Anokhi De Silva&quot;
        title=&quot;&quot;
        src=&quot;/static/9bdff29f5f4b6805cae6080ff1b59b87/9ecec/anokhi-de-silva-bFDLzdLHjHo-unsplash.jpg&quot;
        srcset=&quot;/static/9bdff29f5f4b6805cae6080ff1b59b87/5d001/anokhi-de-silva-bFDLzdLHjHo-unsplash.jpg 263w,
/static/9bdff29f5f4b6805cae6080ff1b59b87/7349d/anokhi-de-silva-bFDLzdLHjHo-unsplash.jpg 525w,
/static/9bdff29f5f4b6805cae6080ff1b59b87/9ecec/anokhi-de-silva-bFDLzdLHjHo-unsplash.jpg 1050w,
/static/9bdff29f5f4b6805cae6080ff1b59b87/39d31/anokhi-de-silva-bFDLzdLHjHo-unsplash.jpg 1575w,
/static/9bdff29f5f4b6805cae6080ff1b59b87/1f368/anokhi-de-silva-bFDLzdLHjHo-unsplash.jpg 2100w,
/static/9bdff29f5f4b6805cae6080ff1b59b87/2fb56/anokhi-de-silva-bFDLzdLHjHo-unsplash.jpg 3452w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/@anokhi_08&quot;&gt;Anokhi De Silva&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;But you haven’t finished yet. Now you have to give the keys from your previous apartment. Good luck with that. The last day of your rental, you will have a meeting with your landlord together with the next person who will rent the apartment. They will search the place centimeter by centimeter. They will try to find every possible defect or dirt.&lt;/p&gt;
&lt;p&gt;I had to pay a professional cleaning company to clean my previous apartment, and it turned out that they had to visit again to clean the parts that were not properly cleaned. There was an awkward moment where four people were on top of my shower sifone, the pipe that collects all the water. I didn’t dare to ask what they found inside, but there was a big discussion with all the parties involved.&lt;/p&gt;
&lt;p&gt;Now, you may want to buy your own apartment at some point. Why spend your money on rent, you may think.&lt;/p&gt;
&lt;p&gt;Well, my friends, I have bad news for you. The apartments in Switzerland are extremely expensive. You can easily spend a million of Swiss Francs. Many of the people I know have a loan until their retirement. Sometimes even until death.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;You finally made it! You have your own apartment. Furthermore, you may think that it’s all over now. But now it’s where the fun begins. You have to agree with the terms and conditions. My previous apartment had 7 pages full of rules.&lt;/p&gt;
&lt;p&gt;Front and back.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/abf746df9be4fceecaae3923b56f3d9e/df51d/claudio-schwarz-NPOz6vzYdWQ-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAABAABA//EABUBAQEAAAAAAAAAAAAAAAAAAAID/9oADAMBAAIQAxAAAAEGla4iucX/AP/EABkQAAMBAQEAAAAAAAAAAAAAAAABAgMRM//aAAgBAQABBQJYlZrjkTZXmf/EABcRAAMBAAAAAAAAAAAAAAAAAAECECH/2gAIAQMBAT8BY7P/xAAVEQEBAAAAAAAAAAAAAAAAAAABEP/aAAgBAgEBPwEn/8QAFxAAAwEAAAAAAAAAAAAAAAAAARAhIP/aAAgBAQAGPwKzAX//xAAbEAACAgMBAAAAAAAAAAAAAAAAARExECFBcf/aAAgBAQABPyF6cx9nRQydEy7xnZ//2gAMAwEAAgADAAAAEEDf/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAEhUf/aAAgBAwEBPxBiSl0//8QAFhEBAQEAAAAAAAAAAAAAAAAAAQAh/9oACAECAQE/EAI2X//EABoQAQADAQEBAAAAAAAAAAAAAAEAESExQWH/2gAIAQEAAT8QaWlYPsHYqt4RDaI/SIB7azCmZG2n/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Credit: Claudio Schwarz&quot;
        title=&quot;&quot;
        src=&quot;/static/abf746df9be4fceecaae3923b56f3d9e/9ecec/claudio-schwarz-NPOz6vzYdWQ-unsplash.jpg&quot;
        srcset=&quot;/static/abf746df9be4fceecaae3923b56f3d9e/5d001/claudio-schwarz-NPOz6vzYdWQ-unsplash.jpg 263w,
/static/abf746df9be4fceecaae3923b56f3d9e/7349d/claudio-schwarz-NPOz6vzYdWQ-unsplash.jpg 525w,
/static/abf746df9be4fceecaae3923b56f3d9e/9ecec/claudio-schwarz-NPOz6vzYdWQ-unsplash.jpg 1050w,
/static/abf746df9be4fceecaae3923b56f3d9e/39d31/claudio-schwarz-NPOz6vzYdWQ-unsplash.jpg 1575w,
/static/abf746df9be4fceecaae3923b56f3d9e/1f368/claudio-schwarz-NPOz6vzYdWQ-unsplash.jpg 2100w,
/static/abf746df9be4fceecaae3923b56f3d9e/df51d/claudio-schwarz-NPOz6vzYdWQ-unsplash.jpg 2400w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/@purzlbaum&quot;&gt;Claudio Schwarz&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;But what kind of rules, you may ask. Well, most of the time, don’t expect something crazy. I said most of the time.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;In some apartments, it is forbidden to flush your toilet after 10pm.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Yes, you heard it right. The older buildings have very old pipe installations and every time that you visit the WC, the noise of the flush is very prominent to all the tenants. Not the best thing to do in a country that detests any kind of neighbor activity.&lt;/p&gt;
&lt;p&gt;You also have to book your laundry times. You see, most of the apartments in the city center are relatively old, and they can’t host a washing machine. What you get instead is a laundry room somewhere in the basement. Each tenant books the time that is most convenient for them. Sometimes you have to pay a small amount of money every time that you use the washing machine or the tumbler. I told you, nothing comes for free.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/653554de644389111ea90ffc675d123d/0f98f/engin-akyurt-yCYVV8-kQNM-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 69.20152091254754%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAOABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAMEAv/EABUBAQEAAAAAAAAAAAAAAAAAAAIE/9oADAMBAAIQAxAAAAFM9OKDWKDX/8QAGxAAAgIDAQAAAAAAAAAAAAAAAQIAAxAREiL/2gAIAQEAAQUCtYhV7Q7h9QVc4//EABYRAQEBAAAAAAAAAAAAAAAAAAEAMf/aAAgBAwEBPwEyG//EABYRAAMAAAAAAAAAAAAAAAAAAAEQMf/aAAgBAgEBPwE1f//EABsQAAICAwEAAAAAAAAAAAAAAAERABACEiIx/9oACAEBAAY/AufTGcnSmxLr/8QAGhABAAIDAQAAAAAAAAAAAAAAAQARITFBEP/aAAgBAQABPyE4bNRFAL1fgGWzonIKMJolz//aAAwDAQACAAMAAAAQhM//xAAXEQADAQAAAAAAAAAAAAAAAAAAAREh/9oACAEDAQE/ENrBMVP/xAAXEQEBAQEAAAAAAAAAAAAAAAABABEh/9oACAECAQE/EA9Tt//EABoQAQADAQEBAAAAAAAAAAAAAAEAESFBUTH/2gAIAQEAAT8Q+8hD4EQiQOkoGuw8VL7iiUA7Eov1h0lrrP/Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Credit: engin akyurt&quot;
        title=&quot;&quot;
        src=&quot;/static/653554de644389111ea90ffc675d123d/9ecec/engin-akyurt-yCYVV8-kQNM-unsplash.jpg&quot;
        srcset=&quot;/static/653554de644389111ea90ffc675d123d/5d001/engin-akyurt-yCYVV8-kQNM-unsplash.jpg 263w,
/static/653554de644389111ea90ffc675d123d/7349d/engin-akyurt-yCYVV8-kQNM-unsplash.jpg 525w,
/static/653554de644389111ea90ffc675d123d/9ecec/engin-akyurt-yCYVV8-kQNM-unsplash.jpg 1050w,
/static/653554de644389111ea90ffc675d123d/39d31/engin-akyurt-yCYVV8-kQNM-unsplash.jpg 1575w,
/static/653554de644389111ea90ffc675d123d/0f98f/engin-akyurt-yCYVV8-kQNM-unsplash.jpg 1920w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/@enginakyurt&quot;&gt;engin akyurt&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Of course, it doesn’t mean that the machines will be 100% free at the time you bring all your belongings downstairs. Some people simply don’t respect the schedule. Just make sure not to forget your underwear in one of the machines and turn your neighbor’s clothes pink.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Speaking about rules, I have another topic which needs its own section. It’s about recycling.&lt;/p&gt;
&lt;p&gt;When I first registered myself in Zurich, I received the official guide for recycling. In the beginning I thought, wow how they have it well organized. Later on, I started struggling to divide my trash in multiple groups.&lt;/p&gt;
&lt;p&gt;The most difficult is paper and carton. Let’s say you receive a package at home. In order to recycle the carton, you need to remove all the plastic, the paper, any stickers or tape. Sometimes it’s very difficult to understand if something goes to carton or paper. For example, the bag from the supermarket goes to carton collection.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/25e04ff3e5acf1f205480def65a715da/df51d/tran-mau-tri-tam-hmBJtWxFOdg-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBP/EABYBAQEBAAAAAAAAAAAAAAAAAAABA//aAAwDAQACEAMQAAABo2CuZhSP/8QAGhAAAwEAAwAAAAAAAAAAAAAAAQMRAAISE//aAAgBAQABBQLixk9mYtbe0BJh3//EABURAQEAAAAAAAAAAAAAAAAAAAAR/9oACAEDAQE/AYj/xAAXEQEAAwAAAAAAAAAAAAAAAAAAARIT/9oACAECAQE/AbS1f//EABsQAAIBBQAAAAAAAAAAAAAAAAABMRAyQXGB/9oACAEBAAY/ApT4YLqbJP/EAB0QAAMAAQUBAAAAAAAAAAAAAAABEUExUWFxgdH/2gAIAQEAAT8hWp9oVqanc+ARTOMiCq+MCO6j/9oADAMBAAIAAwAAABAUz//EABURAQEAAAAAAAAAAAAAAAAAABAR/9oACAEDAQE/EIH/xAAWEQEBAQAAAAAAAAAAAAAAAAAAETH/2gAIAQIBAT8QlqX/xAAdEAEAAgICAwAAAAAAAAAAAAABABEhMUFRcaGx/9oACAEBAAE/EL3irdvpmOYdKPsasA4oqbRaqttjpeQjgjq9zhTwE//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Credit: Tran Mau Tri Tam&quot;
        title=&quot;&quot;
        src=&quot;/static/25e04ff3e5acf1f205480def65a715da/9ecec/tran-mau-tri-tam-hmBJtWxFOdg-unsplash.jpg&quot;
        srcset=&quot;/static/25e04ff3e5acf1f205480def65a715da/5d001/tran-mau-tri-tam-hmBJtWxFOdg-unsplash.jpg 263w,
/static/25e04ff3e5acf1f205480def65a715da/7349d/tran-mau-tri-tam-hmBJtWxFOdg-unsplash.jpg 525w,
/static/25e04ff3e5acf1f205480def65a715da/9ecec/tran-mau-tri-tam-hmBJtWxFOdg-unsplash.jpg 1050w,
/static/25e04ff3e5acf1f205480def65a715da/39d31/tran-mau-tri-tam-hmBJtWxFOdg-unsplash.jpg 1575w,
/static/25e04ff3e5acf1f205480def65a715da/1f368/tran-mau-tri-tam-hmBJtWxFOdg-unsplash.jpg 2100w,
/static/25e04ff3e5acf1f205480def65a715da/df51d/tran-mau-tri-tam-hmBJtWxFOdg-unsplash.jpg 2400w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/@tranmautritam&quot;&gt;Tran Mau Tri Tam&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;And don’t even think about cheating, the garbage police will get you. They have people who open the trash bags, searching for any clue to identify your identity. Maybe you forget the receipt in the package. Silly you. In that case, you will receive the trash bag outside your door, with a friendly post-it on it.&lt;/p&gt;
&lt;p&gt;“Please don’t do this again, we know where you live”.&lt;/p&gt;
&lt;p&gt;Glass is also challenging to recycle. You need to remove the cover, of course, because it’s plastic. There are different bins depending on the color of the glass. Ah, and never recycle glass on Sundays. The noise will make everyone crazy. People will be very mean to you. They will start yelling from their balconies. Don’t tell me I didn’t warn you.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;We reached the end of our journey, my friends. This will be most probably the last part of this special series. I think I covered most of the points I wanted to share with you. Now that I’m thinking, I have one post for every year I’m living here.&lt;/p&gt;
&lt;p&gt;A special thanks to my colleague &lt;a href=&quot;https://fcvbz.ch/fc/?lang=en&quot;&gt;Laszlo Markert&lt;/a&gt; and his wife. They contributed a lot to collect these facts.&lt;/p&gt;
&lt;p&gt;Until the next post, take care and wear your mask! 😷&lt;/p&gt;
&lt;p&gt;The beautiful cover is taken somewhere in Montana, by &lt;a href=&quot;https://unsplash.com/photos/frkc9sO47Ag&quot;&gt;Artiom Vallat&lt;/a&gt;.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Fun Facts About Switzerland Part III]]></title><description><![CDATA[What is forbidden to ask in a Swiss restaurant? Where all the people go on Sundays? Where do all the cows go in summer? What is a Hüsli? Our…]]></description><link>http://www.nicotsou.com/fun-facts-about-switzerland-part-iii/</link><guid isPermaLink="false">http://www.nicotsou.com/fun-facts-about-switzerland-part-iii/</guid><pubDate>Wed, 17 Nov 2021 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;What is forbidden to ask in a Swiss restaurant? Where all the people go on Sundays? Where do all the cows go in summer? What is a Hüsli?&lt;/p&gt;
&lt;p&gt;Our journey continues with another post about Switzerland. This time we will talk about life in the city, traveling and languages.&lt;/p&gt;
&lt;p&gt;Bratwurst? 🌭&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;The most important question I get from my friends from Greece is related with nightlife. You see, Thessaloniki, my hometown, has a very vivid nightlife. People like to go out several times per week. We even receive jokes about the coffee habit that we have from other cities of Greece.&lt;/p&gt;
&lt;p&gt;Well, I have to say, Zurich has a pretty balanced nightlife. You can potentially find a wide list of options for entertainment, but it may be a little bit limited. Don’t expect the variety of Berlin or London, for example.&lt;/p&gt;
&lt;p&gt;What was unusual for me here is the fact that people are drinking on their way to the bar. You will see a lot of teenagers carrying beers on the street or sometimes even in the busses. No, they are not alcoholics, they are just trying to reduce their costs. Drinks can be an expensive hobby here, especially if you are a student.&lt;/p&gt;
&lt;p&gt;Public drinking is awesome in the summer. You don’t need to dress perfectly to go out, you don’t need to wait for hours in the lines, or to impress the doorman guy. You call your friends, you grab a couple of beers, snacks, sometimes a fully charged bluetooth speaker, which at the end it only works for 15 minutes. There are plenty of places to go, usually people prefer to stay by the lake. The vibe is really relaxed. Sometimes you may be lucky to find a spot next to a band, and you can enjoy exquisite music. Sometimes the music can be really poor.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/0483c9c4fc60f350827ee4432a11ab56/df51d/wyron-a-PnFAa1ro9b0-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 69.20152091254754%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAOABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAMEAgX/xAAWAQEBAQAAAAAAAAAAAAAAAAACAAH/2gAMAwEAAhADEAAAAY2K1tQc8K//xAAZEAADAQEBAAAAAAAAAAAAAAABAgMAERL/2gAIAQEAAQUC91yvfGlhva9FFBZut//EABURAQEAAAAAAAAAAAAAAAAAABAR/9oACAEDAQE/AYf/xAAWEQEBAQAAAAAAAAAAAAAAAAAAERL/2gAIAQIBAT8B0r//xAAbEAABBAMAAAAAAAAAAAAAAAAAAQIyoREhIv/aAAgBAQAGPwKSmnnTkIWQsyf/xAAcEAACAgIDAAAAAAAAAAAAAAAAARExIXGBkbH/2gAIAQEAAT8hVa7hy58yYdB6MV+gtNQ0xNUk/9oADAMBAAIAAwAAABDH7//EABYRAQEBAAAAAAAAAAAAAAAAAAABEf/aAAgBAwEBPxCGx//EABURAQEAAAAAAAAAAAAAAAAAAAAB/9oACAECAQE/EC0//8QAHBAAAgIDAQEAAAAAAAAAAAAAAREAITFBkXHB/9oACAEBAAE/EEdFihiC4tH7Q6CeGApqYSe9gSlcBRQrvnIQ02UATU//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Credit: Wyron A&quot;
        title=&quot;&quot;
        src=&quot;/static/0483c9c4fc60f350827ee4432a11ab56/9ecec/wyron-a-PnFAa1ro9b0-unsplash.jpg&quot;
        srcset=&quot;/static/0483c9c4fc60f350827ee4432a11ab56/5d001/wyron-a-PnFAa1ro9b0-unsplash.jpg 263w,
/static/0483c9c4fc60f350827ee4432a11ab56/7349d/wyron-a-PnFAa1ro9b0-unsplash.jpg 525w,
/static/0483c9c4fc60f350827ee4432a11ab56/9ecec/wyron-a-PnFAa1ro9b0-unsplash.jpg 1050w,
/static/0483c9c4fc60f350827ee4432a11ab56/39d31/wyron-a-PnFAa1ro9b0-unsplash.jpg 1575w,
/static/0483c9c4fc60f350827ee4432a11ab56/1f368/wyron-a-PnFAa1ro9b0-unsplash.jpg 2100w,
/static/0483c9c4fc60f350827ee4432a11ab56/df51d/wyron-a-PnFAa1ro9b0-unsplash.jpg 2400w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/photos/PnFAa1ro9b0&quot;&gt;Wyron A&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The story can be a little bit different for other cities. I have the sense that most of the Swiss cities are very silent at night, except of Zurich. The most disappointing for me was Geneva. Don’t be surprised if you find no people on the streets on a Saturday summer night.&lt;/p&gt;
&lt;p&gt;Zurich is known as the venue for the brightest Swiss festivals, events, and celebrations. Sometimes the whole city is being transformed into a trance club. You may enjoy popular bands and DJs. And it goes really wild. But the impressive thing is, the next morning, everything goes back to normal. No more confetti, empty cups, bottles, DNA, or any other waste. It’s like nothing ever happened.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;If you ever visit a Swiss city, you will most probably notice the water fountains. And I’m not talking about these large fountains that are used for decoration purposes, I’m talking about drinkable water flows. For example, in Zurich you will find 1,200 fountains.&lt;/p&gt;
&lt;p&gt;In your face Fontana di Trevi.&lt;/p&gt;
&lt;p&gt;A lot of people are carrying a small water bottle and they fill it up with fresh water.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/ef05210f1f4a402d8052037818ce08f4/df51d/dorothea-oldani-c01oPaDbxWw-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIEAQP/xAAWAQEBAQAAAAAAAAAAAAAAAAAAAQL/2gAMAwEAAhADEAAAAXfe0VEpl//EABkQAQADAQEAAAAAAAAAAAAAAAEAAhIRA//aAAgBAQABBQJqLX0vvXYUNZnUn//EABYRAQEBAAAAAAAAAAAAAAAAAAASAf/aAAgBAwEBPwG1Y//EABgRAAIDAAAAAAAAAAAAAAAAAAABAhNR/9oACAECAQE/AXDCpn//xAAaEAACAgMAAAAAAAAAAAAAAAAAAQIRECFB/9oACAEBAAY/AtEU2dLLvH//xAAbEAACAwEBAQAAAAAAAAAAAAABEQAhMUFRYf/aAAgBAQABPyHQKVHkLwp55AhgJDge0rmxw+QVHP/aAAwDAQACAAMAAAAQUx//xAAXEQEAAwAAAAAAAAAAAAAAAAABEBEh/9oACAEDAQE/EAVsX//EABgRAAIDAAAAAAAAAAAAAAAAAAABESFR/9oACAECAQE/EHzQ1P/EABwQAQEBAAIDAQAAAAAAAAAAAAERACExQYHB4f/aAAgBAQABPxCtVMk4J2ZE4gIT8vzGFt1rJTTsJ4zwkIhwwc8T1v/Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Credit: Dorothea OLDANI&quot;
        title=&quot;&quot;
        src=&quot;/static/ef05210f1f4a402d8052037818ce08f4/9ecec/dorothea-oldani-c01oPaDbxWw-unsplash.jpg&quot;
        srcset=&quot;/static/ef05210f1f4a402d8052037818ce08f4/5d001/dorothea-oldani-c01oPaDbxWw-unsplash.jpg 263w,
/static/ef05210f1f4a402d8052037818ce08f4/7349d/dorothea-oldani-c01oPaDbxWw-unsplash.jpg 525w,
/static/ef05210f1f4a402d8052037818ce08f4/9ecec/dorothea-oldani-c01oPaDbxWw-unsplash.jpg 1050w,
/static/ef05210f1f4a402d8052037818ce08f4/39d31/dorothea-oldani-c01oPaDbxWw-unsplash.jpg 1575w,
/static/ef05210f1f4a402d8052037818ce08f4/1f368/dorothea-oldani-c01oPaDbxWw-unsplash.jpg 2100w,
/static/ef05210f1f4a402d8052037818ce08f4/df51d/dorothea-oldani-c01oPaDbxWw-unsplash.jpg 2400w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/photos/c01oPaDbxWw&quot;&gt;Dorothea OLDANI&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;What is really weird is if you ever ask for tap water in a restaurant. The waiters will look at you like you are doing a crime or something. It’s kinda ironic if you think that the water quality of Switzerland is one of the best you can find on earth.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;What also impresses me is the fact that people travel a lot. And I mean a lot! Sometimes it’s hard to meet friends because they may be in different mountains. The author is not excluded. Alongside Switzerland is in the center of Europe. For starters, Switzerland is next to Germany, Italy, France and Austria. The Swiss salary is also another factor that makes people less concerned about the hospitality and transportation costs.&lt;/p&gt;
&lt;p&gt;Having a stressed week? No problem. Just book a Chalet on a mountain for the weekend and enjoy the iced view while you are relaxing in your warm Jacuzzi. What? It is summer you said? Kein Problem. Get a plane for Malta and in 2 hours you can get loose at the beach. It only costs hundred Swiss francs. That’s what you will pay if you go out Saturday night anyway.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;People in Switzerland like to plan in advance. Don’t be surprised if you don’t find any friend to join you for a coffee this weekend. Most probably they have planned to go to a mountain, ride the whole city with a bicycle, visit their parents in Saint Gallen, or even being in another country for a long weekend trip.&lt;/p&gt;
&lt;p&gt;I have observed that people are more active here. They will most likely prefer to spend 1-2 days in their free time on the mountains, doing sports or enjoying the nature. I also changed my habits for the better.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/65c05aa953952e7939670e7b3f9fc9b2/212bf/C441A2E0-E06A-484E-8952-322CE647CE5C_1_105_c.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 133.46007604562737%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAbABQDASIAAhEBAxEB/8QAGQABAQADAQAAAAAAAAAAAAAABAABAgUD/8QAFgEBAQEAAAAAAAAAAAAAAAAAAQIA/9oADAMBAAIQAxAAAAHfHiS57kKnBN3wiyVD/8QAHRAAAgEEAwAAAAAAAAAAAAAAAQIAAxESIRMUMv/aAAgBAQABBQKwnJoNrJZmpaxMwJRQTUFErG9KB2p//8QAFREBAQAAAAAAAAAAAAAAAAAAASD/2gAIAQMBAT8BCP/EABURAQEAAAAAAAAAAAAAAAAAAAEg/9oACAECAQE/AVj/xAAeEAABBAEFAAAAAAAAAAAAAAABAAIQESESIDFBUf/aAAgBAQAGPwLBgZCsmk1ouvYOrnoRQfs//8QAHhAAAgMAAQUAAAAAAAAAAAAAAREAITFBEFFxgbH/2gAIAQEAAT8hTjUqjs4oAqw5hhjg6MuYBAQGO8ZS+PzBFi3cBUfUEEqEpFW9P//aAAwDAQACAAMAAAAQUzHC/8QAFhEBAQEAAAAAAAAAAAAAAAAAABEQ/9oACAEDAQE/EDMj/8QAFxEAAwEAAAAAAAAAAAAAAAAAAAERQf/aAAgBAgEBPxCpRNYVn//EABwQAQEAAwEBAQEAAAAAAAAAAAERACFBMVFhsf/aAAgBAQABPxBa17a6zBXaJWkcYAXY0jjTieWAXWsqkodq6T5l60Ei3JCjLDSpteeUwph4ghcmSTg5/cU1N/MeWbiP2Y8z/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;C441A2E0-E06A-484E-8952-322CE647CE5C_1_105_c.jpeg&quot;
        title=&quot;&quot;
        src=&quot;/static/65c05aa953952e7939670e7b3f9fc9b2/212bf/C441A2E0-E06A-484E-8952-322CE647CE5C_1_105_c.jpg&quot;
        srcset=&quot;/static/65c05aa953952e7939670e7b3f9fc9b2/5d001/C441A2E0-E06A-484E-8952-322CE647CE5C_1_105_c.jpg 263w,
/static/65c05aa953952e7939670e7b3f9fc9b2/7349d/C441A2E0-E06A-484E-8952-322CE647CE5C_1_105_c.jpg 525w,
/static/65c05aa953952e7939670e7b3f9fc9b2/212bf/C441A2E0-E06A-484E-8952-322CE647CE5C_1_105_c.jpg 768w&quot;
        sizes=&quot;(max-width: 768px) 100vw, 768px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;That’s why you may not find a lot of people in the city on Sundays. They are most likely somewhere away or they enjoy their family time. Restaurants and coffee bars are also likely to be closed on Sunday.&lt;/p&gt;
&lt;p&gt;This means you should definitely reserve your table in advance. I have a friend who tried to book a table for brunch and they told him that the next available date is in one month.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Even cows in Switzerland go for holidays during the summer months.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I’m not kidding. According to the &lt;a href=&quot;https://www.agrarbericht.ch/de/betrieb/strukturen/soemmerungsbetriebe?_sm_au_=iVVFrz6tDqV3040H&quot;&gt;Federal Office for Agriculture&lt;/a&gt;, around 270,000 cows are marched from their valley farms to mountain meadows at the start of every summer, just to come back down again in early autumn. Apparently this &lt;a href=&quot;https://www.swissinfo.ch/eng/milk-run_why-swiss-cows-climb-mountains/44380436&quot;&gt;increases the quality of the milk&lt;/a&gt;. Which is an extremely important ingredient for the famous Swiss cheese.&lt;/p&gt;
&lt;p&gt;So don’t get bored, do it like a cow!&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I have observed that many of my Swiss friends, travelled from 6 months up to a year somewhere in the world, right after their university studies.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Besides the fact that it’s costly, and it’s not easy to manage if your budget doesn’t allow it, I find it one of the nicest things to do in your life. You have the opportunity to discover the world and earn new experiences. Plus, you are more relaxed and prepared to ride your career train. Of course, as everything in this life, it depends on your wealth and life situation.&lt;/p&gt;
&lt;p&gt;But don’t let the money discourage you. It’s relatively easy to make some savings here and give a long break to yourself and your family. A lot of people take this opportunity to learn, for example Italian, so they leave for 1-3 months in Italy and they are working from there, practicing their language skills with native speakers. Companies will also welcome your decision, if the language is important to their business.&lt;/p&gt;
&lt;p&gt;Now, you may ask, how you can manage to leave everything behind for such a long period of time. The best way is by subletting your apartment. You will help other people, and more precisely students, to find a decent and affordable place to stay. And the amount of money you will save is usually enough to pay for your accommodation.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Now I would like to open another big topic, which is the language.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Switzerland has 4 official languages; German, French, Italian and Romansh. It is common to find people that speak or at least being able to understand most of the languages.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In Zurich people speak German (Deutsch). To be able to communicate with them it requires a lot of effort. You start by taking German classes. After some months, you feel empowered with your achievements. You can perfectly communicate with your teacher. Now it is time to practice your skills with locals, you think. What can possibly go wrong, right? Well, my friend, I’m afraid it’s not gonna work.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1024px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/3a6474318fb4d4f1848eb9491d7968e2/72e01/luzern-by-night.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.90494296577947%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAEDAgX/xAAVAQEBAAAAAAAAAAAAAAAAAAAAAv/aAAwDAQACEAMQAAAB5SkqmxgP/8QAGRAAAgMBAAAAAAAAAAAAAAAAAAECERIT/9oACAEBAAEFAssoo6SFNmkf/8QAFhEBAQEAAAAAAAAAAAAAAAAAABIB/9oACAEDAQE/AZxL/8QAFhEAAwAAAAAAAAAAAAAAAAAAAAES/9oACAECAQE/AaZbP//EABcQAAMBAAAAAAAAAAAAAAAAAAAQMUH/2gAIAQEABj8Cjq0//8QAHRAAAgEEAwAAAAAAAAAAAAAAAREAECExUUGB8P/aAAgBAQABPyEOTYdHqEAOigPNtTdH/9oADAMBAAIAAwAAABAvL//EABgRAAIDAAAAAAAAAAAAAAAAAAARASFR/9oACAEDAQE/EJZQun//xAAYEQACAwAAAAAAAAAAAAAAAAAAEQEhYf/aAAgBAgEBPxCF2jM//8QAHRABAAMAAgMBAAAAAAAAAAAAAQARITFBYXGBsf/aAAgBAQABPxBfJW+JvA3yMsZSpipU4Zc/UmDd7JsYNbSvRT+z/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Luzern is one of the most beautiful Swiss cities.&quot;
        title=&quot;&quot;
        src=&quot;/static/3a6474318fb4d4f1848eb9491d7968e2/72e01/luzern-by-night.jpg&quot;
        srcset=&quot;/static/3a6474318fb4d4f1848eb9491d7968e2/5d001/luzern-by-night.jpg 263w,
/static/3a6474318fb4d4f1848eb9491d7968e2/7349d/luzern-by-night.jpg 525w,
/static/3a6474318fb4d4f1848eb9491d7968e2/72e01/luzern-by-night.jpg 1024w&quot;
        sizes=&quot;(max-width: 1024px) 100vw, 1024px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Luzern is one of the most beautiful Swiss cities.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In Switzerland we speak Swiss German (Schwiizertüütsch). Don’t be surprised if you don’t understand a thing from what you hear in the streets, even if you speak German fluently. You may want to spend some time to learn the dialect. But that’s also not enough.&lt;/p&gt;
&lt;p&gt;And to make it even more difficult, the cantons of the German part have their own dialects. People from Zurich speak differently from people from Bern. Some cities have a lighter version, others a forceful one.&lt;/p&gt;
&lt;p&gt;What I was struggling the most here in Zurich is to learn how to greet people. You always need to check for the other person’s age. There are different phrases for people younger than you, and different for older people. Also there are formal and informal phrases. You can’t just say Grüezi to a kid that passes by, it will be very formal.&lt;/p&gt;
&lt;p&gt;Of course, the more Swiss German you speak, the more people will like you, no matter which phrases you are using. Good luck when you go back to German-speaking countries. You now have the habit to speak with your Swiss dialect. I have a friend who received complaints because he wasn’t speaking German in a German train somewhere in Germany. Of course, the old lady who complained didn’t know this is a German dialect.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/c52e68731d7ba006111f9f90a851e7d3/212bf/swiss-mountain-house.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 133.46007604562737%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAbABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAEEAwX/xAAXAQADAQAAAAAAAAAAAAAAAAAAAwQF/9oADAMBAAIQAxAAAAHDeGpimUDpuerCDRmKAP/EAB0QAAICAQUAAAAAAAAAAAAAAAABAhEDBBIgMTL/2gAIAQEAAQUC8vtUb7Mc0ULTNE8cipcP/8QAGhEAAQUBAAAAAAAAAAAAAAAAAAECAxAREv/aAAgBAwEBPwGRuoc1h//EABgRAQADAQAAAAAAAAAAAAAAAAACERIB/9oACAECAQE/AYSppfWn/8QAHxAAAAUEAwAAAAAAAAAAAAAAAAEQERICICExMkFR/9oACAEBAAY/ApUh1iePE5ENOO7P/8QAHRABAAICAgMAAAAAAAAAAAAAAQAREDEhQVFxsf/aAAgBAQABPyFRGtiADTil1Pcm2KQ/eUaOXvxKAKQa7nWGf//aAAwDAQACAAMAAAAQcNIC/8QAFxEAAwEAAAAAAAAAAAAAAAAAABARIf/aAAgBAwEBPxDeiwg//8QAFxEBAQEBAAAAAAAAAAAAAAAAEQABEP/aAAgBAgEBPxBhseSv/8QAHhABAAICAgMBAAAAAAAAAAAAAQARITFhcRBRofD/2gAIAQEAAT8QBIrkdJWpnRCx8KoHKU61+YsMp4Tx3Gu0O2JxAbpMvk3t7WxAunKslD1bmW2bgtRI7n//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Swiss house under the mountain&quot;
        title=&quot;&quot;
        src=&quot;/static/c52e68731d7ba006111f9f90a851e7d3/212bf/swiss-mountain-house.jpg&quot;
        srcset=&quot;/static/c52e68731d7ba006111f9f90a851e7d3/5d001/swiss-mountain-house.jpg 263w,
/static/c52e68731d7ba006111f9f90a851e7d3/7349d/swiss-mountain-house.jpg 525w,
/static/c52e68731d7ba006111f9f90a851e7d3/212bf/swiss-mountain-house.jpg 768w&quot;
        sizes=&quot;(max-width: 768px) 100vw, 768px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The only Swiss word I used to know before I moved here was Müesli.&lt;/p&gt;
&lt;p&gt;Yes, you most probably know it as well, it’s the healthy yogurt companion. What I didn’t know is that Swiss German is characterized by the frequent use of the diminutive. This means that nouns are usually “reduced in size” by adding the suffix “li” to them.&lt;/p&gt;
&lt;p&gt;For example, if you want to say little man (Man in German) you say Männli, for little mountain (Berg) you say Bergli, and for little house (Haus) you say Hüsli.&lt;/p&gt;
&lt;p&gt;If you ever want to make fun of a Swiss friend, just put -li at the end of every word. Who knows, maybe you will catch some.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Once again, thank you for reading this post. I am saving the fourth part for next time, which will probably be the last. After that, I will continue with my boring programming topics. I forgot how to write technical articles after all these posts.&lt;/p&gt;
&lt;p&gt;The beautiful cover is taken at the lake Klöntalersee, by &lt;a href=&quot;https://unsplash.com/photos/pxjN_6Z_3x8&quot;&gt;Dave Ruck&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Fun Facts About Switzerland Part II]]></title><description><![CDATA[What do we really mean in Switzerland when we talk about hot dogs? Which are the most indistinguishable Swiss meals? How to eat a salad like…]]></description><link>http://www.nicotsou.com/fun-facts-about-switzerland-part-ii/</link><guid isPermaLink="false">http://www.nicotsou.com/fun-facts-about-switzerland-part-ii/</guid><pubDate>Wed, 03 Nov 2021 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;What do we really mean in Switzerland when we talk about hot dogs? Which are the most indistinguishable Swiss meals? How to eat a salad like the Swiss? And what do you need to know in case they invite you for lunch?&lt;/p&gt;
&lt;p&gt;Yes, you guessed it right! This will be a food special.&lt;/p&gt;
&lt;p&gt;In case you missed &lt;a href=&quot;/fun-facts-about-switzerland-part-i/&quot;&gt;my previous post&lt;/a&gt;, this is the second part with fun facts about Switzerland. It’s pretty biased, not sophisticated whatsoever, and I will do my best to not include any stereotypes.&lt;/p&gt;
&lt;p&gt;Cookie? 🍪&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Grüezi wohl! It’s finally time for lunch! The best way to socialize with your friends and colleagues. Here I have some tips for you if you ever visit Switzerland. Let’s start with the basics.&lt;/p&gt;
&lt;p&gt;The most important phrase to remember is “En guete”, which literally means “Have a good one”. It is similar to “Bon Appétit”. You will hear people here use it a lot. They actually expect to hear it from you before they even eat.&lt;/p&gt;
&lt;p&gt;Don’t worry if you forget it, somebody will remind you to do so. It’s one of these moments that you feel like a kid, and your parents are teaching you how to behave.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/9471d0d1aca11bc90234e07f44a43ec3/212bf/cheesecake.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 77.18631178707224%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIEBf/EABQBAQAAAAAAAAAAAAAAAAAAAAL/2gAMAwEAAhADEAAAAbFzWY1iUR//xAAbEAACAgMBAAAAAAAAAAAAAAABAgADBBIUIv/aAAgBAQABBQINoq2eZ0WCDIEW13H/xAAWEQADAAAAAAAAAAAAAAAAAAAAARL/2gAIAQMBAT8BpFo//8QAFhEBAQEAAAAAAAAAAAAAAAAAABIB/9oACAECAQE/AY1D/8QAHhAAAgIABwAAAAAAAAAAAAAAAAECERASISIxM6H/2gAIAQEABj8C1Ru5wqaUjr9LypH/xAAaEAADAQADAAAAAAAAAAAAAAAAARGBITFR/9oACAEBAAE/IYyo6cHaFH0OYxBprAnCHh//2gAMAwEAAgADAAAAEDTf/8QAFhEBAQEAAAAAAAAAAAAAAAAAAREQ/9oACAEDAQE/ECUcP//EABYRAQEBAAAAAAAAAAAAAAAAABEAAf/aAAgBAgEBPxDVclf/xAAdEAEAAgMAAwEAAAAAAAAAAAABABEhMUGBkaHR/9oACAEBAAE/EGveSujMdo442jnkscPMS4va+xNxuBWFBH5Hd6UB/Z//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;cheesecake.jpeg&quot;
        title=&quot;&quot;
        src=&quot;/static/9471d0d1aca11bc90234e07f44a43ec3/212bf/cheesecake.jpg&quot;
        srcset=&quot;/static/9471d0d1aca11bc90234e07f44a43ec3/5d001/cheesecake.jpg 263w,
/static/9471d0d1aca11bc90234e07f44a43ec3/7349d/cheesecake.jpg 525w,
/static/9471d0d1aca11bc90234e07f44a43ec3/212bf/cheesecake.jpg 768w&quot;
        sizes=&quot;(max-width: 768px) 100vw, 768px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The German version of the cheesecake is one of my favorite desserts. You can find plenty of them in multiple variations.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Time to raise your glass? Yeah, I know you will probably try to cheer anyone on the table, but this is not how it works here.&lt;/p&gt;
&lt;p&gt;You should greet every person on the table individually, by looking at their eyes. Maintaining a visual with the suspect is really important. Sometimes, when I forget, people are staring at me with a question on their faces. “Why he doesn’t even look at me? What is his problem?“.&lt;/p&gt;
&lt;p&gt;Time to go home? I know it’s a bit late. Get your coat ready and prepare for a warm goodbye gesture.&lt;/p&gt;
&lt;p&gt;In Switzerland, it is common to give 3 kisses and a hug when you say goodbye. There is also a strict specification on which side to go first, to avoid the awkward moments. Actually no, that’s sometimes unavoidable. Thankfully, this is only between friends, so you will not have to kiss your boss.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/27ae6edcf56493e5e99145d133085860/e5715/cappuccino-viennois.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 133.46007604562737%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAbCAYAAAB836/YAAAACXBIWXMAAAsTAAALEwEAmpwYAAAHZ0lEQVR42h2Vd1RUZxrGr5qNZVfU1RiNu7qabOLKHjceS1TABoiA9D60YYABhqEMwzD03kGaFKWjQFCRLihVZVFXjRAroiKauDG6a0u2aDb+9sZ7zj33++d73qe873uFxJphavsnaL3wiJKOm5R2jVM3MEnj8AMyj46hLB4hqX6U3OM36Lv+lPFnbzgz8YzSU3eJPHSN7NbbXH/8PxraTjF3/hIEU3UjkuR20hovEVg4hF/+IBlHxkhqHCWw5Bw+eWepHZzixpM3tF76O/ENV/EqOEd80y0mn73ml+f+/XusWfNnZsz8DYJFdAv28e3siWjBUNOKkbYDh9QB4g6NkV9/itFvfmRobIKkql6khZeR5IygOHCFwyPf8fBHeP7yBXoGuxCmz2beoo8QXNL7SW8aJb/tBoVdExR0TJDQcI3Dpx+QkJXHlftPaejsJTynltaxl5y795yinikODDxi5N4POLhKef/XC1iy/BMWLl2BoKn+Ck3NKIfOPKTv2lPOjv+Tv02+pHl4Ak+5nIErd9h3sAZ18j7OP/yJE9eeMXz3BX+d/BeyhEoW/u5jZs9dyLRp7yMI7yH0Xf2O9kvf0Hz+vgj2PYPXHjE8/oTO87dwdLSi58JVskoqScpIZ+TBa7K6pugY+557L15TIrLM6xynoGmAnP0Hcff2Q+i+8i0dF+9TPXBbTHecY+enGBalDN18jNTbk5pjrUQnp5CSm8fXj//DsdEXpJ58xuDUGyZf/czjn97y5O1b/i2G8/zVK4SiyjoyCwspazxOw5kbVLQNUtrYRnF9KyEqJU5ONri7WlN/5BBXn/5M762nHBm4yJmxm9z5x0tOfzVGYXkldY31dJ8eRsiMDSAhwpvKBDk5yREEhwWhDFGiioogLFpLZFws/j6W7M8L5uK3P5BeUIBM5kpUTARJ6Wn4BQWLXvsTl5pEYnoygv3eXbi52OAjleDnJSEgOBDfwEDi0lIJiYokIT2VPVY7cZPo01xfgDpMidTHi6i4SPJLS8gpKaG66QjRKel89PuPRUA3GT7+gShCwvD2kaNRyvD298fL2weFqyOJUeEo3SSEhASQnRaNxF2CncQFb7FofFwUmgg1ts4SZs+ZJ6Y8HcFLoUAeEowiQktyQQWpsRrkwQqCRCZBvnIUqjTiU5JxMzMnTizouOZzVLvNUKo1bN9sgNTNixUrP3kHNuO9WQiK+DhUqYkExScQGpdOfVEWzeX51CSJ/tlbYahnhSY5G4WnH5JtuwmwlOC6WQ+nnTtZr7seub0zixeJMyz24bTpv0IoO1xHXsVBZEFhrNloQFh4NFEKJWqJJwGmFnjo7eDzVZ+xZbUuBqs/xdbCBmMzazZ9oYeDkyeOds6s+qPuO4az58xF0N9mgOX27Xha2OFl5YSnuS2uxpaEOrhSEBROseih3ZZNbF6+FBPdVez8dDkWhoYYG1ug9gsWU45gk4Exc3UWoKMzH2GHsQn21vZYmlgg1dfHeZsh5oamWJvaoFVp6etsJzHIH6M/rcJhhwEW69fiZGqGibkNLk4e2DlJsXBwx83RA6vtJgiunjJ2W9hiau3AXgdH3Hx8cPeQ4eQsfqUyyitriQ4TWep9gY2BPjZbt2JjvAcHiTtWNo4YmViht8OYdes28BfRU0EZqsLGxRMvPwVSmYyIyAg8xJYxNrPBSx6An4eU6OBgfB0dsNy8GbONGwlx9aelppMj5a3UFB6nvqqdlPg0VPJwhFCtFpmT2NTiYKtDVOwxMsVNDCMmUEumLJQUO1diVWok5uZY6eljtG4dJQn53LlwlzNdYwx136bpQAfhouz9MdkIMc4yIk1tsd1liu8Oc3JlKrqSi7koVu/NraU+u4yC3ELkLhKsdu3C2cqWts6zDPZ+TW/TCG3728jziUEu2ra/oAqhUPQgSTQ4Q+LPyfhyRko6GNh3jJOl7bSfvk7n5UmauoeIFWfcdOt6PGW+ZJY2UNY8QGfPJb5MKCXJyJHAP6wmzcIFQess5aBUS3d0Nd3xdRzXlHE0uIiWvKP0T4gLd+q/nLh8l9KKcjxcbDHbu5eEgsN82T9KRV0L3dVtFHqo8Bc3dpajuA8z5FF0h5XSoS6hL66cE9piOjTFNITlEuUeQFZWMWW1x8krqSJIGYKtpTWuEimmBkY47nWlvqKH0pCMdwzznAMResXL/VHF9GqLOB1fRm9kAT2RRVRLNdjPms9G4X3WztZh7cIP0f1gKSvFaVg8fQYfCAJbFy2jIquB+uQqMkycSBelCyfVeQzFFNKnzmVAmy8C53EiPJdjyhSS1m5BqrMY81kL0Jsxh/XCTNZNm8UG8aw3bSZ+m4ypTa6hJbuRttRKil3U4j9FnU2/JotToRkMaHLo1+bQHZ7JqdhC2sPTyNE3IXTJSuS/XYaHzhKc5yxCIp4jd9qIUrOpjiilVlR5WFtChV86wkBkDr3qdE6GpNEjvl1BKSJghgiYS39qMc2qROJ0N6BatorAD1fgO28J6s82cEiTS71oU6Uyk6rgXCoUmZRKk/g/k+4p5wS5VC0AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Untitled&quot;
        title=&quot;&quot;
        src=&quot;/static/27ae6edcf56493e5e99145d133085860/e5715/cappuccino-viennois.png&quot;
        srcset=&quot;/static/27ae6edcf56493e5e99145d133085860/17741/cappuccino-viennois.png 263w,
/static/27ae6edcf56493e5e99145d133085860/52211/cappuccino-viennois.png 525w,
/static/27ae6edcf56493e5e99145d133085860/e5715/cappuccino-viennois.png 768w&quot;
        sizes=&quot;(max-width: 768px) 100vw, 768px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;What about the food Nick?&lt;/p&gt;
&lt;p&gt;Well, since I’m coming from a Mediterranean country, I usually find the food in Switzerland kinda meh. I don’t have a problem with the quality, as much as I have with the quantity. The truth is, there are plenty of nice dishes to try.&lt;/p&gt;
&lt;p&gt;One of the most traditional Swiss delights is &lt;a href=&quot;https://en.wikipedia.org/wiki/Fondue&quot;&gt;Fondue&lt;/a&gt; and &lt;a href=&quot;https://en.wikipedia.org/wiki/Raclette&quot;&gt;Raclette&lt;/a&gt;. They are both really tasty. It’s one of the best activities you can do with friends. I mean, who needs to ski anyway…&lt;/p&gt;
&lt;p&gt;But be careful. They do smell a lot. I haven’t been on sports that much in my life, but I can tell you that my socks after a long workout don’t even compare to that smell. We always have to come up with a practical solution in order to cover the smell when we’re inviting friends home to enjoy some cheese.&lt;/p&gt;
&lt;p&gt;My least favorite Swiss dessert is called “Gipfeli”. It looks a lot like a croissant. But it’s not! I think the people who brought the first croissant from France forgot to put butter, sugar and make it crispy. It’s a terrible disappointment when you are craving something sweet.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/de725eb3ebbad8ef67949cce6bef30e5/e5715/sausages.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 100.38022813688212%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEqklEQVR42g3UWVMTBwAA4J3xpfWpxem0jjMUtIoaoWAD5VASSDYXOTbJZpfcxya7m4Nkc7K5NzcJhIQccqhYOYURsMioY7W1jqMzWqftS5/60Jn+kfb7ER8QIwxb87GNcmwtn7hbzu0tFQ6azMNG/EEr/Wydebdf/Oukuh43lh3idRppBtRpm3DBDW1nrC8abuDt8fKbk9XjzcWT3VtPt2rvj5b+frX2cqfweq/w++PK/qKnNWu8nbA2Q9pV2pC0ClfCmr08tpm13I+jwL9/7L3YX3DhjmqePlzNPlvPPblX2K3PHlT9T5dnf0jZWkFkr+io+eClGajph+dw0cum82WT/KXtAablAqdREyZtHrOxFvVuVeh7KW8GQ3JWVd2N7jO2R2nj7Yj20bwzPs2puaU7WevRnP15y/Ok5gbyhPqwMvP2bvL5KnNUT9xmqAdl/8OK38sfVHR+oe/vIYb77uDiR/Pk/YwtapjMYsKmX1N3yZ82vcCH9dBvG/Rhxf/TWu7jwfzPm4mdOc92ydcO6oi+z+Duz9WXuyP86y1CvuJReoX9Ef63dfhGy8z/seoCDqq+x43QySqzW08f3i028547jKuV9GyWvElpn+GbU5orXyKXzup6Oo1Xu7CBi1jvpRKvt4XxSMUogEJSE4pgVgeOu00WUq81omrIbjGHcbNlvF/JOie80GG4eCrI7vAMnLWxOpzXvsoI+jzi6zrZBMAZ50qlkFxlVsIWldoEKXVSsVwkkEGQzmDANSgGTU5APR121mlP76f04Od/lvQf4mp4oPNa1xlAJRKicjmKmlC5KmiAcYNJq8UkErUAVE7JzJAa501IenoGrn59fqzrDNz9yTtG9c9eskXwUsabgJjDmRznQeIpIatT1NNp435vlsj0GisoQrlig0DlHhcZL10ZPHvuApvVrRhj6bpO75OckktYd00Ck6OjN2/yQL4gZeUSU/24fMTC7TeNDVr4PJ1EoUTsoNTMHgZlIFc61s8fYoGDVwW95/nsyxb5GMBmj4zcmBwaA5VSccQkCOu5TtUog8uiBtAwcsUh4YZQIaWULLhgSiekdGCChGI4FLbJ7FoxwOXwEAjSw2pQBElBgU7CsU0NM3bhfACpBuCSV1UNwjUKup+zHC3TTb9qu2hqRGAfMm6VDgMJhzaK6wsBx3LS3Uw4KUxLE9P1sDFiFKds4IJXthhQNmbRdgypJV0MqbmXNi1FtDG73KHmAVslZznsWMtT7WyQCRDlWbya9DbzgaQfCzh0Iatqo0A2aEsxaKHdWNJnz5BQzqMpedWEVgoohRz24HDI70WndZwJMErHCqlYLRP0GhUUhkQIfQDTUi7i1mJhZS66mKdDNjg7gwYtU8NDQ8C1vu/kSoSi/DjpJTyzVChFR1PNarFdSZZTgVIyGA360ql0c6F0sP1/FkkHLIjaIQzmIyoF4JnxZ8utpdXt9Z3j3f3jnZ0H7XYjFk/GYnEHZrfZ7IEwrVDq3CSZYZhiuYpbjJReEqWIApMAQr4Zs4XMl1u7D58cP37++tdXH9+/yVVW6NR8Lj+XLS06AzmTIwTyRCiCZsq3ssWFcqlYLM4d7G78ByAE9DCpla5ZAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Untitled&quot;
        title=&quot;&quot;
        src=&quot;/static/de725eb3ebbad8ef67949cce6bef30e5/e5715/sausages.png&quot;
        srcset=&quot;/static/de725eb3ebbad8ef67949cce6bef30e5/17741/sausages.png 263w,
/static/de725eb3ebbad8ef67949cce6bef30e5/52211/sausages.png 525w,
/static/de725eb3ebbad8ef67949cce6bef30e5/e5715/sausages.png 768w&quot;
        sizes=&quot;(max-width: 768px) 100vw, 768px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Now let’s talk about pizza. As a pizza lover, I usually don’t trust people who don’t like pizza. And Switzerland, has some very nice pizza restaurants. The dough is highly influenced by our Italian neighbors, and yet it’s very thin.&lt;/p&gt;
&lt;p&gt;The unusual part about the pizza here in Switzerland is when you order in. They never cut the pizza into slices. Apparently, this is a way to preserve the taste. So when you are home, watching the movie with friends and the delivery arrives, everybody takes a break to cut the pizzas. Cutting the pizza with friends is another entertaining activity to do at home.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;In Switzerland, it is allowed to eat your dog. You are not allowed to bring company at home to eat it together.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;OK, I know you need some minutes to recover after this fact. Let’s talk about the regular hot dog. I promise no more Halloween splatter references.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/ec5fe1d6fc31d8604097a42b00df96ff/212bf/C349E7F1-F86B-42AB-BFD4-4B0581F04268_1_105_c.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 133.46007604562737%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAbABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAMEAf/EABYBAQEBAAAAAAAAAAAAAAAAAAEAAv/aAAwDAQACEAMQAAABXkbIsw8vromJsD//xAAcEAEAAgIDAQAAAAAAAAAAAAABAgMAERAUIjL/2gAIAQEAAQUCLZVSsn47NmaGDVsrj5FxVyr4gAcf/8QAFREBAQAAAAAAAAAAAAAAAAAAASD/2gAIAQMBAT8BSP/EABYRAAMAAAAAAAAAAAAAAAAAAAECIP/aAAgBAgEBPwENH//EAB0QAAICAgMBAAAAAAAAAAAAAAABAhEhIiAxQTL/2gAIAQEABj8Cz8v0zJUzSOCnZs6XiMCqTOpGyp8P/8QAHRAAAgIDAAMAAAAAAAAAAAAAAREAITFBURBhof/aAAgBAQABPyEgAk4uIQOg0jmchKuXwbv1CDHxKJHWLo1oQYHUNDMWhAHkCUM+DP/aAAwDAQACAAMAAAAQwC/y/8QAGBEBAAMBAAAAAAAAAAAAAAAAAQAQESH/2gAIAQMBAT8QFxms7X//xAAXEQEBAQEAAAAAAAAAAAAAAAABEQAQ/9oACAECAQE/EECchpv/xAAiEAEAAgECBgMAAAAAAAAAAAABABEhMUFRYXGBobGRwdH/2gAIAQEAAT8QyMCowvz1KzeVp7GXUOLK6rx18QANBFF0XFfMbcBVuHp5mFbu7b7yiBEK7OnTPqI97EOclqy0tEIo5/cYzEWvGOpEjP/Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;C349E7F1-F86B-42AB-BFD4-4B0581F04268_1_105_c.jpeg&quot;
        title=&quot;&quot;
        src=&quot;/static/ec5fe1d6fc31d8604097a42b00df96ff/212bf/C349E7F1-F86B-42AB-BFD4-4B0581F04268_1_105_c.jpg&quot;
        srcset=&quot;/static/ec5fe1d6fc31d8604097a42b00df96ff/5d001/C349E7F1-F86B-42AB-BFD4-4B0581F04268_1_105_c.jpg 263w,
/static/ec5fe1d6fc31d8604097a42b00df96ff/7349d/C349E7F1-F86B-42AB-BFD4-4B0581F04268_1_105_c.jpg 525w,
/static/ec5fe1d6fc31d8604097a42b00df96ff/212bf/C349E7F1-F86B-42AB-BFD4-4B0581F04268_1_105_c.jpg 768w&quot;
        sizes=&quot;(max-width: 768px) 100vw, 768px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The Swiss hot dog is called &lt;a href=&quot;https://en.wikipedia.org/wiki/Bratwurst&quot;&gt;Bratwurst&lt;/a&gt;. It’s basically a type of German sausage, which is extremely popular here. When you order a Bratwurst, you get separately the sausage, the bread, and the sauce, which is usually mustard. Good luck fitting the sausage in the bread. Despite the fact that you don’t have any hands, the bread is much smaller.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/3a5f42ee783916dd243126d589fe364b/e5715/nick-the-fat-greek.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 133.46007604562737%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAbCAYAAAB836/YAAAACXBIWXMAAAsTAAALEwEAmpwYAAAHWUlEQVR42h2VWVDU6RXF/xk1VqY0RkAUF3QAHdEwgyiyyr4Issoy0IqKgtDsq0Czydbs3ewC3c2+2cCwCyLDoiiMS1CTchuTmtRMTSZ5mEoqladU5Zd/8XAevu/h1D33nnuuoLtjK7o7PkF3906iYi5TVCajuqmGlm41/VNTpMlycPN0o6jiNoNjg8ytzDOzOEtZVTF1TXJ6h+4QI71EVLQEa5P9CPsMdrFPdxsmRgcpKMlC0VBJW3c7nXf7GZiaJDL6Bp6eLpRWFlNcWUSFopS2zibG7mnp7G0iMjIIHz9HLEwM8dTTQdA7uIs9Br/hnJM1tfVyWlQNaPo7GRgfpa23mwB/HwJ8vQgPDSQq8hJFxXmUlBeQLUvC39eBc3ZmePs4YioWZvHpNgR9Q130DnxKqCSYNk0DbR3NdA320DnQw/nz7ujr7+VzI0NOnzgqwgQbCzMir4RSpSgmPuEqoWEXCBNhbiwq1f8tgqmlBcbHDUnOSETT3YK6u5X+r4fILczlpLEhMlHS045Kfhhr57W2mfAAb8w+N+G8ux3Xb4SQmpVEeuJlLE11OGi8F8HKwRIj40NU1pbS0XNH7IuKgdEhZCW3KbnqA/ea+N/DAf67PABrWoYV+Tjb2xBw0YekdCnK1jpk8cF8tn8b+odFQktrUyzOmG9W19XXTtdAB4Nf36WkuhJ1qoR/9ZUylXODqpshDBTE88ucBqUskYrUWIb72+gY6iIlzI5dOlvQPbQb4eix/fgG+qEdH6D3bjd9w/2iPbRkF8i44mVLb5qEpbIECkM9mJCn8Z+lLr6/38PzijRmWypEVS0E2hxi645t7NL7NcKZ08dJykxlbnkW7cQwI9Nj9Gr7iYqN5rK/G8vVGbDQw18ac/j3WAusDvHDjIYPHeU8GB+kuSqTEOt9HDHWQ89gJ0JLZSSK5kYWnywzOX+P6YU5+rSD+IcEkxLux1xWBLkXnakI86bQ5xzL8hRGKjK5X3+b8YlRSuO8MTcQcDXbjY/1QYT62xIGxyd59OwJ9x8uMb+6QnuXBmc3F6QRQUS7nCLWxpyKIG/iPG2QR/qRFRGAVlHB+OgM8vggbOzsOWF6DEPdXyEU3rrG/JP1zcpmlr9h6fm3VNcrcXG2Iz0zGZlUQk2wC6OZ4mBiQnGzMMXX0RaFXMnw0CwrC4/5+P5H3r35Hi9xo4Tq4lSWN16jvNPI7OojHm5skJGTjaurI3X1ldTVlRHubEGgvQWnTI9yZN9eLvqGUFfbzqR2mo2nf+KnH//Jdx9+ZnFpAyEqOpLktBSy87Pp0Q4xPD0lbk0YQUH+TM1oWX08T9atTHR2/o7De3SwPm1HYmwatWUKFu8t8v7VBzSt3ZSL76qKRgQvF1u8fS8gTZSSnJ5EQkoCpqamSKXXyC/IIjMjCUVFOWbHT3L0kCHerueRZeTR3dbNq/WXDPWMMHx3mlcbf+bd+78jfBXkRXR8HIlpSaSK9pFEXOLECVMyxFW0sbbFw/siLq6e2NrY4uLihq2VFcmx8awtPWZuQuz7+APWV5/z6uV3vHn3M4KHmz1+gf5E3bxOolidq4cbbu4u3MpK58ih/SKZBxf8gnB28eDy9Ti+OGUpSpby7cpjxofHmR6fZXVlnZXlZ0xMLiFI42PFgIwlKTmOa+GBONmdFfvnJ/5F4+rujaOjM05Oztja2uHo7IadgxtlJXJmRau1NjTT3z2IsqaRcnkNPX2jCFWKGmqUtSjra6muKKI4P528/FtcipDgft6XwGAJ4VeiCQq7hpdvEOanz5KZmsrM2KQ4DDXxsQlUVyl5/uINbz+KPWxpbaa5rZl2jUrMQxWqTjUqdTOSyxIsrWywOyeGqJMrDs4eIjyxc/SgprKGhZl5ym6XUJhXyMT4DLOzS6y9eIug0rSj7lTR3dW6iZ6+Dhru1OHm4Y6NvcMmqY29M3ZO7hgeMeLAgYN0qjR0qbtIkMaRnprO6Ogkj9df88cPPyGoO1S0tjcy1FbE/aF61u73Mdleib6uDkZGJpz4/ReYfWnB3r0GbN++HWMjIzrVKjSqLnKyZOTm5jG/sMrC4jqLj/6A0DPQRXOrAkWyP6OKJNbnOlGlhXHK7DAG+rvZtvUT9Pbsw/AzEz7ZsgVrq7P09/WhUDSSlZWHXC5nbGKGxZWnTN77BqGjR0VxUQa5l+zpzv2KF3Nqfnmo5B9rtSxrZdQXRRAR7Cj678tN8sAAf0aGxyi8XYY0LoGEhCTUHT08e/mRjTd/RVCK+xop8SLOxwxNmhdrvXI2mjIZzY9hpFHK2wU5f3taz8eHVYy2SlGU55CccouYGCkJicn4+voTEOBHdY2S8ekHCKniXfB2Nueax0la45wYaSvE394cz1PHCHA4w1nxygV4WFKY5M+0WrRIaRaJyRlkZmWTkppBSGiYmAWp4iDbxMQfQwgN9sbJ6jhXfSzRiJIfiKmsKIhFKt7cm6EBhEgiN2Fp7YCPjx8NzW3i/RlA2dhK5PVozjk4kJKRvkmoHZvm/8Dh/mIbBi8zAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Nick The Fat Greek&quot;
        title=&quot;&quot;
        src=&quot;/static/3a5f42ee783916dd243126d589fe364b/e5715/nick-the-fat-greek.png&quot;
        srcset=&quot;/static/3a5f42ee783916dd243126d589fe364b/17741/nick-the-fat-greek.png 263w,
/static/3a5f42ee783916dd243126d589fe364b/52211/nick-the-fat-greek.png 525w,
/static/3a5f42ee783916dd243126d589fe364b/e5715/nick-the-fat-greek.png 768w&quot;
        sizes=&quot;(max-width: 768px) 100vw, 768px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;But my biggest fight when it comes to food is the salad. I don’t know why, but the lettuce pieces, here in Switzerland, are ridiculously large. It’s just ludicrous. Don’t even think to grab them with your spoon, you will embarrass yourself trying to eat the whole piece at once. You will also decorate the table with the salad dressing.&lt;/p&gt;
&lt;p&gt;After some research, I found the approved way to eat a salad. You have to use your fork and knife to fold them into pieces. Here’s a video for dummies.&lt;/p&gt;
&lt;h2&gt;&lt;iframe width=&quot;100%&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/lTBq3LwSr-8&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;&lt;/h2&gt;
&lt;p&gt;Having a barbecue is one of our most favorite urban activities. Drinks are allowed in the public. Usually people grill sausages, and they enjoy them with fresh salad and drinks. Beer is our national drink.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;There are open fireplaces in many parts of the city to barbecue with friends. Some of them even provide firewood and dining furniture.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In Switzerland, we celebrate with &lt;a href=&quot;https://swissandchips.com/swiss-apero/&quot;&gt;Apéros&lt;/a&gt;. These are in-betweens, organized on special occasions, celebrations, and team bonding events. Their menus usually involve wine, cold meals, cheese, and crackers.&lt;/p&gt;
&lt;p&gt;Due to the fact that I am working in a multinational company, I had the chance to participate in many Apéros and try a variety of cuisines. Every individual brings their national delights to the table.&lt;/p&gt;
&lt;p&gt;You want to celebrate for a release? It’s time for an Apéro. Your friend got a new job? Time for an Apéro. You have no clue what to do with your life? Well, an Apéro may help here, but it’s better to stay away from Alcohol. Just saying…&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;That’s all folks. I have to go. Suddenly I am craving Bratwurst. Don’t worry, I will not eat my dog.&lt;/p&gt;
&lt;p&gt;I have more things to share with you. I am already preparing the third part, with more fascinating facts about Switzerland. So stay tuned. You know where you will find me.&lt;/p&gt;
&lt;p&gt;If you are living in Switzerland, don’t hesitate to suggest your favorite fun facts. You can &lt;a href=&quot;https://x.com/nicotsou/status/1455967631081619460?s=21&quot;&gt;reply to this tweet&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Adieu 👋&lt;/p&gt;
&lt;hr&gt;</content:encoded></item><item><title><![CDATA[Fun Facts About Switzerland - Part I]]></title><description><![CDATA[What makes Swiss transportation so good? Why is driving in Switzerland so boring? How does the Swiss army land aircraft? Let’s discover some…]]></description><link>http://www.nicotsou.com/fun-facts-about-switzerland-part-i/</link><guid isPermaLink="false">http://www.nicotsou.com/fun-facts-about-switzerland-part-i/</guid><pubDate>Wed, 27 Oct 2021 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;What makes Swiss transportation so good? Why is driving in Switzerland so boring? How does the Swiss army land aircraft?&lt;/p&gt;
&lt;p&gt;Let’s discover some of the funniest facts about Switzerland, mixed with my personal experiences and tips. Normally, I write technical articles about software engineering on this blog. This time, I’m going to try something completely different.&lt;/p&gt;
&lt;p&gt;But first, we need a proper introduction.&lt;/p&gt;
&lt;p&gt;Cookie? 🍪&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;For the last 4 years, I have lived and worked in Zurich. I have had so many nice experiences in this country. The nature, the people, the places, the life quality and the list goes on. I was originally born in Thessaloniki. This is the first time I live abroad. In fact, I haven’t even visited Switzerland before moving. It was a completely new experience for me.&lt;/p&gt;
&lt;p&gt;Many people are asking me about the life here. Is it worth it? I am usually trying to avoid all these questions, since it’s a very personal decision at the end, whether a place is good or not for you. This is not what I’m trying to achieve with this article.&lt;/p&gt;
&lt;p&gt;This article contains fun facts about Switzerland. Things I found interesting and unusual. I tried to stay away from stereotypes, although it’s not always the easiest thing when you write an article with this topic.&lt;/p&gt;
&lt;p&gt;The idea for this post came from my recent participation in the &lt;a href=&quot;https://community-z.com/events/zed-compass&quot;&gt;Zed Conference&lt;/a&gt;. We worked with my colleague &lt;a href=&quot;https://fcvbz.ch/fc/?lang=en&quot;&gt;Laszlo Markert&lt;/a&gt; to make a very funny &lt;a href=&quot;https://community-z.com/events/zed-compass/talks/14399&quot;&gt;presentation&lt;/a&gt; about Living and Working in Switzerland. This was a good opportunity for me to put together some words and create this post. Many thanks to Laszlo and his wife for their contribution.&lt;/p&gt;
&lt;p&gt;While I was writing this post, I discovered that I have too much to write. I started splitting them on multiple posts. I thought it would be easier for me to write and for you to read. This is the first part of the series. I will try to post a new one every week.&lt;/p&gt;
&lt;p&gt;OK let’s leave these disclaimers aside. It’s time for the actual facts. Let’s start with the most obvious. Nature.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Switzerland has over 48 mountains and 7000 lakes. It has plenty of activities for everyone. It really feels that you don’t have the time to visit and do everything.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1033px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/321d7f1ae2691ac7183d9e2890283800/3d469/99F82899-E06F-4AC0-BEC7-71B137CFD83B_1_105_c.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 73.76425855513308%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAUCBP/EABUBAQEAAAAAAAAAAAAAAAAAAAID/9oADAMBAAIQAxAAAAHPRKVlaSRX/8QAGhABAQACAwAAAAAAAAAAAAAAAQIDEQASIf/aAAgBAQABBQIHG95p43t83WS5f//EABcRAAMBAAAAAAAAAAAAAAAAAAECEBH/2gAIAQMBAT8BC5P/xAAXEQADAQAAAAAAAAAAAAAAAAABAhAR/9oACAECAQE/AS2z/8QAGRAAAwEBAQAAAAAAAAAAAAAAAAERIRAx/9oACAEBAAY/Asawjl6o/SH/xAAbEAEAAgIDAAAAAAAAAAAAAAABABEhMUFhcf/aAAgBAQABPyGypgOKCg7SHmx5K2XhLFXHc//aAAwDAQACAAMAAAAQTz//xAAWEQEBAQAAAAAAAAAAAAAAAAAAETH/2gAIAQMBAT8QyI//xAAWEQEBAQAAAAAAAAAAAAAAAAAAETH/2gAIAQIBAT8Q2K//xAAbEAEBAAIDAQAAAAAAAAAAAAABEQAhMUFRgf/aAAgBAQABPxCCqEUlPT3FKnUs04o+AJlYWzb071gy0aiI3neI13xj/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;99F82899-E06F-4AC0-BEC7-71B137CFD83B_1_105_c.jpeg&quot;
        title=&quot;&quot;
        src=&quot;/static/321d7f1ae2691ac7183d9e2890283800/3d469/99F82899-E06F-4AC0-BEC7-71B137CFD83B_1_105_c.jpg&quot;
        srcset=&quot;/static/321d7f1ae2691ac7183d9e2890283800/5d001/99F82899-E06F-4AC0-BEC7-71B137CFD83B_1_105_c.jpg 263w,
/static/321d7f1ae2691ac7183d9e2890283800/7349d/99F82899-E06F-4AC0-BEC7-71B137CFD83B_1_105_c.jpg 525w,
/static/321d7f1ae2691ac7183d9e2890283800/3d469/99F82899-E06F-4AC0-BEC7-71B137CFD83B_1_105_c.jpg 1033w&quot;
        sizes=&quot;(max-width: 1033px) 100vw, 1033px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Growing up in Greece, I was convinced there was no other place as beautiful. Switzerland changed my mind. In the winter, the snow covers the tops of the mountains, which blend magnetically in the lake waters. In the summer, the whole country is transformed into an endless green land.&lt;/p&gt;
&lt;p&gt;While you’re enjoying your trekking on a mountain, it is very likely to meet a cow. You can hear them far away from the ringing bells they carry. You will also sense the smell.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/17d893daf264b4afcfd8d5d1398fb8ab/df51d/wolfgang-hasselmann-aj4wPhXF3hA-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDAf/EABUBAQEAAAAAAAAAAAAAAAAAAAIB/9oADAMBAAIQAxAAAAFoalFiYj//xAAZEAEAAgMAAAAAAAAAAAAAAAABAAIQERP/2gAIAQEAAQUCTUoNq84Of//EABYRAQEBAAAAAAAAAAAAAAAAAAASIf/aAAgBAwEBPwGcQ//EABcRAAMBAAAAAAAAAAAAAAAAAAAREyH/2gAIAQIBAT8BtrLn/8QAFxAAAwEAAAAAAAAAAAAAAAAAACAhUf/aAAgBAQAGPwIur//EABsQAAIDAQEBAAAAAAAAAAAAAAABESExYVGR/9oACAEBAAE/IdrHh+lkhk6yFD2uld+n/9oADAMBAAIAAwAAABDYL//EABYRAQEBAAAAAAAAAAAAAAAAAABBcf/aAAgBAwEBPxCTb//EABcRAQADAAAAAAAAAAAAAAAAAAABEVH/2gAIAQIBAT8Qgq4//8QAGxABAQEAAgMAAAAAAAAAAAAAAREAIWExUXH/2gAIAQEAAT8QEA2sCUvc8YuKgCnJd04KSg+9RuPEhO+Mf//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;wolfgang-hasselmann-aj4wPhXF3hA-unsplash.jpg&quot;
        title=&quot;&quot;
        src=&quot;/static/17d893daf264b4afcfd8d5d1398fb8ab/9ecec/wolfgang-hasselmann-aj4wPhXF3hA-unsplash.jpg&quot;
        srcset=&quot;/static/17d893daf264b4afcfd8d5d1398fb8ab/5d001/wolfgang-hasselmann-aj4wPhXF3hA-unsplash.jpg 263w,
/static/17d893daf264b4afcfd8d5d1398fb8ab/7349d/wolfgang-hasselmann-aj4wPhXF3hA-unsplash.jpg 525w,
/static/17d893daf264b4afcfd8d5d1398fb8ab/9ecec/wolfgang-hasselmann-aj4wPhXF3hA-unsplash.jpg 1050w,
/static/17d893daf264b4afcfd8d5d1398fb8ab/39d31/wolfgang-hasselmann-aj4wPhXF3hA-unsplash.jpg 1575w,
/static/17d893daf264b4afcfd8d5d1398fb8ab/1f368/wolfgang-hasselmann-aj4wPhXF3hA-unsplash.jpg 2100w,
/static/17d893daf264b4afcfd8d5d1398fb8ab/df51d/wolfgang-hasselmann-aj4wPhXF3hA-unsplash.jpg 2400w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/photos/aj4wPhXF3hA&quot;&gt;Wolfgang Hasselmann&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Especially if you live in a village, it will be difficult to stay with the windows open while it is summer. This can also be a practical way for you to quit smoking.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/b0186eb4819c277666aa54920e2556d9/d2602/swiss-house.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.90494296577947%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAABQABBP/EABYBAQEBAAAAAAAAAAAAAAAAAAIAAf/aAAwDAQACEAMQAAABT5j9M3GSz//EABsQAAICAwEAAAAAAAAAAAAAAAECAAMEEiER/9oACAEBAAEFArD1cgbeSp2WFdmC8//EABURAQEAAAAAAAAAAAAAAAAAAAAR/9oACAEDAQE/AUf/xAAVEQEBAAAAAAAAAAAAAAAAAAAAEv/aAAgBAgEBPwGlP//EAB0QAAIBBAMAAAAAAAAAAAAAAAABEQIQEjEhMmH/2gAIAQEABj8ChMxr49tobmJO2j//xAAaEAEBAAMBAQAAAAAAAAAAAAABEQAhMUGx/9oACAEBAAE/IViZ6vzKE0Mjmbc2YSjVe3PURsd4kivBn//aAAwDAQACAAMAAAAQmx//xAAWEQEBAQAAAAAAAAAAAAAAAAABEBH/2gAIAQMBAT8QwSf/xAAXEQADAQAAAAAAAAAAAAAAAAAAARFR/9oACAECAQE/EE1qLw//xAAeEAEAAgIBBQAAAAAAAAAAAAABABEhMYFBYZGhwf/aAAgBAQABPxDX9VKew5y8QrdmdR8mpVmkyMDXlO3VAe4JuQQ28kvugwKAT//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Untitled&quot;
        title=&quot;&quot;
        src=&quot;/static/b0186eb4819c277666aa54920e2556d9/9ecec/swiss-house.jpg&quot;
        srcset=&quot;/static/b0186eb4819c277666aa54920e2556d9/5d001/swiss-house.jpg 263w,
/static/b0186eb4819c277666aa54920e2556d9/7349d/swiss-house.jpg 525w,
/static/b0186eb4819c277666aa54920e2556d9/9ecec/swiss-house.jpg 1050w,
/static/b0186eb4819c277666aa54920e2556d9/39d31/swiss-house.jpg 1575w,
/static/b0186eb4819c277666aa54920e2556d9/1f368/swiss-house.jpg 2100w,
/static/b0186eb4819c277666aa54920e2556d9/d2602/swiss-house.jpg 4032w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I mean seriously, take a look at this house. It’s like a dream!&lt;/p&gt;
&lt;p&gt;My previous apartment had a very important feature. A device that circulates the air, without having to keep your windows open. My landlord advertised this feature a lot when I was viewing his apartment. Back then, I couldn’t understand the reason. You see, in Greece without an air conditioner, you will most probably be paralyzed every summer. My mind changed when I discovered the smell. Since then, I am very grateful for this masterpiece of machinery.&lt;/p&gt;
&lt;p&gt;But enough with this cr@p. It’s time to talk about something more adventurous.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;To say that the Swiss transportation system is great, would be an understatement. It’s so good that people who grew up here don’t really understand how good it is.&lt;/p&gt;
&lt;p&gt;You can conveniently go anywhere. To the most infamous village. To the tallest mountain and beyond. It’s just possible. In many places around the world, this is unheard of.&lt;/p&gt;
&lt;p&gt;But where the Swiss transportation system &lt;em&gt;really&lt;/em&gt; shines is not the flexibility, it’s the punctuality.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/01a30eccff1ec4129c54dc889cea9db4/c3b2b/eric-weber-_wB88hxsW8M-unsplash-2.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 125.09505703422053%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAZABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAABAIAA//EABYBAQEBAAAAAAAAAAAAAAAAAAIBA//aAAwDAQACEAMQAAABigsqjC1KVE5Z6RpyP//EABwQAAIDAQADAAAAAAAAAAAAAAECAAMREhAhMf/aAAgBAQABBQKtz0xE1Yg5suGq32oabG2Ee6Y3j//EABcRAQEBAQAAAAAAAAAAAAAAAAABExH/2gAIAQMBAT8BzZJXX//EABgRAQADAQAAAAAAAAAAAAAAAAABAxET/9oACAECAQE/AYsdmMh//8QAHhAAAgEDBQAAAAAAAAAAAAAAAAEQAhEhIjEyQYH/2gAIAQEABj8Cq7jcQtBkVxnI8n//xAAcEAADAAMAAwAAAAAAAAAAAAAAAREhMVFBcYH/2gAIAQEAAT8hcs4uMnpFBBJVdZvJPyqLOkLqsCVykFPJPpuN2dP/2gAMAwEAAgADAAAAEHMzzv/EABkRAQEAAwEAAAAAAAAAAAAAAAEAESExQf/aAAgBAwEBPxDbvZw9kWVf/8QAGBEBAAMBAAAAAAAAAAAAAAAAAQAQEUH/2gAIAQIBAT8QENhrkQFH/8QAHRABAAICAgMAAAAAAAAAAAAAAQARITFxsYGR8P/aAAgBAQABPxA1tKBv68QK7tsYZnaLcIlN9uCtusthXWYRgMFyqQMsOGWBYru7qMVHDM18e53ob5T/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;eric-weber-_wB88hxsW8M-unsplash-2.jpg&quot;
        title=&quot;&quot;
        src=&quot;/static/01a30eccff1ec4129c54dc889cea9db4/9ecec/eric-weber-_wB88hxsW8M-unsplash-2.jpg&quot;
        srcset=&quot;/static/01a30eccff1ec4129c54dc889cea9db4/5d001/eric-weber-_wB88hxsW8M-unsplash-2.jpg 263w,
/static/01a30eccff1ec4129c54dc889cea9db4/7349d/eric-weber-_wB88hxsW8M-unsplash-2.jpg 525w,
/static/01a30eccff1ec4129c54dc889cea9db4/9ecec/eric-weber-_wB88hxsW8M-unsplash-2.jpg 1050w,
/static/01a30eccff1ec4129c54dc889cea9db4/39d31/eric-weber-_wB88hxsW8M-unsplash-2.jpg 1575w,
/static/01a30eccff1ec4129c54dc889cea9db4/1f368/eric-weber-_wB88hxsW8M-unsplash-2.jpg 2100w,
/static/01a30eccff1ec4129c54dc889cea9db4/c3b2b/eric-weber-_wB88hxsW8M-unsplash-2.jpg 3648w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/photos/_wB88hxsW8M&quot;&gt;Eric Weber&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Imagine a world in which every bus, every tram, every train arrives on time. You can conveniently know the exact time you will arrive in your destination. Google Maps departure estimates simply make sense.&lt;/p&gt;
&lt;p&gt;To put this in the appropriate context for everyone to understand, in the Zurich central station, if the train has a 30 second delay, everybody is in a panic. They are checking their watches. They are moving nervously. They are under stress.&lt;/p&gt;
&lt;p&gt;And the best part of it is we already know when there is a delay. The digital signage software is one of the best in the market. The official application also provides useful information about every possible inconvenience.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1024px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/566fb0a57caa95d5e43c1405e84ca57f/2bef9/zurich-city.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.90494296577947%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEFElEQVR42h2SWWxUdRTG580nE3lQYpHSbabMfqezL3furLfTTjtLp9N12pmutKVQWrDsCqgBG6AiRgoCEjGpKEE0UWIqBRJEIxgxqESQRYiAMVEkBB80/rzl4cv558v5f+c7X45qzUeP6D54j8LeWzTvvErz5M/EN36JtGIGaewU4fGzhFedwb3sc0Kbr9G4/28SE9cJrf+GwKafiE3cJjJxh9jBf5CG30KVf+MKrTt+oPvAHZZ/8ICOqbtkt18lsfnCY1F5zTlFeBb38lmSu+7R/uF/iuhDal+9hbz7T2rf/Ivo3geEj4C44hCq0OpzDL97l56pG8Q2fK24Oc0cV//S9wRGT+EamkEcmaV643ekXvuF2ORN4lP3Sb39iPArNxG33CS66w9iB/4lOHIYlX3gBJ6RGdp3XSE4ehqh8wiWrmOkt12m//BvjE5fI7/vNg2Tv1K76RLiqDJ06w1S++4r4veom3M7+TuR3Q+Rx95DVZnZgz53FFPhOLbej9G1HKaq/wThtecJKZhbPaq486+9iHf8W5wrvsDU8Y7yvoRnw2WkLVfxrv0R17rrREemFYf9x5FWX0Qav0Bw/LyS1VkcQ6eo6juJrU+p3bOY8p8qAz/BUJjB23MEoXYUfc9JTP1nEAbPIQx9hWn4PNGlysr69Mvo09swJF7E1bYNaWAa55LPsHZOY2raj6V9WsH7WJM7lJ7XscTXo4sspaLxEObsHtTxrZTU7aQ8d4zasaOoKuwxik0SGttcDVJqq6My0IdWzKH1tbPAoPDOvIIO9GIri0ODLLI2YgovwR/pwOproirQhru6l9TAdlSRRA/RRB9iLI+cXEIoMYgxMkA6W6A2WaDMmkLr78QWbsPml7FWKz3xAq5oJ7FYC05vGp/cTqS6gUzPOlQ6SxiDIPG02o3e4MFgFJmnFtEYnRTrJeaVudCanLiddiSfCb3Fi9HqY2GlHbXewrOL1JRpBbRmgXTheVRVVhGT4KFE58Is+KlU1n9S40df5UUreHmiyMR8XRCDO4Y36MfslfEFqzF6avBKEeri9bS0NDPYnWHVC1uVDI0+Fmic6AwOyiw1PKPx8dQiK/M1IkUKP6/UTaZvgK6VG8h1DdHUXiCdSpJrS9LZkmR5LsCynEhItJHp6EG10NFMiauVcl+BYluGIqNMiaORcmeWcnuaxZ4snoBEusZJb8ZOPqGhM2Eg4tNidzowmrVKHHqichg51aWcjTysnEE/5a7s489WuVdBN3YldFuoGX+8g0C0jlK1FqNgoFit5rlKHcWVWgRzBfVRE/mUkbEuL91dw6iEcAGz1ITBIVMlJvHKrUjVTURqskRjacRgDKPFxsIKDcVlpThM5TQbi2iIammrF8g32OhtcrOyT6Z16QT/AyXNc7w7FmpuAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Untitled&quot;
        title=&quot;&quot;
        src=&quot;/static/566fb0a57caa95d5e43c1405e84ca57f/2bef9/zurich-city.png&quot;
        srcset=&quot;/static/566fb0a57caa95d5e43c1405e84ca57f/17741/zurich-city.png 263w,
/static/566fb0a57caa95d5e43c1405e84ca57f/52211/zurich-city.png 525w,
/static/566fb0a57caa95d5e43c1405e84ca57f/2bef9/zurich-city.png 1024w&quot;
        sizes=&quot;(max-width: 1024px) 100vw, 1024px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Of course, nothing comes for free. And I’m not talking about the cost of the tickets here. Although, now that I’m thinking, I really should, because they are extremely expensive. But that’s another topic for another time. I’m talking about the side-effects of having everything running on time.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;In Zurich, tram and bus drivers can control the traffic lights. They simply stop all the cars.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;What I hate the most is when you realize that your tram is on the other side of the road. You wait for the appropriate moment to cross the street and then you run a marathon to catch it. And right at the moment that you reach its doors, the doors shut in front of you. Now you have to wait for another 15 minutes.&lt;/p&gt;
&lt;p&gt;There were cases where the bus stayed there, with the doors closed, for 1-2 minutes, even if there was a red light in front of it. The driver will completely ignore the people who are yelling and knocking on the doors.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/12013b5a9fb8261d088ac01d367d10ef/df51d/alexis-fauvet-g9iXQKjPlGM-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAUBAgT/xAAWAQEBAQAAAAAAAAAAAAAAAAAAAgP/2gAMAwEAAhADEAAAAddUcaQ4Ewf/xAAZEAACAwEAAAAAAAAAAAAAAAAAAQISEwP/2gAIAQEAAQUC2iPsjaJZlmWZ/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAFxABAQEBAAAAAAAAAAAAAAAAADEQof/aAAgBAQAGPwJO5VV//8QAGhAAAgMBAQAAAAAAAAAAAAAAABEBUWGRof/aAAgBAQABPyF8pekEKmauk2jUf//aAAwDAQACAAMAAAAQEA//xAAXEQEAAwAAAAAAAAAAAAAAAAAAAREh/9oACAEDAQE/EMVD/8QAGBEAAgMAAAAAAAAAAAAAAAAAAAERIVH/2gAIAQIBAT8Qhl6f/8QAGxABAAIDAQEAAAAAAAAAAAAAAQARITFBYZH/2gAIAQEAAT8QSA1XGMtr18RuSg52N7Nj89Zi7b95/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;alexis-fauvet-g9iXQKjPlGM-unsplash.jpg&quot;
        title=&quot;&quot;
        src=&quot;/static/12013b5a9fb8261d088ac01d367d10ef/9ecec/alexis-fauvet-g9iXQKjPlGM-unsplash.jpg&quot;
        srcset=&quot;/static/12013b5a9fb8261d088ac01d367d10ef/5d001/alexis-fauvet-g9iXQKjPlGM-unsplash.jpg 263w,
/static/12013b5a9fb8261d088ac01d367d10ef/7349d/alexis-fauvet-g9iXQKjPlGM-unsplash.jpg 525w,
/static/12013b5a9fb8261d088ac01d367d10ef/9ecec/alexis-fauvet-g9iXQKjPlGM-unsplash.jpg 1050w,
/static/12013b5a9fb8261d088ac01d367d10ef/39d31/alexis-fauvet-g9iXQKjPlGM-unsplash.jpg 1575w,
/static/12013b5a9fb8261d088ac01d367d10ef/1f368/alexis-fauvet-g9iXQKjPlGM-unsplash.jpg 2100w,
/static/12013b5a9fb8261d088ac01d367d10ef/df51d/alexis-fauvet-g9iXQKjPlGM-unsplash.jpg 2400w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/photos/g9iXQKjPlGM&quot;&gt;Alexis Fauvet&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;But what’s their problem, you may ask? Well, as it turns out, the drivers are stressed about being on time. If they are delayed, the system will be notified and this will have a negative impact on their performance. Which may give their manager a reason to write a negative review. At least that’s what they think.&lt;/p&gt;
&lt;p&gt;If you visit Switzerland, make sure to get a ticket for your dog or your bike.&lt;/p&gt;
&lt;p&gt;What did you say? You are arriving by car? Well, keep reading. The next section is dedicated to you.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Driving in Switzerland can be boring.&lt;/p&gt;
&lt;p&gt;You see, most people are driving ridiculously expensive super cars. And yet, the speed limits are some of the lowest you will find in Europe.&lt;/p&gt;
&lt;p&gt;On top of that, you have sneaky cameras everywhere. The police are actually moving them to catch you by surprise. And they are working pretty well. In Switzerland, most drivers respect the speed limits. Because they don’t have another option. The law is &lt;a href=&quot;https://www.ch.ch/en/driving-over-speed-limit/&quot;&gt;very strict&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/ec780ebed1973f513351f23638fb60d1/df51d/nigel-tadyanehondo-d30E78EkCIE-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.90494296577947%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAQFAgb/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIQAxAAAAHb3OslciB//8QAGxAAAgIDAQAAAAAAAAAAAAAAAAEDEgIRIRP/2gAIAQEAAQUChn5dHojfXJYeR//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABoQAAICAwAAAAAAAAAAAAAAAAAxAQIQEZH/2gAIAQEABj8C1boxxlH/xAAcEAACAgIDAAAAAAAAAAAAAAAAAREhEDFBUdH/2gAIAQEAAT8hS04xppeESaCGlTXZC/R//9oADAMBAAIAAwAAABCTL//EABURAQEAAAAAAAAAAAAAAAAAAAAR/9oACAEDAQE/EFf/xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAcEAEAAwACAwAAAAAAAAAAAAABABExIWFBgcH/2gAIAQEAAT8QtQU0eSd9xMVJhXY3/aGQQXUKjBeGlNj6h1pwxT//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;nigel-tadyanehondo-d30E78EkCIE-unsplash.jpg&quot;
        title=&quot;&quot;
        src=&quot;/static/ec780ebed1973f513351f23638fb60d1/9ecec/nigel-tadyanehondo-d30E78EkCIE-unsplash.jpg&quot;
        srcset=&quot;/static/ec780ebed1973f513351f23638fb60d1/5d001/nigel-tadyanehondo-d30E78EkCIE-unsplash.jpg 263w,
/static/ec780ebed1973f513351f23638fb60d1/7349d/nigel-tadyanehondo-d30E78EkCIE-unsplash.jpg 525w,
/static/ec780ebed1973f513351f23638fb60d1/9ecec/nigel-tadyanehondo-d30E78EkCIE-unsplash.jpg 1050w,
/static/ec780ebed1973f513351f23638fb60d1/39d31/nigel-tadyanehondo-d30E78EkCIE-unsplash.jpg 1575w,
/static/ec780ebed1973f513351f23638fb60d1/1f368/nigel-tadyanehondo-d30E78EkCIE-unsplash.jpg 2100w,
/static/ec780ebed1973f513351f23638fb60d1/df51d/nigel-tadyanehondo-d30E78EkCIE-unsplash.jpg 2400w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/photos/d30E78EkCIE&quot;&gt;Nigel Tadyanehondo&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;To some extent, I believe the police intentionally set the speed limits that low, in order to collect more money. Otherwise, I’m not sure why there is a limit of 80 km/h (50 mph) on certain highways. In comparison with other countries, Switzerland has the best roads in terms of construction. On top of that, our neighbors in Germany have highways with no speed limits. And in Italy on the other side, the speed limits are just for decoration.&lt;/p&gt;
&lt;p&gt;OK, you didn’t read this from me. Speed limits are actually helping us. Switzerland is one of the safest countries to drive worldwide.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a77dbd88044ccf15019c01ac6a3abd4d/df51d/andreas-gucklhorn-X3420VR5T9w-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAALABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAQCAwX/xAAVAQEBAAAAAAAAAAAAAAAAAAADBP/aAAwDAQACEAMQAAABXcy5JLQANP8A/8QAGxAAAgEFAAAAAAAAAAAAAAAAAAIBAxESITL/2gAIAQEAAQUCUargkvsnm0H/xAAXEQADAQAAAAAAAAAAAAAAAAABAhAh/9oACAEDAQE/AUwz/8QAFREBAQAAAAAAAAAAAAAAAAAAAhD/2gAIAQIBAT8BU//EABgQAAMBAQAAAAAAAAAAAAAAAAABIRBB/9oACAEBAAY/AqydLjz/xAAbEAEAAgMBAQAAAAAAAAAAAAABABEhMWFRkf/aAAgBAQABPyE025w3PF/VRVNF6S243dMzif/aAAwDAQACAAMAAAAQfy//xAAWEQADAAAAAAAAAAAAAAAAAAAQESH/2gAIAQMBAT8QhY//xAAXEQADAQAAAAAAAAAAAAAAAAAAAREh/9oACAECAQE/EKaMP//EAB0QAQACAgIDAAAAAAAAAAAAAAEAEUFRIWFxgdH/2gAIAQEAAT8QIvaNfJV1sqUbDL5iAIbVFZuQ1S5Eeyosjk5dz//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;andreas-gucklhorn-X3420VR5T9w-unsplash.jpg&quot;
        title=&quot;&quot;
        src=&quot;/static/a77dbd88044ccf15019c01ac6a3abd4d/9ecec/andreas-gucklhorn-X3420VR5T9w-unsplash.jpg&quot;
        srcset=&quot;/static/a77dbd88044ccf15019c01ac6a3abd4d/5d001/andreas-gucklhorn-X3420VR5T9w-unsplash.jpg 263w,
/static/a77dbd88044ccf15019c01ac6a3abd4d/7349d/andreas-gucklhorn-X3420VR5T9w-unsplash.jpg 525w,
/static/a77dbd88044ccf15019c01ac6a3abd4d/9ecec/andreas-gucklhorn-X3420VR5T9w-unsplash.jpg 1050w,
/static/a77dbd88044ccf15019c01ac6a3abd4d/39d31/andreas-gucklhorn-X3420VR5T9w-unsplash.jpg 1575w,
/static/a77dbd88044ccf15019c01ac6a3abd4d/1f368/andreas-gucklhorn-X3420VR5T9w-unsplash.jpg 2100w,
/static/a77dbd88044ccf15019c01ac6a3abd4d/df51d/andreas-gucklhorn-X3420VR5T9w-unsplash.jpg 2400w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/photos/X3420VR5T9w&quot;&gt;Andreas Gücklhorn&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;On another note, I have observed that people have the tendency to lease their cars, instead of purchasing them. Nobody wants to maintain the running costs of having a car. As a trend, there are a lot of people who don’t even keep a car in their garage. Why do you need a car if you can just get the train to go anywhere you want?&lt;/p&gt;
&lt;p&gt;Interestingly, other people prefer to use services like Mobility. You locate one of their cars using their app, you enter with your Swiss transportation card, and you basically rent the car for a couple of hours. Once you finish, you can return it to its original location. You may consider this as an option if you don’t use your car on a daily basis.&lt;/p&gt;
&lt;p&gt;I have also observed green buildings, that prevent car owners from renting apartments.&lt;/p&gt;
&lt;p&gt;We are approaching the end of this first part of our Swiss special. Before I let you go, there is one more story I would like to share with you. It is somehow related to the transportation topic.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Flight 702 departs from Addis Ababa Bole International Airport in Addis Ababa, Ethiopia, at 00:30 EAT on 17 February 2014. During the flight, the aircraft’s transponder begins to emit squawk 7500 — the international code for an aircraft hijacking — while flying north over Sudan.&lt;/p&gt;
&lt;p&gt;When the pilot exited the cockpit to use the restroom, the co-pilot locked the cockpit door and continued to fly the aircraft.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/83890f17abe6547a5402b08fc5bd2d04/df51d/devam-jhabak-OcFOfR8QI4g-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.90494296577947%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAQFAQL/xAAWAQEBAQAAAAAAAAAAAAAAAAADAAH/2gAMAwEAAhADEAAAAVsodsMseNv/xAAaEAABBQEAAAAAAAAAAAAAAAAAAQIRExQS/9oACAEBAAEFAqpM7jO4R8i9Fp//xAAWEQEBAQAAAAAAAAAAAAAAAAAAEmH/2gAIAQMBAT8BlOv/xAAWEQEBAQAAAAAAAAAAAAAAAAAAEgH/2gAIAQIBAT8Btev/xAAZEAACAwEAAAAAAAAAAAAAAAAAARARITH/2gAIAQEABj8CyORaZp//xAAaEAACAwEBAAAAAAAAAAAAAAAAARExYRBR/9oACAEBAAE/IZ+hlwUrYs4SJrWH/9oADAMBAAIAAwAAABAv/wD/xAAWEQEBAQAAAAAAAAAAAAAAAAABEFH/2gAIAQMBAT8QMsP/xAAXEQEAAwAAAAAAAAAAAAAAAAAAEUFh/9oACAECAQE/EJ2wf//EAB4QAQACAgEFAAAAAAAAAAAAAAEAESExQRBxkaHh/9oACAEBAAE/EMgRTdDHhv0hYfFo2vyWuiAN5sG67S2hW3Ir2T//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;devam-jhabak-OcFOfR8QI4g-unsplash.jpg&quot;
        title=&quot;&quot;
        src=&quot;/static/83890f17abe6547a5402b08fc5bd2d04/9ecec/devam-jhabak-OcFOfR8QI4g-unsplash.jpg&quot;
        srcset=&quot;/static/83890f17abe6547a5402b08fc5bd2d04/5d001/devam-jhabak-OcFOfR8QI4g-unsplash.jpg 263w,
/static/83890f17abe6547a5402b08fc5bd2d04/7349d/devam-jhabak-OcFOfR8QI4g-unsplash.jpg 525w,
/static/83890f17abe6547a5402b08fc5bd2d04/9ecec/devam-jhabak-OcFOfR8QI4g-unsplash.jpg 1050w,
/static/83890f17abe6547a5402b08fc5bd2d04/39d31/devam-jhabak-OcFOfR8QI4g-unsplash.jpg 1575w,
/static/83890f17abe6547a5402b08fc5bd2d04/1f368/devam-jhabak-OcFOfR8QI4g-unsplash.jpg 2100w,
/static/83890f17abe6547a5402b08fc5bd2d04/df51d/devam-jhabak-OcFOfR8QI4g-unsplash.jpg 2400w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/photos/OcFOfR8QI4g&quot;&gt;Devam Jhabak&lt;/a&gt;. Please note that Swiss Air has nothing to do with this story.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The flight was originally scheduled to arrive at Leonardo da Vinci–Fiumicino Airport in Rome, Italy, at 04:40 CET, before continuing to Malpensa Airport in Milan, Italy. Instead, the aircraft was flown to Geneva, Switzerland.&lt;/p&gt;
&lt;p&gt;The co-pilot makes several circles while communicating with air traffic control at Geneva International Airport. He is trying to negotiate political asylum for himself and an assurance that he would not be extradited to Ethiopia.&lt;/p&gt;
&lt;p&gt;At 06:02 CET, the airplane lands at Geneva International Airport with about 10 minutes of fuel remaining. One of its engines had suffered a flameout.&lt;/p&gt;
&lt;p&gt;The co-pilot exits the aircraft by scaling down a rope that he throws out of the cockpit window, before walking over to police. He surrendered after identifying himself as the hijacker, and was taken into custody. The airport was briefly shut down during the incident. No passengers or crew were injured.&lt;/p&gt;
&lt;p&gt;Now you will ask, how do all these relate to our topic? Keep reading, now the good part starts.&lt;/p&gt;
&lt;p&gt;While flying through their respective airspaces, the flight was escorted by Italian Eurofighter and French Mirage fighter jets. The Swiss Air Force did not respond because the incident occurred outside normal office hours, which are 08:00–12:00 and 13:30–17:00 Monday to Friday.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/9d825513b10b36591b3d848c215cacb6/df51d/ellen-jenni-WkuEX4soRRQ-unsplash.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIEBf/EABYBAQEBAAAAAAAAAAAAAAAAAAIAAf/aAAwDAQACEAMQAAABuR5kazBLf//EABsQAAIBBQAAAAAAAAAAAAAAAAECAAMQERIU/9oACAEBAAEFAtYFNgch0YTtqT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAgBAgEBPwGq/8QAGRAAAwADAAAAAAAAAAAAAAAAAAECECEy/9oACAEBAAY/Ajp5q5tpm0mf/8QAGhABAAMBAQEAAAAAAAAAAAAAAQARITFBUf/aAAgBAQABPyFuY1DuqHjNm5CeqK6QA0vtT//aAAwDAQACAAMAAAAQzM//xAAWEQEBAQAAAAAAAAAAAAAAAAABABH/2gAIAQMBAT8QAsv/xAAVEQEBAAAAAAAAAAAAAAAAAAABEP/aAAgBAgEBPxBU/wD/xAAdEAACAgEFAAAAAAAAAAAAAAABEQAxoSFBYXGx/9oACAEBAAE/ECKatiNpU2hkWIg0WYQFXFKYWAdCMUOWA8n/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;ellen-jenni-WkuEX4soRRQ-unsplash.jpg&quot;
        title=&quot;&quot;
        src=&quot;/static/9d825513b10b36591b3d848c215cacb6/9ecec/ellen-jenni-WkuEX4soRRQ-unsplash.jpg&quot;
        srcset=&quot;/static/9d825513b10b36591b3d848c215cacb6/5d001/ellen-jenni-WkuEX4soRRQ-unsplash.jpg 263w,
/static/9d825513b10b36591b3d848c215cacb6/7349d/ellen-jenni-WkuEX4soRRQ-unsplash.jpg 525w,
/static/9d825513b10b36591b3d848c215cacb6/9ecec/ellen-jenni-WkuEX4soRRQ-unsplash.jpg 1050w,
/static/9d825513b10b36591b3d848c215cacb6/39d31/ellen-jenni-WkuEX4soRRQ-unsplash.jpg 1575w,
/static/9d825513b10b36591b3d848c215cacb6/1f368/ellen-jenni-WkuEX4soRRQ-unsplash.jpg 2100w,
/static/9d825513b10b36591b3d848c215cacb6/df51d/ellen-jenni-WkuEX4soRRQ-unsplash.jpg 2400w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/photos/WkuEX4soRRQ&quot;&gt;Ellen Jenni&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;A Swiss Air Force spokesman stated: “Switzerland cannot intervene because its airbases are closed at night and on the weekend. It’s a question of budget and staffing.” Switzerland relies on neighboring countries to police its airspace outside of regular business hours. The French and Italian Air Forces have permission to escort suspicious flights into Swiss airspace, but do not have authority to shoot down an aircraft over Switzerland.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Let me share with you a personal photo. What kind of a personal blog would this be without such a photo, right?&lt;/p&gt;
&lt;p&gt;This is me hang gliding above Interlaken.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1024px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/887f694c107f41be1e47e9663696ec88/2bef9/hang-gliding.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.90494296577947%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAAsTAAALEwEAmpwYAAAEIUlEQVR42h2SWVOTBxSGvz/Rq1537IU3tuOCS0HGghsWBWSRLSxhR5JAIASQPQlJSAJJvoQ1JBADCSEClrAE0BZnukjtVMdKp1al1tZaLaPj7dNvenvOmfe8532OENy14LlvpHW2jlTlF3TN12OIqonuOZn9o5+ZXSOLr+2EXpiYfNJNWs1puudrmXvVz/wrG/6HPbg2OjDd0OBc60Kw6Jvweo1M3bCiai+hyViGyl7K9UcmvL8amPnLSuSNlcU9K467LaRIgqZbWpb+FVn428HSazc3XgwSfirNPXEgPPrmAeuR28T8URbFScb73KgrtdQXlKMoLsQ+oGXr0Rxzv3lw3TXQMl3OwAMdS3sjrLwfI/rWTeCpkbV3w6y/H0WIRL8juvGQ9TuPuXf/TzwrT7GGdxgObjNomqO3zsJoixGvqhlnowptq4LZsJ3Ylp+poJXWAQWWSA+Dq9347vUjVMiz6WpvpKdfZHA4wHx0iwc/P2PvnzfsvnzH2MZjBr/8BfvUNpr0SuqSL+MobWNYa6Avt4n27DpsldfoyruKMrMIoSw3kR5lOt21GVi0MpxdFVi767H2tTI0MEC438zS4grrqz8QyWkgUt1B6O5bvLeeMbn5O5Nrz/Ev7xJYfc71mzsIem0C3crTGBrT8ZkrMHVkY9cV4dBdleoZdJSmUpOcxHJpIQX79tN+/gLjzpushzZYubPDze9fEv32Bavbr4j99AbBfO0cTVXHqJHFoy6Lx6bPJ7ZspFN9kbWRdoKiFnXBZXTlJZw8cBTlwePkxedzvUIhud5i8esdFla2WY7dY3njR4Se5s9RFR2hOO0TaoovMSGa6ezMZDpQgCw9GXlOOiWpyRz6NI7EM5fwaOvprMgi7uhn6HVmXIFNeofX6B1Zp1OMIQzpz2BuSUBTdpCq4mTOJCWRmROP6MqhsjYJjSKd/fuO8MGHHxN//ABFWWdx9V4h5K5moFfOuNeBGN7ENbeJO7SKYGpLQKeVtrXG06w4QZ08AXEoleHxVCYDeUz75FRVyKiqlDE9VCHlm8+YKY8JWyGjlkJshmpCwQmmfD4i4SCCouawRDSRRmUcrXXHqJQdxmw5T2g2n7mZchZDV/FPlkrks/FaCv4XGTbJcZtLJDh1ZF5OxTdiw9Jbz/SoG6FTeYKGkmM42s/h6UlDo0pi1JNNcE5GeLYUv7sAp+EKg9ZCwtNFTHsKCYzJmBHz8FlzOZUogTTUYjdnIVqrEDTVp4iNl0vNNPTNF7mmT8IXziAQyMbWlyK9Twp+yYl/opzwgozbK22EfSV4+zPwu7Kw6TJwGC4wJeYimnMQzp78CFXZIdTyONrUSSgaTuIdysTUckk6sYivQkr8IxpmJxqILlQyIzmcEXMIOq/gsqYw5LhIwJ3HlDuTcXsm/wHzCBTEYJqW8gAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Untitled&quot;
        title=&quot;&quot;
        src=&quot;/static/887f694c107f41be1e47e9663696ec88/2bef9/hang-gliding.png&quot;
        srcset=&quot;/static/887f694c107f41be1e47e9663696ec88/17741/hang-gliding.png 263w,
/static/887f694c107f41be1e47e9663696ec88/52211/hang-gliding.png 525w,
/static/887f694c107f41be1e47e9663696ec88/2bef9/hang-gliding.png 1024w&quot;
        sizes=&quot;(max-width: 1024px) 100vw, 1024px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Hang gliding is the closest thing you can get to flying like a bird, and Interlaken is one of the best places in the world to do it!&lt;/p&gt;
&lt;p&gt;This is one of the moments you question your friendship with &lt;a href=&quot;http://panos.tech&quot;&gt;panos.tech&lt;/a&gt;. After some days he texted me the following video. Thankfully, he lives far away.&lt;/p&gt;
&lt;iframe width=&quot;100%&quot; height=&quot;360&quot; src=&quot;https://www.youtube.com/embed/dLBJA8SlH2w&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;
&lt;p&gt;Well, I don’t want this to discourage you. It’s really worth a try.&lt;/p&gt;
&lt;p&gt;That’s all, folks! I hope you found these facts entertaining as much as we did. I have already started writing the second part.&lt;/p&gt;
&lt;p&gt;Uf Wiederluege! 👋&lt;/p&gt;
&lt;p&gt;Cover Credit: &lt;a href=&quot;https://unsplash.com/photos/MpufQjt8b3o&quot;&gt;Alain Rieder&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[I built a Twitter clone to learn React]]></title><description><![CDATA[Every month I curate the music that help me concentrate, create and feel, in a music playlist series called "Cherry Pick".]]></description><link>http://www.nicotsou.com/react-twitter-clone/</link><guid isPermaLink="false">http://www.nicotsou.com/react-twitter-clone/</guid><pubDate>Thu, 06 May 2021 23:46:37 GMT</pubDate><content:encoded>&lt;p&gt;My goal with this article is to help you learn React. We will create a small twitter clone. I will guide you on how to build your own app step-by-step and I will try to teach you all the little details you need to get started.&lt;/p&gt;
&lt;p&gt;If you never tried React before in your life, this will be a good starting point for you. If you know already the basics of React, you may find this article boring at best.&lt;/p&gt;
&lt;p&gt;You can fork my GitHub repo: &lt;a href=&quot;https://github.com/nicotsou/react-twitter&quot;&gt;@nicotsou/react-twitter&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Found something strange? You can suggest changes by creating &lt;a href=&quot;https://github.com/nicotsou/react-twitter/issues&quot;&gt;issues&lt;/a&gt;. Pull requests are also welcomed. This tutorial is also available in my &lt;a href=&quot;https://github.com/nicotsou/nicotsou.com/blob/master/content/blog/react-twitter-clone/index.md&quot;&gt;blog repo&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To follow this guide you must have &lt;a href=&quot;https://nodejs.org/en/&quot;&gt;Node.js&lt;/a&gt; installed. Any version above &lt;code class=&quot;language-text&quot;&gt;v10&lt;/code&gt; will work just fine.&lt;/p&gt;
&lt;p&gt;One last thing. For making my tutorials easier to read, I always remove the &lt;code class=&quot;language-text&quot;&gt;;&lt;/code&gt; semicolons in my code snippets. In spite of the fact that they are not really needed, you will find them in the actual project.&lt;/p&gt;
&lt;p&gt;Ready to write some code? OK, let’s go! 🐤 🤩&lt;/p&gt;
&lt;h2&gt;Create a React application&lt;/h2&gt;
&lt;p&gt;Open your terminal, move to your development directory and run the following command:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;npx create&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;react&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;app my&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;twitter&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;clone&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The following prompt will appear. Accept the installation and take a small ☕️ break. The installation will take some time, especially if you don’t have any React projects in your system.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/763b07f3fa09dc704024c44522a4bd1f/7831d/terminal-react-create-app-install.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60.07604562737643%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAAB00lEQVR42p2T226bQBCGN0HlJAPhDMWweDnYBjuOWqfvUPWyUV6zUl8hldpXiXriPs1MZzGJq17Vvfj4Z4adf4cVyxhjamCp787181uFKTeUP6Moyo1hGIQ6qTHW/lxDvCduibeExnSmF0Vo/QjCC9RUDTRNQ4n6QpU5eoGHF56Ltu2g6/kU+6jpBhqzGZqmhaYxA0M3UdeMe+pbMHqIRVX+7FYrXK/Xj33XQd0KEDWHnKeQ8QSyIoE4DSDJIkjTFJIkIdInHqmGURR9tSyrYY7jiK7rhnXfo2hbyMoFiqZGXlfISZtlg3klMClyTNME4zj+G7nB0VAjw+WyHV6/usLN5SX0mx55WWIQhhhEEYZSw4gaYooPuSSa3pGCjIMgOE64bIrhatvjfr+HN9fXuNvtsKlrrKsK27bFilTGTdNgTXVZE0Lgio6JcpA56dGwLPmw2W6lERRFMTZwzjHLMszzfNT5fP7MU00q5TDVD4Y0rqDmQU7V9z3Iz5jO5F8BeZbUdzCkiaThdzkZ8UALftGCU3iYBri3bbsefxtKBrmL3E2+PJGxh/q/jROappl6nvfR9/3PpHfEpxO5c133C+kHukkv6bawM8IkHML+T5zJ4+w3mgfxdidK0D8AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;images/terminalreact-create-app-install.png&quot;
        title=&quot;&quot;
        src=&quot;/static/763b07f3fa09dc704024c44522a4bd1f/e996b/terminal-react-create-app-install.png&quot;
        srcset=&quot;/static/763b07f3fa09dc704024c44522a4bd1f/17741/terminal-react-create-app-install.png 263w,
/static/763b07f3fa09dc704024c44522a4bd1f/52211/terminal-react-create-app-install.png 525w,
/static/763b07f3fa09dc704024c44522a4bd1f/e996b/terminal-react-create-app-install.png 1050w,
/static/763b07f3fa09dc704024c44522a4bd1f/087e3/terminal-react-create-app-install.png 1575w,
/static/763b07f3fa09dc704024c44522a4bd1f/8079d/terminal-react-create-app-install.png 2100w,
/static/763b07f3fa09dc704024c44522a4bd1f/7831d/terminal-react-create-app-install.png 2116w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This will create a subfolder with all the files we need to run our newly created React application:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/9c3db987829ee24b1ec38047f76acb50/e0577/finder-react-create-app.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60.836501901140686%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAACM0lEQVR42qWSzU7bQBCA4VKJQyuSEOL1DyGBtjGxvXY2duI4P/QnBkQaOwhaeiw3bj30Cdr3gRPv0Eo4L0TW01mTUEgvlWrp02pnR9/OrGdlBb9Rx/XfDv1T1ulEzPNiXddj3TBig9Js1Q0aU8YeEDliFec2YxFy6jiOvzL/1sIg+BkeDcDv9bjv+0DdFrQ7XfA7AdBGA0yLArUdsBATcVgTLGpjrCFiM9pgUDetX8IlhC/6fS8Zj8fQH7yZNRhLq4ad2qyZYnJa0/V0t26mpttKLWqk5WolLRGS1i07ZRiTNW1GVA02JTkRrnth103iOIL3wwPOXBckQmCTyLApK5goQblmgN0N4WXNhgKpwHpRhT3ThhZ2oG1XuBCWiDJF1/NMuN9nyeQkgmF4wBvYjoQJsrYFylYZihKBmmXCt8sAvp5LcHmSh8/hOnhtD1pBD8rVHa6Wt6EkkT/Cw2GQXFx8gbOPn3gn6IKkqCBuzW6WVajs7IDv1cAxVaB7MuivJDDx3Tys0LAoxyqxk0fC/YGXTCYxhAdH3G21nwjvpQoUSgpsSBoUyRaiZV2IM8zlBLt50vK7fSc5Pz+D49GYs6b7l5BkT4AsxeZweVnY7zVvo+gDRJOTOxwVjhWJh34gq+LRfom7TCgrtw/CwaA7PR6NIAwPuZi9xQ9ZsLxfgqv4hnjpdDE2a2b99XfXdW9wYK8q1d3rXD5/ndvY+Feu8sXiTa5Q+LEY7FXk2bzc/0E4Vn8D8tQQlzJsO6IAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;images/finder-react-create-app.png&quot;
        title=&quot;&quot;
        src=&quot;/static/9c3db987829ee24b1ec38047f76acb50/e996b/finder-react-create-app.png&quot;
        srcset=&quot;/static/9c3db987829ee24b1ec38047f76acb50/17741/finder-react-create-app.png 263w,
/static/9c3db987829ee24b1ec38047f76acb50/52211/finder-react-create-app.png 525w,
/static/9c3db987829ee24b1ec38047f76acb50/e996b/finder-react-create-app.png 1050w,
/static/9c3db987829ee24b1ec38047f76acb50/087e3/finder-react-create-app.png 1575w,
/static/9c3db987829ee24b1ec38047f76acb50/e0577/finder-react-create-app.png 1796w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Let’s have a closer look at the initial folder structure:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;/node_modules&lt;/code&gt;: This hosts all third-party npm packages that are needed in order to run and deploy our application. This folder is ignored from git. Sometimes it makes sense to delete it and run &lt;code class=&quot;language-text&quot;&gt;npm install&lt;/code&gt;. re-create it.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;/public&lt;/code&gt;: This folder hosts public static files. The best place to put favicons, iOS icons or any other complementary file.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;/src&lt;/code&gt;: This holds the source code of our application. We will work here to build our application.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;How to run the application&lt;/h2&gt;
&lt;p&gt;Give it a try! The application is pretty functional.&lt;/p&gt;
&lt;p&gt;At first you have to navigate inside the application folder:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;cd my&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;twitter&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;clone&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The only thing you have to do is to run the following command:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;npm start&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now wait a bit. Your JavaScript application is building. Since our app is pretty small, this will not take much time.&lt;/p&gt;
&lt;p&gt;If your firewall prompts you about allowing this application to access networks, press accept.&lt;/p&gt;
&lt;p&gt;In seconds you will notice a new browser tab with the following page. If not, try to visit &lt;a href=&quot;http://localhost:3000/&quot;&gt;http://localhost:3000/&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/9cea91b6a38b73757ad56fd36839a167/fc83e/react-create-app.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 64.63878326996198%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAABbElEQVR42qWSy07CQBSGu/QRxBRoOjNlOm2ZDnJTQvASwJVG3qQudWfinq0LH9DLG9D0eE5pABEUscmXmU7m/3rmdCxZqZSM5onSJjGmnUgVJSrSSbXKk3K5ujPM8+5qxhxZjsNHOm5A3OhAu9ODIIxB+hEw4YPL5U4EkclarS5wLoeWbbMxlxp4rT5DUkZ4UUrzBVJ/fV9DYFago2Q747mwVidhVozfYK4ExhRwL4Ite/Ks/auwEIQX16D6VyB8s02KWaqQ/SDEIzDmQzi8hf7DFPr301zMRABFi/YT1ocTOHt6gcHjM0SXN1hhCBT+u5B6J0JguDEcTSAgGR7Z5cF+Qvpr8fEJmGYPYt3Kr1UD56Z5uqmPSyH9arwmgMzouqyC0tTz41R1z3NoTmvr+yhLHymXnVFeIVWDZMW4YNnPeM5K9WtkwtdALuvQdgYOU+/Im8PVaz5uxN+8Tpl57oNcFj4HCEPEP3HJ9QltpkfQR4JnKQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;images/react-create-app.png&quot;
        title=&quot;&quot;
        src=&quot;/static/9cea91b6a38b73757ad56fd36839a167/e996b/react-create-app.png&quot;
        srcset=&quot;/static/9cea91b6a38b73757ad56fd36839a167/17741/react-create-app.png 263w,
/static/9cea91b6a38b73757ad56fd36839a167/52211/react-create-app.png 525w,
/static/9cea91b6a38b73757ad56fd36839a167/e996b/react-create-app.png 1050w,
/static/9cea91b6a38b73757ad56fd36839a167/087e3/react-create-app.png 1575w,
/static/9cea91b6a38b73757ad56fd36839a167/8079d/react-create-app.png 2100w,
/static/9cea91b6a38b73757ad56fd36839a167/fc83e/react-create-app.png 2596w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The application is running successfully and you can interact with it. Well, there’s nothing to interact with at the moment, but we’re gonna change that soon.&lt;/p&gt;
&lt;p&gt;Now try this one. Edit &lt;code class=&quot;language-text&quot;&gt;/src/App.js&lt;/code&gt; file. Change some of its text and save it. Notice that as you save the file, the changes are being reflected to your browser window automatically. That’s the best way to test our application in real time.&lt;/p&gt;
&lt;p&gt;Stop the application by running &lt;code class=&quot;language-text&quot;&gt;Ctrl+C&lt;/code&gt; on the terminal window.&lt;/p&gt;
&lt;p&gt;I want you to delete all the files inside the &lt;code class=&quot;language-text&quot;&gt;/src&lt;/code&gt; folder. We will recreate them one-by-one in the following section.&lt;/p&gt;
&lt;p&gt;Now it’s time to write some code. 👨‍💻&lt;/p&gt;
&lt;h2&gt;Render the React application&lt;/h2&gt;
&lt;p&gt;In this section we will write all the elements that are needed in order to run our app. It’s better to spend some time to understand what is happening under the hood.&lt;/p&gt;
&lt;p&gt;I want to give you an overview of how React works. Unfortunately in this article we can’t explain everything in detail. But I have selected a bunch of nice articles you can read at the end of this experiment.&lt;/p&gt;
&lt;p&gt;In our public folder you will find an &lt;code class=&quot;language-text&quot;&gt;index.html&lt;/code&gt;. Search for the following div element:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;root&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is basically where we are planning to render our React application. The root element.&lt;/p&gt;
&lt;p&gt;React gives us an easy api to place React applications in our existing html elements. You just register them and you forget about them. React will manage all DOM changes for you inside those elements, but it will not touch anything outside.&lt;/p&gt;
&lt;p&gt;The part of the React library that is handling DOM is called &lt;code class=&quot;language-text&quot;&gt;react-dom&lt;/code&gt;. Let’s see how we can use it.&lt;/p&gt;
&lt;p&gt;By the way. If you are thinking now to change the &lt;em&gt;favicon&lt;/em&gt; with the one from twitter, I would not judge.&lt;/p&gt;
&lt;p&gt;To render a React element, create a new file &lt;code class=&quot;language-text&quot;&gt;/src/index.js&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// index.js&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ReactDOM &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;react-dom&apos;&lt;/span&gt;

ReactDOM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;no more hello worlds please&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; document&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;root&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Go ahead and run the application. You should see our friendly message.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/357c75c2b448eb8757923f4e8c576d15/71ba4/no-hello-world.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 57.79467680608364%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAAB7UlEQVR42rWQ32uSURjHN+gmqJzlaNPW3nqpUb2s3v1IGMH+megfaDnbcsvmZrDtFRKKYhfeCAouwR8E8oaIVw4R/IGimL/uFAVvw+P5ds7rbmL0k/rCl+ec5zzP53k4Y6v35wTzoztmcWFhcXlldUmWl5ckSdaiLK9oZ0mSfmhRFBcF4ZZ5fv7hjTEucWo6fFM04uqsOLgnPaC35+5So2mWTjObrglUpzfQS7rLVDdxhV64qNc8uhvohH6SGianBjMzAoym65804FPLunqovMHe3muyu+vEwYEC+6sdbG2/xDOLFTbbNqzPN7Bu3dDOdvsONl9ssdwmizbs7x+So/cfsLZmiWtAn8+nnpykUC6XSSKRQCwWg6qqSCaTyGQyCIfD8Pv9Wr5SqSAajSIejyMSieDj8TFSqRQBU7PZHAFZk9put9Hr9Uin00GtVuOPWuz3+yiVSqhWvzBX0e12kcvl+HC0Wi0Ui0U0Gg0NyOII6PF4VD4tGAySUCiEQCCAQqHAazAcDvErsZrvN/R6vWq9Xkc+nyfZbBbpdBp8Yy5K6U/NB54BMph62kzwFzoDZH8V4wlCyFdmMhgMeCC88HfM+06Bn//Phg6H47Hb7T5SFOWty+V696fmfbzf6XQ+YbhxzjzHfP4fmHPGvwE8MG3wpJ5m6AAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;images/no-hello-world.png&quot;
        title=&quot;&quot;
        src=&quot;/static/357c75c2b448eb8757923f4e8c576d15/e996b/no-hello-world.png&quot;
        srcset=&quot;/static/357c75c2b448eb8757923f4e8c576d15/17741/no-hello-world.png 263w,
/static/357c75c2b448eb8757923f4e8c576d15/52211/no-hello-world.png 525w,
/static/357c75c2b448eb8757923f4e8c576d15/e996b/no-hello-world.png 1050w,
/static/357c75c2b448eb8757923f4e8c576d15/087e3/no-hello-world.png 1575w,
/static/357c75c2b448eb8757923f4e8c576d15/71ba4/no-hello-world.png 1870w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Note that changes specifically to this &lt;code class=&quot;language-text&quot;&gt;index.js&lt;/code&gt; are not being automatically transferred to your browser window. You will have to manually refresh the page every time to see the changes.&lt;/p&gt;
&lt;p&gt;The method &lt;code class=&quot;language-text&quot;&gt;ReactDOM.render()&lt;/code&gt; takes the following parameters:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The React component we want to render.&lt;/li&gt;
&lt;li&gt;a DOM element in which we are planning to render the markup (render output).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;ReactDOM is the one who will take care of updating the DOM as we dynamically make changes. At the moment it will display the following message:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/e7c320db23e1b6c3d6fcfc663854cd29/88745/no-hello-world-browser.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 72.6235741444867%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAAsTAAALEwEAmpwYAAACtElEQVR42o1TXU8TQRRFTfSBQAiFtrbb2Z3Z7+1u6RZKqygmvuqDBoiY8KLRH4BgATHGdx/VxCd/gcaEt5oAgUzVAr8D+A12d693poSgQeskJ2fnbnL23HvP9rFC4YbKlHnDLs54XjBr2N6sjWwjE0Il8qcgZ9CtZbP5mZFMbp5S82afOKHvtyZqFShP1KPpW7chKFXAdnxEETRqAFGZ5HOh6eC4flSvXYdSKfwuBT99/tLm/Bs0m82o2fyacN5K2nt7yeMnTxON6gljRsJ0K7FsLzFMJzEtVz4LpsxMPK/YCcvjgO/2Ue5S3/HxcRvwxHhOWBCsrL+CglsGLwgBBUE3bAkmYQHFmoouVY1FghWiCcGLfYeHh1IwiqI4SRLodDpSsLG6BoruQLV2DcYnJqFarUN1sg4BuvFLIbhBGWzPB9NyItvxQNfNruDfHC4uLsHQUAocnCW2I90Z0qX123OB0AgBuTzpIfhsGYZTafD9McDtg4uwUNxxi2BZDuAchTuxtIioOuQVtbfgKDHBDychrFRl20FYBSuogGa6QLRuAoRDov2H4NLKOqQUAwzLlUthTMAEggsooCOlQBGaQCSEeztcbkAmT6A8XsMWXRCDF22KlgXEXTCOIhKZpb2WsopbzqSvysGLEGPmpEOmd1nUhHMUizDc4qOnsflxEpuOEMXYCMW4sfYizhASO64XU8piotJ4NFuIR7IKshKrGo0ZY7FuGD8ty0ZBuy2DfXR0tHfuDJ83IJXOYpsuhleHMd+FhbtT8OjeNCzcmQLPtjHYunAaCaf4Vx1IwY2Njbc7Ozv7W1tbu9vb23xzc5O3Wi0+N/eA9/cP8lxO4UOpDK8GJv/wepZ/fPOQv395n9tU5cMjWZ7O5HYRB+l09p0UxHMFMYAYPMHAmfvAH/d/1S4jLvwCcrkfbQJzZ2EAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;images/no-hello-world-browser.png&quot;
        title=&quot;&quot;
        src=&quot;/static/e7c320db23e1b6c3d6fcfc663854cd29/e996b/no-hello-world-browser.png&quot;
        srcset=&quot;/static/e7c320db23e1b6c3d6fcfc663854cd29/17741/no-hello-world-browser.png 263w,
/static/e7c320db23e1b6c3d6fcfc663854cd29/52211/no-hello-world-browser.png 525w,
/static/e7c320db23e1b6c3d6fcfc663854cd29/e996b/no-hello-world-browser.png 1050w,
/static/e7c320db23e1b6c3d6fcfc663854cd29/087e3/no-hello-world-browser.png 1575w,
/static/e7c320db23e1b6c3d6fcfc663854cd29/8079d/no-hello-world-browser.png 2100w,
/static/e7c320db23e1b6c3d6fcfc663854cd29/88745/no-hello-world-browser.png 2148w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;We haven’t defined a React component yet. Let’s do so:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; React &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;react&apos;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ReactDOM &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;react-dom&apos;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// This is our first React component&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;App&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;no more hello worlds please&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// We use &amp;lt;App /&gt; as the root React component to render&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;ReactDOM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;App&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; document&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;root&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we imported the core &lt;code class=&quot;language-text&quot;&gt;react&lt;/code&gt; library. This one allows you to create and manipulate React components. It hosts all the core react features, except everything DOM or mobile related.&lt;/p&gt;
&lt;p&gt;You have to import this library, otherwise your JS runtime will not be smart enough to understand the syntax inside the function &lt;code class=&quot;language-text&quot;&gt;App()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;That’s &lt;a href=&quot;https://reactjs.org/docs/introducing-jsx.html&quot;&gt;JSX&lt;/a&gt;. It basically allows us to write HTML inside JS. It has &lt;em&gt;almost&lt;/em&gt; the same syntax, with some minor differences.&lt;/p&gt;
&lt;p&gt;Our first component is named &lt;code class=&quot;language-text&quot;&gt;App&lt;/code&gt; and it will be the root component of our application. React allows you to write components in the form of JS functions. Simply use the &lt;code class=&quot;language-text&quot;&gt;return&lt;/code&gt; statement to return what you want this component to display on screen.&lt;/p&gt;
&lt;p&gt;Every React component renders something on screen. If you don’t want to render something, then you can use a plain JS function instead of a React component.&lt;/p&gt;
&lt;p&gt;You can create as many components as you want. You can nest components within other components to declaratively express your UI. Similar to how you write HTML.&lt;/p&gt;
&lt;p&gt;In the next section we will do exactly that. We are building a twitter clone, so we will definitely need some React components to display its UI.&lt;/p&gt;
&lt;p&gt;But first let’s fix some minor issues.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;There’s nothing cool with Times New Roman or whatever the default serif font is in your OS.&lt;/p&gt;
&lt;p&gt;Let’s make it nicer by adding the following file under &lt;code class=&quot;language-text&quot;&gt;/src/index.css&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;body &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  font&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;family&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; sans&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;serif&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Ok now it’s more UIsh… We also removed the default margin in &lt;code class=&quot;language-text&quot;&gt;body&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To see the changes we will have to import this css file in our main &lt;code class=&quot;language-text&quot;&gt;index.js&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./index.css&apos;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now you will be wondering, what the hell? We import a css file inside a JS file? How this is even legal?&lt;/p&gt;
&lt;p&gt;Well, it is working perfectly if you refresh your browser. And that’s because your react application is using a bundle loader called &lt;a href=&quot;https://webpack.js.org&quot;&gt;webpack&lt;/a&gt;. Pretty awesome isn’t it?&lt;/p&gt;
&lt;p&gt;If you managed to do everything your codebase will look like this: &lt;a href=&quot;https://github.com/nicotsou/react-twitter/tree/step-1&quot;&gt;https://github.com/nicotsou/react-twitter/tree/step-1&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Now let’s implement that 🐤 twitter app.&lt;/p&gt;
&lt;h2&gt;Display some tweets&lt;/h2&gt;
&lt;p&gt;It’s time to show those tweets! Now it will get more interesting. 😁&lt;/p&gt;
&lt;p&gt;Since this will be a very simple app without any api integration, I have prepared some initial data for you. It’s basically a json file with a list of tweets in it. To make things easier, copy its contents in a new file &lt;code class=&quot;language-text&quot;&gt;src/tweets.json&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now we can move the implementation of the &lt;code class=&quot;language-text&quot;&gt;&amp;lt;App /&gt;&lt;/code&gt; component to a new file. In general, it’s a good practice to keep every React component in its own file.&lt;/p&gt;
&lt;p&gt;As a first step, we will update the &lt;code class=&quot;language-text&quot;&gt;index.js&lt;/code&gt; as follows:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; React &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;react&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ReactDOM &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;react-dom&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; App &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./components/App.jsx&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./index.css&apos;&lt;/span&gt;

ReactDOM&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;App&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; document&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;root&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, let’s create another file &lt;code class=&quot;language-text&quot;&gt;/src/components/App.jsx&lt;/code&gt;. Type the following code in it:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// App.jsx&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; tweets &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;../tweets.json&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;tweets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;tweet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;tweet.content&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;span&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; App&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we are leveraging the &lt;code class=&quot;language-text&quot;&gt;map()&lt;/code&gt; function to loop through all the tweets. That’s how you loop elements in JS. Which brings us to an important observation.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;React is just JS. There’s no pseudo code for us to remember. React basically embraces you to learn JS.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;We imported the list of the tweets from a json file. Similar to css, Webpack can handle other filetypes too. One of these filetypes is the &lt;code class=&quot;language-text&quot;&gt;.jsx&lt;/code&gt;. This is a common pattern to name React components. It’s not mandatory though. You can use &lt;code class=&quot;language-text&quot;&gt;.js&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Note that a React component must always return a single root element. In our case this is a &lt;code class=&quot;language-text&quot;&gt;&amp;lt;div&gt;&lt;/code&gt;. Try to remove it, it will fail. Try to return more elements at the same level, it will again fail. One React element at the root level. Simple rule right?&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;You may ask why this is a React element and not an HTML element. Well, it looks alike because they both use the same tag name &lt;code class=&quot;language-text&quot;&gt;&amp;lt;div&gt;&lt;/code&gt;. But don’t let it fool you.&lt;/p&gt;
&lt;p&gt;React has one React element for every HTML element that is available. When you &lt;code class=&quot;language-text&quot;&gt;return &amp;lt;div&gt;text&amp;lt;/div&gt;;&lt;/code&gt; you are not returning an DOM element. You are returning a React element.&lt;/p&gt;
&lt;p&gt;The &lt;code class=&quot;language-text&quot;&gt;react-dom&lt;/code&gt; library (the one we declared before in &lt;code class=&quot;language-text&quot;&gt;index.js&lt;/code&gt;) will take all these React elements, render them and this will produce the corresponding DOM tree. Another important thing to have in mind. A small detail, but it makes a lot of difference.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;There’s another thing that is missing.&lt;/p&gt;
&lt;p&gt;If you check the console in DevTools you will see the following warning message:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token literal-property property&quot;&gt;Warning&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; Each child &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; a list should have a unique &lt;span class=&quot;token string&quot;&gt;&quot;key&quot;&lt;/span&gt; prop&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;React also provides you a link to the &lt;a href=&quot;https://reactjs.org/docs/lists-and-keys.html#keys&quot;&gt;associated article&lt;/a&gt; in their documentation.&lt;/p&gt;
&lt;p&gt;Basically, React internally creates an auto generated key for every element. This key is being used in order to associate the actual DOM elements with the React elements. When you are dynamically rendering a list of items, React isn’t able to generate keys automatically. It simply cannot distinguish those elements because there’s no common attribute.&lt;/p&gt;
&lt;p&gt;In order to help React recognize what is being rendered, we need to manually add a key attribute:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;tweets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;tweet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;        &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;tweet&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;id&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;tweet.content&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;span&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;      &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This key is a reserved prop for every React component. The key has to be unique, otherwise React will not be able to update the DOM correctly.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Now let’s add some structure:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; tweets &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;../tweets.json&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;timeline&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;tweets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; user&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; created_on&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; content &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;        &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;id&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-header&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-user&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;user&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;span&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; ·&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos; &apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-created-on&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;created_on&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;span&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-content&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;      &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; App&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here I used classes to differentiate the divs. We will add the corresponding styles later.&lt;/p&gt;
&lt;p&gt;Take a look at how we retrieve the data from tweets. We use a cool new syntax called &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment&quot;&gt;Destructuring Assignment&lt;/a&gt;. That makes the code shorter and easier to follow. Of course it gets some time to practice it.&lt;/p&gt;
&lt;p&gt;Note that even in the map loop, we are &lt;em&gt;only&lt;/em&gt; allowed to return a single root element. If we don’t want any DOM element to be rendered as a wrapper we can use an empty &lt;code class=&quot;language-text&quot;&gt;&amp;lt;&gt;&lt;/code&gt;. This is a syntactic sugar for &lt;code class=&quot;language-text&quot;&gt;React.Fragment&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now that we learned how to create React components, let’s create more. Our goal is to separate the concern of the tweet from the actual application. Then we can add some styling.&lt;/p&gt;
&lt;h2&gt;Displaying our tweets&lt;/h2&gt;
&lt;p&gt;Let’s create a new component &lt;code class=&quot;language-text&quot;&gt;src/components/Tweet.jsx&lt;/code&gt; with the following code:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Tweet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; user&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; createdOn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; children &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; props

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-header&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-user&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;user&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;span&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; ·&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos; &apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-created-on&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;createdOn&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;span&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-content&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;children&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; Tweet&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Our newly created &lt;code class=&quot;language-text&quot;&gt;&amp;lt;Tweet /&gt;&lt;/code&gt; component is only responsible to render tweets. Now it is time for some theory lessons. It will help us understand what is actually happening here.&lt;/p&gt;
&lt;p&gt;I suppose you are familiar with HTML attributes. For example an anchor tag has an attribute to set the href:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;/&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Home&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Similar to HTML elements, React components can have their own attributes. For example our Tweet component has an attribute &lt;code class=&quot;language-text&quot;&gt;&amp;lt;Tweet name=&quot;nicotsou&quot; /&gt;&lt;/code&gt; which can be used from consumers to set the handle name for this tweet. It also has another attribute for the creation date.&lt;/p&gt;
&lt;p&gt;There’s a reserved attribute called &lt;code class=&quot;language-text&quot;&gt;children&lt;/code&gt;. React uses this name convention for all the elements that you render inside this element.&lt;/p&gt;
&lt;p&gt;This means that you are free to decide how to implement the api of your component. If you want your component to accept children, just use this prop.&lt;/p&gt;
&lt;p&gt;In React there is also an easy way to do type checking. You can use a package called &lt;code class=&quot;language-text&quot;&gt;prop-types&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; PropTypes &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;prop-types&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Tweet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; user&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; createdOn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; children &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; props

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-header&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-user&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;user&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;span&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt; ·&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos; &apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-created-on&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;createdOn&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;span&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-content&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;children&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;Tweet&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;propTypes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token literal-property property&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; PropTypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;string&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token literal-property property&quot;&gt;createdOn&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; PropTypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;string&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; Tweet&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;React will now check whether the types of the values match with the types you declared in your component implementation.&lt;/p&gt;
&lt;p&gt;Note that we do &lt;em&gt;not&lt;/em&gt; need to set a type for children, because we allow the consumers of our component to put anything they like.&lt;/p&gt;
&lt;p&gt;Now here’s the good thing about this type checking system. It will &lt;em&gt;only&lt;/em&gt; happen during development. This means while you are running your app locally. This part of the code will not be included in the actual application. That’s good right?&lt;/p&gt;
&lt;p&gt;Of course nowadays we have some more sophisticated tools for type checking, like &lt;a href=&quot;https://www.typescriptlang.org&quot;&gt;TypeScript&lt;/a&gt; or &lt;a href=&quot;https://flow.org&quot;&gt;Flow&lt;/a&gt;. Since React is just a JS library, you can integrate it with any tool you like.&lt;/p&gt;
&lt;p&gt;Another small note from my side. Notice that I’m using &lt;code class=&quot;language-text&quot;&gt;className&lt;/code&gt; instead of &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt;. I guess you’ve read this &lt;a href=&quot;https://reactjs.org/docs/introducing-jsx.html&quot;&gt;JSX article&lt;/a&gt; I referenced before. No? Well, it looks like jsx has some tiny minor differences compared to HTML. Since &lt;code class=&quot;language-text&quot;&gt;class&lt;/code&gt; is a reserved word in JS, they had to come up with a different naming convention for classes that have to do with styling.&lt;/p&gt;
&lt;p&gt;Ok that’s it! We made our first reusable React component.&lt;/p&gt;
&lt;p&gt;To render our new component we have to modify &lt;code class=&quot;language-text&quot;&gt;/src/components/App.jsx&lt;/code&gt; as follows:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; tweets &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;../tweets.json&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Tweet &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./Tweet&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;timeline&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;tweets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; user&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; created_on&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; content &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;        &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Tweet&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;id&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;user&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;createdOn&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;created_on&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Tweet&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;      &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; App&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice the difference in the api. The json property &lt;code class=&quot;language-text&quot;&gt;created_on&lt;/code&gt; has been renamed to camelCase. Well that’s not a strict rule. You can name your attributes however you like. But, please don’t! Be a good person. Nobody uses kebab_case in HTML. 🙈&lt;/p&gt;
&lt;p&gt;Also notice that the &lt;code class=&quot;language-text&quot;&gt;key&lt;/code&gt; attribute is &lt;em&gt;only&lt;/em&gt; needed in the parent component and it doesn’t even appear in the &lt;code class=&quot;language-text&quot;&gt;&amp;lt;Tweet /&gt;&lt;/code&gt; component.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Remember: Only when you render multiple React elements dynamically you have to provide a &lt;code class=&quot;language-text&quot;&gt;key&lt;/code&gt;. The key is &lt;em&gt;not&lt;/em&gt; meant to be used inside the children component.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Our app is finally ready, but it looks like the famous emoji (sic). We will deal with this in the following section.&lt;/p&gt;
&lt;h2&gt;Adding styles&lt;/h2&gt;
&lt;p&gt;When it comes to styling we have plenty of options in React. The simplest one is to use a style attribute:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token literal-property property&quot;&gt;marginBottom&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;20px&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
  ...
&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In certain situations this can be useful. Especially when we have dynamic styles. Instead I will use the same pattern we used in the &lt;code class=&quot;language-text&quot;&gt;index.js&lt;/code&gt; file.&lt;/p&gt;
&lt;p&gt;Next to our Tweets component, let’s add a new stylesheet file called &lt;code class=&quot;language-text&quot;&gt;/src/components/Tweet.css&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-css line-numbers&quot;&gt;&lt;code class=&quot;language-css&quot;&gt;&lt;span class=&quot;token selector&quot;&gt;.tweet&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 10px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token selector&quot;&gt;.tweet-header&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; flex&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;justify-content&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; space-between&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;margin-bottom&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 5px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token selector&quot;&gt;.tweet-user&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;font-weight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; bold&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;rgb&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;29&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 161&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 242&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;padding-right&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 5px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token selector&quot;&gt;.tweet-created-on&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;flex-grow&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 1&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;padding-left&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 5px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;rgb&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;110&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 118&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 125&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token selector&quot;&gt;.tweet-content&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;padding-left&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 16px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;padding-bottom&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 20px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Of course in order to make it work we would need to import this file in our &lt;code class=&quot;language-text&quot;&gt;Tweet.jsx&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-css line-numbers&quot;&gt;&lt;code class=&quot;language-css&quot;&gt;import &lt;span class=&quot;token string&quot;&gt;&apos;./Tweet.css&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Not bad isn’t it? I would &lt;em&gt;not&lt;/em&gt; call it art. At least not yet. But we are getting there… 🎨&lt;/p&gt;
&lt;p&gt;![images/react-twitter-clone first draft.png](images/react-twitter-clone first draft.png)&lt;/p&gt;
&lt;p&gt;If you followed everything until this point, your codebase will look like this: &lt;a href=&quot;https://github.com/nicotsou/react-twitter/tree/step-2&quot;&gt;https://github.com/nicotsou/react-twitter/tree/step-2&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Next step: refactoring! 🧑‍🎨&lt;/p&gt;
&lt;h2&gt;Adding more components&lt;/h2&gt;
&lt;p&gt;I’m not 100% satisfied with the current version of our &lt;code class=&quot;language-text&quot;&gt;&amp;lt;App /&gt;&lt;/code&gt; component. I am thinking to separate the logic of the timeline with the app itself. I take my notepad and after some thinking, I came up with the following sketch:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/7a1831ee19ad4dcac3bdd7429357edc6/29114/components-diagram.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 56.27376425855514%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAACd0lEQVR42mNgAIL/DAyMIAxhh7L9Z0hj/a+VxfNRKlv4v0yy0H+lNP7/wkm8/xkKOUH0P6FcPoja/4wgjGADzfjPUM/0396eBWJoPctn8QyxdeHVytsCy2QW59bzTcusENzinSm4LbRSdL9HmcyslDKZVxoFkiCLEY6pZ2GAgf8iaZL/xbJ1/4unaP8XBLqEM126o6xGu6S10bqmqUa3oq1erbSl3qi4tV6rvKOc32dmPddzvRzF/yIpav8l0rX+iWVY/JfINP0vmqP/X7iUF+RCln8quewgDLLttXq21CGQa0LredZlVwivCi0Ump5eJd1dXC8GtEAzb0K93AudPPH/olk8IK+DDPlvDAwimVBOoKuZES6FhuEb1ULp415lmsecCqWPehXLg/A12zTJo47F8ovSK13npdUEvdEtk2EgBOCRArQV5OXKzkbb7Mltwtn19VKZ09oF6+sh4bQsuVIcZCk4eEARBYo45DBEN/ChbqbgtIxisfr6LB6gIWLtFe2CbRUVwmAD/zMwgrz+TitP7r9QuvR/8Qyd/xJpJkDvSyCbgWLge/18gf7SGvWapiZdoCECvb29Qq2traIVFRWCIFxdXS1/1qpUChK7kDADuRCWfDAMPGNfJNJXUCVZ29joXN9SbwCK2aqWFsmZM2eyguRfWyXx3rXJ0/vPnSz+QypD/b9kjsZ/sUw9lAhBNnC3T7ncev96gc7SUt4+YFguyqpQWZRVrzIxt1V0ZloH/4S8SvEDfkWGwMTP9d8e6DK5TMH/4sXcOCPnuV4M913zZPGnxkUi/5XK+d+YAZOGehLvc70MsbvmeeIgfN80SwKUIbAZAABlKwYlo/uZQQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;images/components-diagram.png&quot;
        title=&quot;&quot;
        src=&quot;/static/7a1831ee19ad4dcac3bdd7429357edc6/e996b/components-diagram.png&quot;
        srcset=&quot;/static/7a1831ee19ad4dcac3bdd7429357edc6/17741/components-diagram.png 263w,
/static/7a1831ee19ad4dcac3bdd7429357edc6/52211/components-diagram.png 525w,
/static/7a1831ee19ad4dcac3bdd7429357edc6/e996b/components-diagram.png 1050w,
/static/7a1831ee19ad4dcac3bdd7429357edc6/087e3/components-diagram.png 1575w,
/static/7a1831ee19ad4dcac3bdd7429357edc6/29114/components-diagram.png 1920w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;We have the following components:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;ComposeForm&lt;/code&gt;, is just an input to post a new tweet.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;Timeline&lt;/code&gt;, is a list of tweets.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;Tweet&lt;/code&gt;, is a tweet that’s being tweeted by tweeters.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;Avatar&lt;/code&gt;, will be used to display profile photo circles.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;App&lt;/code&gt;, is the one that contains the compose form and the timeline.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let’s transfer it in code. Before we start I want you to stop the project and install the following npm package:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-bash line-numbers&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; react-icons &lt;span class=&quot;token parameter variable&quot;&gt;--save&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We want icons to make our app look awesome. Instead of downloading manually those svg files and importing them to our project, we are using a third-part package called &lt;a href=&quot;https://github.com/react-icons/react-icons#readme&quot;&gt;react-icons&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;We used to say “There’s an app for that” for mobile applications. Similarly, in React/JS you will find a lot of open source projects for everything you can imagine. It’s the perfect way to create a beautiful prototype for your app.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I’m now going to paste here the final version of my files. With the knowledge you gained so far, you can follow without me.&lt;/p&gt;
&lt;p&gt;You know what? Let’s go with dark mode. We only need to change some styles in &lt;code class=&quot;language-text&quot;&gt;index.css&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-css line-numbers&quot;&gt;&lt;code class=&quot;language-css&quot;&gt;&lt;span class=&quot;token selector&quot;&gt;body&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 0&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;font-family&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; sans-serif&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token property&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; black&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; white&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token selector&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token property&quot;&gt;box-sizing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; border-box&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We will begin with the &lt;code class=&quot;language-text&quot;&gt;/src/components/Timeline.jsx&lt;/code&gt;, which is very similar to our App component:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Timeline.jsx&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; PropTypes &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;prop-types&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Tweet &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./Tweet.jsx&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./Timeline.css&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Timeline&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; tweets &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;ul&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;timeline&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;tweets&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; user&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; created_on&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; content &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;id&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;timeline-item&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
          &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Tweet&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;user&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;createdOn&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;created_on&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
          &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Tweet&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;ul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

Timeline&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;propTypes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;tweets&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; PropTypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;array&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; Timeline&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This component now receives as a prop the &lt;code class=&quot;language-text&quot;&gt;tweets&lt;/code&gt; collection. For the sake of semantics, I have converted the div to an unordered list. Again, notice how we now use a key for every &lt;code class=&quot;language-text&quot;&gt;&amp;lt;li&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Another thing to note here is how we access the &lt;code class=&quot;language-text&quot;&gt;tweets&lt;/code&gt; variable from the &lt;code class=&quot;language-text&quot;&gt;Timeline()&lt;/code&gt; function. Notice the &lt;code class=&quot;language-text&quot;&gt;{}&lt;/code&gt;. The following syntax is equivalent:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;docker&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-docker line-numbers&quot;&gt;&lt;code class=&quot;language-docker&quot;&gt;function Timeline(props) {
	const tweets = props.tweets;
}

function Timeline(props) {
	const { tweets } = props;
	...
}

function Timeline({ props }) {
	...
}&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And here is its associated stylesheet &lt;code class=&quot;language-text&quot;&gt;/src/components/Timeline.css&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-css line-numbers&quot;&gt;&lt;code class=&quot;language-css&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/* Timeline.css */&lt;/span&gt;
&lt;span class=&quot;token selector&quot;&gt;.timeline&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 0&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 0&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;border&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 1px solid &lt;span class=&quot;token function&quot;&gt;rgb&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;47&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 51&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 54&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token selector&quot;&gt;.timeline-item&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;list-style&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; none&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 1px solid &lt;span class=&quot;token function&quot;&gt;rgb&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;47&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 51&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 54&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token selector&quot;&gt;.timeline-item:last-child&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; none&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now let’s add the Avatar component in &lt;code class=&quot;language-text&quot;&gt;/src/components/Avatar.jsx&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Avatar.jsx&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; PropTypes &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;prop-types&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./Avatar.css&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Avatar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;avatar&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

Avatar&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;propTypes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; PropTypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;string&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; Avatar&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This basically doesn’t display the image of the user, but it just renders the first letter of their username. Having access to profile images requires access to the &lt;a href=&quot;https://developer.x.com/en&quot;&gt;X developer api&lt;/a&gt;. I thought this project is already too much for people who start with React and I didn’t want to introduce extra complexity. In a future post we will learn how to consume apis, by implementing more complex React applications.&lt;/p&gt;
&lt;p&gt;Here’s the associated stylesheet &lt;code class=&quot;language-text&quot;&gt;/src/components/Avatar.css&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-css line-numbers&quot;&gt;&lt;code class=&quot;language-css&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/* Avatar.css */&lt;/span&gt;
&lt;span class=&quot;token selector&quot;&gt;.avatar&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;border-radius&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 50%&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;background-color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;rgb&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;29&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 161&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 242&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 50px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 50px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;margin-right&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 10px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;flex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 0 0 auto&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;text-align&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; center&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;line-height&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 50px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 1.3em&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;font-weight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 200&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;text-transform&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; uppercase&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We will now consume the Avatar component in &lt;code class=&quot;language-text&quot;&gt;/src/components/Tweet.jsx&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Tweet.jsx&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; PropTypes &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;prop-types&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Avatar &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./Avatar&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./Tweet.css&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Tweet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; user&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; createdOn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; children &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; props

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Avatar&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;user&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-header&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
          &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-user&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;user&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;span&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;·
          &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-created-on&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;createdOn&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;span&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-content&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;children&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

Tweet&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;propTypes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; PropTypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;string&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;createdOn&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; PropTypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;string&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; Tweet&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And its associated stylesheet &lt;code class=&quot;language-text&quot;&gt;/src/components/Tweet.css&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-css line-numbers&quot;&gt;&lt;code class=&quot;language-css&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/* Tweet.css */&lt;/span&gt;
&lt;span class=&quot;token selector&quot;&gt;.tweet&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; flex&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  ...&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token selector&quot;&gt;... .tweet-content&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;padding-left&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 3px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  ...&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now let’s create the compose form component in &lt;code class=&quot;language-text&quot;&gt;/src/components/ComposeForm.jsx&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// ComposeForm.jsx&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Avatar &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./Avatar&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./ComposeForm.css&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ComposeForm&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;form&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;compose-form&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;compose-form-container&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Avatar&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;textarea&lt;/span&gt;
          &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;compose-form-textarea&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
          &lt;span class=&quot;token attr-name&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;What&apos;s happening?&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;button&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;compose-form-submit&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Tweet&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;button&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;form&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; ComposeForm&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We haven’t implemented yet the functionality to tweet, but we are going to add some extra code in the following section. For now it helps to put all the layout together.&lt;/p&gt;
&lt;p&gt;Here’s its associated stylesheet &lt;code class=&quot;language-text&quot;&gt;/src/components/ComposeForm.css&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-css line-numbers&quot;&gt;&lt;code class=&quot;language-css&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/* ComposeForm.css */&lt;/span&gt;
&lt;span class=&quot;token selector&quot;&gt;.compose-form&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; flex&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;flex-direction&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; column&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;align-items&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; flex-end&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 10px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;border&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 1px solid &lt;span class=&quot;token function&quot;&gt;rgb&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;47&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 51&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 54&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token selector&quot;&gt;.compose-form-container&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; flex&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 100%&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token selector&quot;&gt;.compose-form-textarea&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;background&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; none&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;border&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 0&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;outline&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; none&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 100%&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;min-height&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 70px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;font-family&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; sans-serif&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 1.2em&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; white&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token selector&quot;&gt;.compose-form-submit&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;flex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 0 0 auto&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 5px 18px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; auto&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;line-height&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 28px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;background-color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;rgb&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;29&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 161&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 242&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; white&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;border-radius&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 50px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;border&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 0&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 0.9em&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;font-weight&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; bold&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let’s put everything together into &lt;code class=&quot;language-text&quot;&gt;/src/components/App.jsx&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// App.jsx&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ComposeForm &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./ComposeForm&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Timeline &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./Timeline&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; FaX &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;react-icons/fa&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./App.css&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; tweets &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;../tweets.json&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;app&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;FaX&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;app-logo&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;2em&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;ComposeForm&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;separator&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Timeline&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;tweets&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;tweets&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; App&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And its associated stylesheet &lt;code class=&quot;language-text&quot;&gt;/src/components/App.css&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;css&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-css line-numbers&quot;&gt;&lt;code class=&quot;language-css&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;/* App.css */&lt;/span&gt;
&lt;span class=&quot;token selector&quot;&gt;.app&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;max-width&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 600px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 0 auto&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;margin-top&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 20px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token selector&quot;&gt;.app-logo&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;margin-bottom&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 18px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token selector&quot;&gt;.separator&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;background-color&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;rgb&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;21&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 24&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 28&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;border&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 1px solid &lt;span class=&quot;token function&quot;&gt;rgb&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;47&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 51&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 54&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;border-top&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 0&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;border-bottom&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 0&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; 10px&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That was it. Here’s the result UI:&lt;/p&gt;
&lt;p&gt;![images/react-twitter-clone ui.png](images/react-twitter-clone ui.png)&lt;/p&gt;
&lt;p&gt;And your codebase will look like this: &lt;a href=&quot;https://github.com/nicotsou/react-twitter/tree/step-3&quot;&gt;https://github.com/nicotsou/react-twitter/tree/step-3&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This is basically an idea of how we build React apps. We start from a basic structure that makes more sense at that specific moment. We give it a try for a while. Later on, new requirements will come and we may want to restructure our app. Or our app stays forever like this, which is also fine.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Don’t think too much about how to break your app in components. It’s so easy to refactor your code. Go with what you think it makes more sense. This is an art you will learn as the time goes.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In the next session we will add interactivity to our UI elements.&lt;/p&gt;
&lt;h2&gt;Adding interactivity&lt;/h2&gt;
&lt;p&gt;So far we were dealing with the layout, by breaking our application in reusable components. We managed to display a list of tweets and a form that allows the user to post a new tweet. Learning how to deal with UI elements is the half side of the story.&lt;/p&gt;
&lt;p&gt;Now we will go a step further. We will make those components interactive. At the moment, nothing is happening when you press the tweet button. And this is exactly where we will start from.&lt;/p&gt;
&lt;p&gt;Let’s revisit our &lt;code class=&quot;language-text&quot;&gt;&amp;lt;ComposeForm /&gt;&lt;/code&gt; component. To make an input interactive in React you have to actually store its value somewhere. The library provides an api for us. It’s called &lt;em&gt;state&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;We can add state to any React component with a function that React provides for us. It’s called &lt;code class=&quot;language-text&quot;&gt;useState()&lt;/code&gt; and here’s how you can use it:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// useState is a part of the React library - mind the {}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; useState &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;react&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;SomeComponent&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// We define a state variable called day.&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// useState() returns a pair of getter &amp;amp; setter within an array.&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// We use destructuring to assing them in variables of our choice.&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// useState() accepts the initial value as a parameter.&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;day&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; setDay&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Monday&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;//       ^  ^                   ^&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// [getter, setter]             initial value (optional)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is basically a &lt;em&gt;hook&lt;/em&gt;. It’s a kinda complicated topic that you can read more about it in the &lt;a href=&quot;https://reactjs.org/docs/hooks-intro.html&quot;&gt;official documentation&lt;/a&gt;. We will come back to this topic in more detail. I have another post for you. For now, learn its api and understand how React manages state.&lt;/p&gt;
&lt;p&gt;Ok, it’s time for a small experiment. Take a look at the following example:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// ComposeForm.jsx&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; useState &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;react&apos;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Avatar &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./Avatar&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./ComposeForm.css&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ComposeForm&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// As we&apos;ve seen before, useState() returns an array&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// of [getter, setter] that we can name as we want.&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// We set the initial value of editorValue to an empty string.&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;editorValue&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; setEditorValue&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// Before we return anything, we log the current value&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  console&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;editorValue&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// We define a handler for the onChange event of our textarea&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;handleEditorValueChange&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token function&quot;&gt;setEditorValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;target&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;form&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;compose-form&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;compose-form-container&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Avatar&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;textarea&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;          &lt;span class=&quot;token attr-name&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;editorValue&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;          &lt;span class=&quot;token attr-name&quot;&gt;onChange&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;handleEditorValueChange&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;          &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;compose-form-textarea&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
          &lt;span class=&quot;token attr-name&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;What&apos;s happening?&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;button&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;compose-form-submit&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Tweet&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;button&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;form&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; ComposeForm&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here notice that the &lt;code class=&quot;language-text&quot;&gt;onChange&lt;/code&gt; attribute of our &lt;code class=&quot;language-text&quot;&gt;&amp;lt;textarea&gt;&lt;/code&gt; is assigned to the &lt;code class=&quot;language-text&quot;&gt;handleEditorValueChange&lt;/code&gt; function &lt;em&gt;without&lt;/em&gt; calling it. In case you are not familiar with this syntax, this is a feature of JS that allows you to pass functions as plain variables.&lt;/p&gt;
&lt;p&gt;Remember this &lt;code class=&quot;language-text&quot;&gt;console.log()&lt;/code&gt; that we put in our component? I want to show you something. Try to type a message in the &lt;code class=&quot;language-text&quot;&gt;textarea&lt;/code&gt; and check the console output:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/1b64f1b9a436db97d1ec8ad3f09fdc30/react-input-handling.gif&quot; alt=&quot;images/react-input-handling.gif&quot;&gt;&lt;/p&gt;
&lt;p&gt;Let’s break down what exactly is happening here:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Our component will be initially rendered. In the very beginning the value of our state element is an empty &lt;code class=&quot;language-text&quot;&gt;&quot;&quot;&lt;/code&gt; string.&lt;/li&gt;
&lt;li&gt;The user interacts with the &lt;code class=&quot;language-text&quot;&gt;textarea&lt;/code&gt;. In every keystroke, the &lt;code class=&quot;language-text&quot;&gt;onChange&lt;/code&gt; event is being triggered, which is handled by the function &lt;code class=&quot;language-text&quot;&gt;handleEditorValueChange(e)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Inside the implementation of this handler function, we call &lt;code class=&quot;language-text&quot;&gt;setEditorValue(newValue)&lt;/code&gt; to update the state variable.&lt;/li&gt;
&lt;li&gt;React re-renders the current component. Every time we update a state variable, the component gets re-rendered. That’s why you need the &lt;code class=&quot;language-text&quot;&gt;useState()&lt;/code&gt; hook.&lt;/li&gt;
&lt;li&gt;In this second render of the &lt;code class=&quot;language-text&quot;&gt;&amp;lt;ComposeForm /&gt;&lt;/code&gt; component, the value of our state variable &lt;code class=&quot;language-text&quot;&gt;editorValue&lt;/code&gt; will now contain the newly saved value.&lt;/li&gt;
&lt;li&gt;The user presses another key and the process repeats from step 2.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Every time the &lt;code class=&quot;language-text&quot;&gt;onChange&lt;/code&gt; event of the &lt;code class=&quot;language-text&quot;&gt;textarea&lt;/code&gt; is being triggered, our component is being re-rendered. That’s how React works.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;At first we describe our UI with pure JS functions. Then for every event that occurs inside our component, we update the state. This causes the component tree from that point and below, to be re-rendered.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In our example above, the &lt;code class=&quot;language-text&quot;&gt;&amp;lt;ComposeForm /&gt;&lt;/code&gt; component was updating its own state. But state can live in a parent component, if you pass the state variable as a prop to a children component. The state can live in the same level or above, but &lt;em&gt;never&lt;/em&gt; below.&lt;/p&gt;
&lt;p&gt;That’s exactly what we want to achieve with our &lt;code class=&quot;language-text&quot;&gt;tweets&lt;/code&gt;. When the user adds a new tweet, we want to append it to the list of tweets we are currently displaying on screen. But the &lt;code class=&quot;language-text&quot;&gt;&amp;lt;ComposeForm /&gt;&lt;/code&gt; component doesn’t know anything about &lt;code class=&quot;language-text&quot;&gt;tweets&lt;/code&gt;. The &lt;code class=&quot;language-text&quot;&gt;&amp;lt;App /&gt;&lt;/code&gt; component does. We would need to lift the state up.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;But first let’s take a pause.&lt;/p&gt;
&lt;p&gt;To implement this functionality we have to automatically generate unique ids for our tweets. This usually happens on the server, but for simplicity we are going to dynamically generate an id every time the user adds a new tweet.&lt;/p&gt;
&lt;p&gt;There’s a very nice library for that. Stop the project and install it:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;npm install nanoid &lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;save&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Ok, now back to &lt;code class=&quot;language-text&quot;&gt;/src/components/App.jsx&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// App.jsx&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; useState &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;react&apos;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; nanoid &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;nanoid&apos;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; ComposeForm &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./ComposeForm&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Timeline &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./Timeline&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; FaX &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;react-icons/fa&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./App.css&apos;&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; initialTweets &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;../tweets.json&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// this is a way to store the current user name&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;CURRENT_USER&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;nicotsou&apos;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// We now store the tweets in the state of our component&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;tweets&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; setTweets&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;initialTweets&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// This handler will be called when we want to post a new tweet**&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;handlePostTweet&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; newTweet &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;      content&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;      &lt;span class=&quot;token literal-property property&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;nanoid&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;      &lt;span class=&quot;token literal-property property&quot;&gt;created_on&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Date&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;      &lt;span class=&quot;token literal-property property&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token constant&quot;&gt;CURRENT_USER&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;      &lt;span class=&quot;token literal-property property&quot;&gt;comments_count&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;      &lt;span class=&quot;token literal-property property&quot;&gt;retweets_count&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;      &lt;span class=&quot;token literal-property property&quot;&gt;favorites_count&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token comment&quot;&gt;// The new tweets array is being created from the values of the&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token comment&quot;&gt;// existing one + the newly created tweet&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token function&quot;&gt;setTweets&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;tweets&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; newTweet&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;app&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;FaX&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;app-logo&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;2em&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;ComposeForm&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;onSubmit&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;handlePostTweet&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;separator&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Timeline&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;tweets&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;tweets&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; App&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here we declared a state variable &lt;code class=&quot;language-text&quot;&gt;tweets&lt;/code&gt; and we stored the contents of the &lt;code class=&quot;language-text&quot;&gt;tweets.json&lt;/code&gt; as the initial value for our array. Previously, our tweets collection was a constant value. Now it is possible to modify its contents.&lt;/p&gt;
&lt;p&gt;We have created the &lt;code class=&quot;language-text&quot;&gt;handlePostTweet()&lt;/code&gt; function which can append a given tweet content to the &lt;code class=&quot;language-text&quot;&gt;tweets&lt;/code&gt; array. Who is triggering this function? The tweet button of course. But this button lives in the &lt;code class=&quot;language-text&quot;&gt;&amp;lt;ComposeForm /&gt;&lt;/code&gt; component.&lt;/p&gt;
&lt;p&gt;We need a way to connect this component with this function. To deal with that, we introduced a new prop &lt;code class=&quot;language-text&quot;&gt;onSubmit&lt;/code&gt; which accepts a function. The function will be called with the new tweet content.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;We will take a moment here to understand what &lt;code class=&quot;language-text&quot;&gt;[...tweets, newTweet]&lt;/code&gt; does. It’s basically generating a new array, with the contents of the existing &lt;code class=&quot;language-text&quot;&gt;tweets&lt;/code&gt; array, by appending the new tweet object. Now you may ask me, why can’t we just use the &lt;code class=&quot;language-text&quot;&gt;.push()&lt;/code&gt; method to add another item to our array? Well, my friends, this is because we try to avoid &lt;em&gt;mutation&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Mutation is a common side-effect in programming. When you touch existing objects to modify their values, you are mutating them. What you have to do instead is to return a copy of your array or object.&lt;/p&gt;
&lt;p&gt;When you work with JS objects and arrays (which are also objects), you should avoid mutations. This makes your app run slower and it can also cause some state inconsistencies. Something that was supposed to be displayed, it simply doesn’t, although its value seems to be updated.&lt;/p&gt;
&lt;p&gt;In JS there are several array methods that can cause mutations. Always check the implementation of these methods, before you use them.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Let’s add this implementation to &lt;code class=&quot;language-text&quot;&gt;ComposeForm.jsx&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; PropTypes &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;prop-types&apos;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; useState &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;react&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Avatar &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./Avatar&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./ComposeForm.css&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ComposeForm&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; onSubmit &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;editorValue&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; setEditorValue&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;handleEditorValueChange&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;setEditorValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;target&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token function-variable function&quot;&gt;handleSubmit&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Prevent the default behavior of the form submission&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// which usually triggers a page reload&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    e&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;preventDefault&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;// Call the onSubmit function with the latest textarea value&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token function&quot;&gt;onSubmit&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;editorValue&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;// Reset the textarea content&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// the user may wants to write another tweet&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token function&quot;&gt;setEditorValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;form&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;compose-form&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;onSubmit&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;handleSubmit&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;compose-form-container&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Avatar&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;textarea&lt;/span&gt;
          &lt;span class=&quot;token attr-name&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;editorValue&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
          &lt;span class=&quot;token attr-name&quot;&gt;onChange&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;handleEditorValueChange&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
          &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;compose-form-textarea&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
          &lt;span class=&quot;token attr-name&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;What&apos;s happening?&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;button&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;compose-form-submit&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;Tweet&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;button&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;form&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;ComposeForm&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;propTypes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;// Mind the isRequired flag,&lt;/span&gt;
  &lt;span class=&quot;token comment&quot;&gt;// if we don&apos;t provide any function it will trhow an error&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;  &lt;span class=&quot;token literal-property property&quot;&gt;onSubmit&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; PropTypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;func&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;isRequired&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; ComposeForm&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We made it! Here’s how this works:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Every time we submit the form (click on the Tweet button), the &lt;code class=&quot;language-text&quot;&gt;&amp;lt;ComposeForm /&gt;&lt;/code&gt; component will trigger the &lt;code class=&quot;language-text&quot;&gt;onSubmit()&lt;/code&gt; event.&lt;/li&gt;
&lt;li&gt;This will be handled by the &lt;code class=&quot;language-text&quot;&gt;handlePostTweet()&lt;/code&gt; handler inside the &lt;code class=&quot;language-text&quot;&gt;&amp;lt;App /&gt;&lt;/code&gt; component, which will update the list of &lt;code class=&quot;language-text&quot;&gt;tweets&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;What will happen next? Well, the &lt;code class=&quot;language-text&quot;&gt;&amp;lt;App /&gt;&lt;/code&gt; component will be re-rendered, because its state has changed. Of course this applies to all the components below it.&lt;/li&gt;
&lt;li&gt;The &lt;code class=&quot;language-text&quot;&gt;&amp;lt;Timeline /&gt;&lt;/code&gt; component will now display the all new &lt;code class=&quot;language-text&quot;&gt;tweets&lt;/code&gt; list, which of course, includes the tweet that was posted.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Well, there’s a catch here. We implemented the functionality to add a new tweet. But the UX is kinda annoying:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/5c047bda639172d6cf0f23e1d36f80ef/Where-is-my-tweet.gif&quot; alt=&quot;images/Where-is-my-tweet.gif&quot;&gt;&lt;/p&gt;
&lt;p&gt;We don’t see the newly added tweet on top of the list, but we have to scroll all the way down to find it. That’s because our list of tweets is not ordered by date. We display the tweets in the same order we received them from the json file.&lt;/p&gt;
&lt;p&gt;Here’s how you can quickly sort those tweets:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Timeline.jsx&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;ul&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;timeline&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;tweets
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;a&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;created_on&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;created_on&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; user&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; created_on&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; content &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;id&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;timeline-item&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Tweet&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;user&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;createdOn&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;created_on&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;content&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Tweet&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;ul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Another issue that we have is the fact that the dates aren’t really readable. We want something closer to how X displays the dates.&lt;/p&gt;
&lt;p&gt;To deal with this issue, we will have to install another npm package:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;npm install moment &lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;save&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I think JS folks know this one pretty well. There’s no better library to deal with dates other than &lt;a href=&quot;https://momentjs.com&quot;&gt;moment&lt;/a&gt;. Here’s what we can do with it:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Tweet.jsx&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; moment &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;moment&apos;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; PropTypes &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;prop-types&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; Avatar &lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./Avatar&apos;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;./Tweet.css&apos;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Tweet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; user&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; createdOn&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; children &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; props

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Avatar&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;user&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-header&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-user&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;user&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;span&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;·&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;span&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-created-on&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;&lt;span class=&quot;token plain-text&quot;&gt;            &lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;moment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;createdOn&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;fromNow&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;          &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;span&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;        &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;div&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;tweet-content&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;children&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;      &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token plain-text&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;div&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

Tweet&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;propTypes &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; PropTypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;string&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token literal-property property&quot;&gt;createdOn&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; PropTypes&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;string&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt; Tweet&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Moment reads the dates and converts them to a very readable text. Now our newly created tweet goes on top of the list:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/895466e6101c681d736e70f0d3214894/posting.gif&quot; alt=&quot;images/posting.gif&quot;&gt;&lt;/p&gt;
&lt;p&gt;And that, ladies and gentlemen, was the implementation of the compose tweet form. I know, it can be trickier than you initially anticipated. React can be a little hard to get in the beginning. But once you understand this reactive nature of the library, you’re gonna fall in love with its simplicity.&lt;/p&gt;
&lt;p&gt;Your codebase will look like this after applying all the changes: &lt;a href=&quot;https://github.com/nicotsou/react-twitter/tree/step-4&quot;&gt;https://github.com/nicotsou/react-twitter/tree/step-4&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Exercises&lt;/h2&gt;
&lt;p&gt;We are approaching the end of this post, but the fun has &lt;em&gt;just&lt;/em&gt; started. 🤩&lt;/p&gt;
&lt;p&gt;I have prepared a couple of simple features you can implement to our twitter clone. Most of them are easy, others may require some extra effort from your side, especially if you are a beginner in JS and Node.js. But definitely it’s a good start.&lt;/p&gt;
&lt;p&gt;I have committed the solutions as separate branches. I encourage you to spend some time trying to implement those features, without looking on my solutions. Partially because your solution could be much better than mine. And of course, because you have to practice what you’ve learned.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/4de150b6a2623cf27a4e666a7f4aa594/29114/tweet-buttons-diagram.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 38.02281368821293%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAABL0lEQVR42oVRvU7DMBgMUDoAadJWtAhRUn6GioUKxFvwGgSJBYEVOYmFLCVKYitiY8nE0C1PwNuF74ojMdBg6eTPZ/u+89myzKiqahcoy3JfSjkg9H3fX3NU91CDa881TbNlbRjrDTo8IriEE621HYbhOM/zMc3DKIoOiZ+AU0o5tJ7Ses/qGhDD5TiOl1QvCDeMMbjtcc4doHVl+INOQXQ2DudZlk3hhuojcEVRDCGIhhAykQx+v/BPQeRk3G5DlOZjiAohZogiTdMZGsFhEAR2pyAyEXQBWUkmJ3TZI6EzWp9DjHBJuCL+wjRy/xPs47lvSXJbPgmPS3n68Rwvv+5fF7xIrlcP4d3nI5+zVHgQxkd1ZljX9Q664iltnoor5/3l5/d1oG04N9m6m375G2HmovxWswTMAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;images/tweet-buttons-diagram.png&quot;
        title=&quot;&quot;
        src=&quot;/static/4de150b6a2623cf27a4e666a7f4aa594/e996b/tweet-buttons-diagram.png&quot;
        srcset=&quot;/static/4de150b6a2623cf27a4e666a7f4aa594/17741/tweet-buttons-diagram.png 263w,
/static/4de150b6a2623cf27a4e666a7f4aa594/52211/tweet-buttons-diagram.png 525w,
/static/4de150b6a2623cf27a4e666a7f4aa594/e996b/tweet-buttons-diagram.png 1050w,
/static/4de150b6a2623cf27a4e666a7f4aa594/087e3/tweet-buttons-diagram.png 1575w,
/static/4de150b6a2623cf27a4e666a7f4aa594/29114/tweet-buttons-diagram.png 1920w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Here’s what you can work on:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Display counters for comments, retweets, favorites and the share button. The last one doesn’t have any counter. You can use the icon library we have already installed. I don’t want to give you more instructions. You can place the buttons wherever you think they fit better.&lt;/li&gt;
&lt;li&gt;Implement share button functionality. For the sake of simplicity, we can just copy the tweet on clipboard. You can also include the name of the user who posted it, for example: &lt;code class=&quot;language-text&quot;&gt;@nicotsou: &quot;Tweet content&quot;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Implement retweet button functionality. Similar to the previous one, we copy the contents of the tweet, but this time we post them as a new tweet. You can have a message like &lt;code class=&quot;language-text&quot;&gt;Retweeted from @nicotsou: &quot;Tweet content&quot;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Implement favorites: The goal here is to store when the tweet is favorite or not. If the tweet is favorited you can change the color of the icon to &lt;em&gt;red&lt;/em&gt; and you can increase its counter by 1. When we click on the favorite button again, restore the color and the counter. To keep the implementation simple, we will only implement this in the button and not involve the &lt;code class=&quot;language-text&quot;&gt;tweets&lt;/code&gt; object. Of course if you refresh your browser window, no favorites will be kept. Not the best UX, but enough for us to get started.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Ah and it doesn’t have to finish today. Nobody is chasing you. 🥸 Take a break and come back to this post tomorrow or some other day. Try to apply what you’ve learned. It’s the best way to learn.&lt;/p&gt;
&lt;h2&gt;Deploying our app&lt;/h2&gt;
&lt;p&gt;Ok I hear you. You want to show your creation with your friends and family. Or let’s just pretend this is a final product that you eventually want to sell.&lt;/p&gt;
&lt;p&gt;Please don’t, we don’t have the copyrights for this social media platform. 🤑 Well, do what you want. Just when they will ask you, you didn’t find this tutorial from me, ok?&lt;/p&gt;
&lt;p&gt;To prepare our app for production we have to run the following command:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-bash line-numbers&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;npm&lt;/span&gt; run build&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will keep your processor busy for a while. Once it is ready, you will find a new &lt;code class=&quot;language-text&quot;&gt;/build&lt;/code&gt; folder in your repo.&lt;/p&gt;
&lt;p&gt;After building your app, you can serve it in your local network by running the following command:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-bash line-numbers&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;npx serve &lt;span class=&quot;token parameter variable&quot;&gt;-s&lt;/span&gt; build&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will serve the build folder on &lt;a href=&quot;http://localhost:5000/&quot;&gt;http://localhost:5000/&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can find more information about hosting your app in the &lt;a href=&quot;https://create-react-app.dev/docs/deployment/&quot;&gt;official create-react-app documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Resources&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Here are the solutions for the exercises: &lt;a href=&quot;https://github.com/nicotsou/react-twitter/compare/step-4...step-5&quot;&gt;1&lt;/a&gt;, &lt;a href=&quot;https://github.com/nicotsou/react-twitter/compare/step-5...step-6&quot;&gt;2&lt;/a&gt;, &lt;a href=&quot;https://github.com/nicotsou/react-twitter/compare/step-6...step-7&quot;&gt;3&lt;/a&gt;, &lt;a href=&quot;https://github.com/nicotsou/react-twitter/compare/step-7...step-8&quot;&gt;4&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;The official &lt;a href=&quot;https://github.com/nicotsou/react-twitter&quot;&gt;repo on GitHub&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Read everything about React in the &lt;a href=&quot;https://reactjs.org&quot;&gt;official documentation&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;We used &lt;a href=&quot;https://create-react-app.dev&quot;&gt;create-react-app&lt;/a&gt; to build this app, which has its own &lt;a href=&quot;https://create-react-app.dev&quot;&gt;documentation&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cover Credit: &lt;a href=&quot;https://unsplash.com/@chrisjdavis&quot;&gt;Chris J. Davis&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The DOM isn't scary. You just never learned it.]]></title><description><![CDATA[The Document Object Model is a tree your browser produces to represent the UI of a website.]]></description><link>http://www.nicotsou.com/dom-issues/</link><guid isPermaLink="false">http://www.nicotsou.com/dom-issues/</guid><pubDate>Wed, 28 Apr 2021 23:46:37 GMT</pubDate><content:encoded>&lt;p&gt;When you think about websites, think about &lt;em&gt;trees&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;Document Object Model&lt;/strong&gt; is a tree your browser produces to represent the UI of a website.&lt;/p&gt;
&lt;p&gt;Every browser provides a JavaScript api for DOM operations. These are called &lt;em&gt;mutations&lt;/em&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;DOM mutations are costly and occasionally inconsistent across browsers. Furthermore, its imperative api produces perplexed code, which is difficult to scale.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That’s why there is a need for UI libraries like React, Angular and Vue.js. These modern JS technologies provide solutions to write declarative code that looks and behaves like plain HTML, by managing DOM mutations in the background.&lt;/p&gt;
&lt;p&gt;Dealing with the DOM has troubled me a lot in the beginning of my career. I hope this article will help you understand these complicated parts. You can also use it as a refresher before joining a technical interview.&lt;/p&gt;
&lt;p&gt;🤵🏼‍♂️ Challenge accepted? Ok, here we go…&lt;/p&gt;
&lt;h2&gt;DOM Basics&lt;/h2&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/1c6f2f3e88c88b22d743d152291c7588/07a9c/cern-first-website.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60.45627376425855%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAAA5klEQVR42pWS2RKCMAxFk8oObmUr+qD//5OYmIvD4MjUhzMkQG5vkhIRzbuwkAoJ4ppnaoQTW/xds3mRQEA54NmykUKUd01s3HRSOAlBuAoD6Nm+LfnIduCuoBMmJwXOioKzPICbM+FRn0LFEQ493AQwwlmPOCAu1GHMDFVUf84xdHXjV+1e8D6NmeFCuTq9wkYL3TCWRrFLWRwOuBqan7GcFq165Ech4UiHKlZiSU9p+S48QI/l1P+0nAk57mGLlj1mp/PVkegoHEVe7HopJhPLIdIgzkCUYAWxEmJvN2R5R78u84cXyR72Pao4nmEAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;cern first website&quot;
        title=&quot;&quot;
        src=&quot;/static/1c6f2f3e88c88b22d743d152291c7588/e996b/cern-first-website.png&quot;
        srcset=&quot;/static/1c6f2f3e88c88b22d743d152291c7588/17741/cern-first-website.png 263w,
/static/1c6f2f3e88c88b22d743d152291c7588/52211/cern-first-website.png 525w,
/static/1c6f2f3e88c88b22d743d152291c7588/e996b/cern-first-website.png 1050w,
/static/1c6f2f3e88c88b22d743d152291c7588/07a9c/cern-first-website.png 1440w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;The birth of the Web. This was the first website ever created. (Source: &lt;a href=&quot;https://home.cern/science/computing/birth-web&quot;&gt;CERN&lt;/a&gt;)&lt;/em&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The Document Object Model represents a web page, which is currently loaded in a browser window.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In other words when you visit a website, your browser constructs a large JavaScript structure, which represents the contents of a website.&lt;/p&gt;
&lt;p&gt;The DOM has a form of a family tree, with &lt;code class=&quot;language-text&quot;&gt;parent&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;children&lt;/code&gt; and &lt;code class=&quot;language-text&quot;&gt;sibling&lt;/code&gt; nodes.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;bash&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-bash line-numbers&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;article
&lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;-- p
&lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;`&lt;/span&gt;-- ul
    &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;-- li
    &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;token variable&quot;&gt;`&lt;/span&gt;&lt;/span&gt;-- &lt;span class=&quot;token string&quot;&gt;&quot;text&quot;&lt;/span&gt;
    `-- li&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A node can be an HTML &lt;code class=&quot;language-text&quot;&gt;element&lt;/code&gt;, plain &lt;code class=&quot;language-text&quot;&gt;text&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;attribute&lt;/code&gt; and pretty much everything you will find in your markup.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/737a231a3caedab95caf0514fa958e4c/devtools-inspect-element.gif&quot; alt=&quot;&quot;&gt;
&lt;em&gt;You can navigate to the available options using the browser console. Give it a try on this website! Right click on some text and select Inspect Element.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;DOM is available &lt;em&gt;only&lt;/em&gt; on web browsers. For example, in Node.js you will not even find the object &lt;code class=&quot;language-text&quot;&gt;window&lt;/code&gt;, the outer container of the DOM. That’s why testing environments mobilize libraries that simulate DOM.&lt;/p&gt;
&lt;p&gt;In addition to the DOM, the browser &lt;code class=&quot;language-text&quot;&gt;window&lt;/code&gt; object provides access to the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;CSSOM, which as you guessed it represents the CSS styles and stylesheets that are attached to a website.&lt;/li&gt;
&lt;li&gt;BOM, which hosts various helpful apis your browser provides, such as &lt;code class=&quot;language-text&quot;&gt;navigator&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;location&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;history&lt;/code&gt;, &lt;code class=&quot;language-text&quot;&gt;XMLHttpRequest&lt;/code&gt;, and the (in)famous &lt;code class=&quot;language-text&quot;&gt;alert()&lt;/code&gt; function.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;What’s wrong with the api?&lt;/h3&gt;
&lt;p&gt;Ah that’s a good one, interacting with DOM elements can sometimes hurt your brain cells.&lt;/p&gt;
&lt;p&gt;Writing code that performs low-level DOM manipulations is like opening the Pandora’s box. Operations that are simple and repetitive, take a lot of code to be implemented.&lt;/p&gt;
&lt;p&gt;Your junior colleagues are starting hating you, your senior colleagues are starting yelling at you, your codebase is becoming more complex than your favorite Italian pasta, changes are taking more time than your promotion, debugging is as painful as the loneliness of Britney Spears, and the fact you can’t reason about your code makes you lose your sleep.&lt;/p&gt;
&lt;p&gt;Let’s see how we can interact with DOM elements.&lt;/p&gt;
&lt;p&gt;Consider the following HTML somewhere in &lt;code class=&quot;language-text&quot;&gt;&amp;lt;body&gt;&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-html line-numbers&quot;&gt;&lt;code class=&quot;language-html&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;nav&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;ul&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;navigationList&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;/&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;Home&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;href&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;/contact&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;Contact&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;a&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;ul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;nav&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now let’s assume we would like to add another list item in the middle because the user has logged in. Here’s a way of approaching this task by using the DOM api:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// create a li element&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; li &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; document&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;createElement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;li&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// create an anchor element and set its attributes&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; anchor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; document&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;createElement&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
anchor&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setAttribute&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;target&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;_blank&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
anchor&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;setAttribute&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;href&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;/settings&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
anchor&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;appendChild&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;document&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;createTextNode&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;User Settings&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// append the anchor to li&lt;/span&gt;
li&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;appendChild&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;anchor&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// append the li to our list (DOM mutation)&lt;/span&gt;
document&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;my-list&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lastElementChild&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;prepend&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;li&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can run the example by yourself in &lt;a href=&quot;https://codepen.io/nicotsou/pen/abpxQYW?editors=1111&quot;&gt;Codepen&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As you can see the api is imperative which makes the developer experience to suffer. It is difficult to read the code and to perform changes.&lt;/p&gt;
&lt;p&gt;But there is &lt;em&gt;another&lt;/em&gt; major problem with the DOM.&lt;/p&gt;
&lt;h3&gt;What about cross-browser compatibility?&lt;/h3&gt;
&lt;p&gt;Ah yes, browsers used to have different DOM implementations and certain APIs are not available everywhere, especially the newest ones.&lt;/p&gt;
&lt;p&gt;The story goes back to the &lt;a href=&quot;https://en.wikipedia.org/wiki/Browser_wars&quot;&gt;Browser wars&lt;/a&gt;. For many years DOM was inadequate. The provided apis were not enough to support the complexity of our web applications. You see, it was never created to support applications in the first place. The web was about delivering content.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/64594667b4294bc8079578d8ff0e7208/29007/mosaic-first-browser.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 73.76425855513308%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAAsTAAALEwEAmpwYAAACMklEQVR42o1Ta2/TQBD0/5fgCyogFAJVGqSkTez4dX6/fXaSFvhHw+w5rlq1kfgw2rW1Nzs7e2epWMH3PSgVwHF9BIFCwDxQIVQcIc0zxGmCJEumSNRtg6ZrX6FuW7i+C8vxXKyWC9wtv+HL1xWWxO3tT2y2G0OgosgQR3GMOBHC1MQZUZwgKzJT/+HjGtbODnC3vodzcOGS3Dk4OLgubMdhfsDetg0k94MAoVKcJnpGGHIKNg5CH3tHwVosf+HTzQ/8/vsHp6czzk+PJh7PJ4yno4lzfg3DkdHUDLBW6y1uVxtESUwPPHgBfYwUi0aMxPDm8Ag9Du+i0z2sKInMEmwZlSPbB5vfHpdAz9KYjZTxUBCyUVlXV0n7QcOSIlEl6gSy6e1DSj+ErCBRgTgrUFQFzS/MRvVxuE4oqnb2Hg/73QU+7ncNtjvNXJsYKLkmNfJy5DUaqF7z8BXCWd1EbDOPeFgTDe9WjarRHLOnQoFGVmrGblI6jm8J5WILqR8GNFWKjxxNk6g2fiVZhyTXSPkvu6CoGtNQVArJHM1S/DCjKnkdPrwwMS8kjHzIsoSwauhf2ZNwINmEsm75v2KeEyU9lkaVqefIITfrUqXHzpOqiai+PDHN2JmnJaia6amJmnc9zPhWc9MpN0RFVdL84hlp3pnusuX5n9QI6TSugORs0PUdCYuXhzOjbL7AE4YLxleYFRlI/nhGx3OWELRkbvppFMllnP+GEFL9cXGD4ftn/AOgMcU8W+RnSAAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;mosaic first browser&quot;
        title=&quot;&quot;
        src=&quot;/static/64594667b4294bc8079578d8ff0e7208/e996b/mosaic-first-browser.png&quot;
        srcset=&quot;/static/64594667b4294bc8079578d8ff0e7208/17741/mosaic-first-browser.png 263w,
/static/64594667b4294bc8079578d8ff0e7208/52211/mosaic-first-browser.png 525w,
/static/64594667b4294bc8079578d8ff0e7208/e996b/mosaic-first-browser.png 1050w,
/static/64594667b4294bc8079578d8ff0e7208/087e3/mosaic-first-browser.png 1575w,
/static/64594667b4294bc8079578d8ff0e7208/29007/mosaic-first-browser.png 1600w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Mosaic was one of the first web browsers. The &lt;a href=&quot;http://www.evolutionoftheweb.com/&quot;&gt;Evolution of the Web&lt;/a&gt; is a great website to learn about web history.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Many big corporations tried to force their own solutions to make the web interactive, which led to a tremendous confusion for web developers. You see, every api was different. Developing an app for Mozilla would not work for Internet Explorer.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;https://www.w3.org/&quot;&gt;W3C&lt;/a&gt; organization was founded in 1994 to standardize browser behaviors and language features. I have to mention here that Microsoft was not willing to support this alliance for years, making &lt;a href=&quot;https://en.wikipedia.org/wiki/Internet_Explorer&quot;&gt;Internet Explorer&lt;/a&gt; the most hated software, after &lt;a href=&quot;https://en.wikipedia.org/wiki/Java_applet&quot;&gt;Java applets&lt;/a&gt; of course.&lt;/p&gt;
&lt;p&gt;Although things are getting better nowadays, there are still a few traps. Quite often certain apis become deprecated in favor of newer ones. There are also cases where companies refuse to implement apis, because it gets in their monopoly plans.&lt;/p&gt;
&lt;p&gt;Chrome is a good example. Developers have access to a superset of web apis that are not available elsewhere, creating a new wave of Chrome developers. On the other side companies which are more desktop-centric like Apple, refrain from supporting those features at first place. They influence developers to write native apps for their platform, using the language and tools provided by them.&lt;/p&gt;
&lt;p&gt;But how can you be sure if you are supposed to use an api or not?&lt;/p&gt;
&lt;p&gt;To avoid unexpected surprises, you should &lt;em&gt;always&lt;/em&gt; consult online references like &lt;a href=&quot;https://developer.mozilla.org/en-US/&quot;&gt;MDN&lt;/a&gt;, or &lt;a href=&quot;https://caniuse.com/&quot;&gt;caniuse&lt;/a&gt;. In this way you can quickly distinguish any incompatibilities.&lt;/p&gt;
&lt;p&gt;In most of the cases there is a way to use modern browser features using a &lt;a href=&quot;https://en.wikipedia.org/wiki/Polyfill_(programming)&quot;&gt;Polyfill&lt;/a&gt;. These js scripts provide an identical api, making it easier to switch to native.&lt;/p&gt;
&lt;p&gt;Using &lt;a href=&quot;https://en.wikipedia.org/wiki/Feature_detection_(web_development)&quot;&gt;feature detection&lt;/a&gt; techniques you can distinguish whether or not to load these extra scripts.&lt;/p&gt;
&lt;p&gt;But wait, there is &lt;em&gt;another&lt;/em&gt; problem with the DOM.&lt;/p&gt;
&lt;h3&gt;What about performance?&lt;/h3&gt;
&lt;p&gt;Ah I almost forgot, DOM operations can be really slow.&lt;/p&gt;
&lt;p&gt;I’m pretty sure you ran into websites with sluggy animations that destroyed the smoothness of your user experience. Compare a gif animation to a Netflix series and you’ll get what I’m trying to explain here.&lt;/p&gt;
&lt;!-- prettier-ignore-start --&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 733px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/0a2d79bd7a75a5515c4fb480567f5a52/00b70/the-horse-in-motion-animation.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 73.38403041825096%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAAAsTAAALEwEAmpwYAAAC40lEQVR42j2TxUsFYRTF5+9z4U4RlGctFMEHgoIoKAoGgh1YqNiFqNjd3d3drajo6sjvwnPxMfOduXHOuXecw8ND3d3daWNjQ9PT0zo6OtLj46O+v791dXWl1dVVnZyc6OXlRT8/P7q+vtbKyoqOj48N+/z8NGxpaUlnZ2dyhoeHlZiYqMDAQIWFhWlgYMASsrKy5HK5FBQUpIaGBm1ubqqoqEjBwcEKCQlRZWWl9vb2VF5ersjISIvj3YFhWVmZkpOTNTg4qKenJ11cXFiC2+1WTU2NMQDr7OxUaGio8vLy7A5zsICAAMXHx2t7e1sOIJ22trZM+v7+vr2fn59bAvJJJpjnwcGB4UienJzU4uKiZmdnzRrIOff393p+frZCIyMjWltb0+vrq35/fy2xq6vL/CHm6+vLmjU2Nmp0dFS3t7emiLiOjg6NjY3JwUgkx8bGKjU1VXjKMPLz8+Xj4yMvLy+Tf3Nzo6SkJHl7exuWk5Ojy8tL8zoiIkK+vr5KS0uTA5u+vj6VlpZasd3dXb2/v6u3t1fNzc32PD09NYb42dTU9C+VJtzJW15etmE6eIUfHg/wkwPOHRb4RjBqsIbvTB35fAcnBtymzMf6+nqT0d3dbZLB2trabPq1tbX6+PgwplNTU6qoqFB7e7v5t7OzYwyxDczBSD8/P0VFRSkmJkZzc3PWOTw8XNHR0fL397dmFMvOzlZGRoYSEhLMJpTRMC4uzlaMwg6T7enpMT88q8FUFxYW9Pb29m/BxMSEseFPwh5iicNzMKyghuPZufX1dfNpfn7eglhmTzESOA8PD3ZntTjkQgAPOVYQU8fHx1VdXW1rUVdXZ1JmZmbU0tKilJQUk4yHJA0NDamgoMA2gKWnOSqLi4vV2toqB1aFhYXKzMxUbm6uUUcaiwpeUlJihWDc39+vqqoqawBjFptBQCQ9Pd2KOx66SEE2U+dOMAFMmz8BnCcyOcTyHRz57CFN/gBye89hk0f9rgAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;the horse in motion animation&quot;
        title=&quot;&quot;
        src=&quot;/static/0a2d79bd7a75a5515c4fb480567f5a52/00b70/the-horse-in-motion-animation.png&quot;
        srcset=&quot;/static/0a2d79bd7a75a5515c4fb480567f5a52/17741/the-horse-in-motion-animation.png 263w,
/static/0a2d79bd7a75a5515c4fb480567f5a52/52211/the-horse-in-motion-animation.png 525w,
/static/0a2d79bd7a75a5515c4fb480567f5a52/00b70/the-horse-in-motion-animation.png 733w&quot;
        sizes=&quot;(max-width: 733px) 100vw, 733px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/The_Horse_in_Motion&quot;&gt;The Horse In Motion&lt;/a&gt; was the first movie ever created and here you can see all of its frames.&lt;/em&gt;&lt;/p&gt;
&lt;!-- prettier-ignore-end --&gt;
&lt;p&gt;An animation is performant enough when it reaches at least &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/Performance/Animation_performance_and_frame_rate&quot;&gt;60 frames per second&lt;/a&gt;. That’s because a common monitor has usually a refresh rate of 60Hz[^In Europe the common refresh rate is 50Hz. There are devices in the market nowadays (like the iPad Pro) which have 120Hz or even more.].&lt;/p&gt;
&lt;p&gt;But how can we reach those 60fps? To answer this question we will have to understand how browsers are rendering a website. How they translate an HTML script to actual pixels on your monitor.&lt;/p&gt;
&lt;p&gt;Here’s where the interesting part begins.&lt;/p&gt;
&lt;p&gt;To render a &lt;em&gt;single frame&lt;/em&gt;, your browser follows the steps below:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;👓 Parsing: Everything starts with a &lt;code class=&quot;language-text&quot;&gt;GET&lt;/code&gt; request to the server. The server responds back, by sending the HTML, which is then being parsed to produce the DOM and CSSOM trees.&lt;/li&gt;
&lt;li&gt;🧮 Style Recalculation: By combining those two trees, your browser produces the &lt;code class=&quot;language-text&quot;&gt;Rendering Tree&lt;/code&gt;. This is quite similar to the DOM, but it includes &lt;em&gt;only&lt;/em&gt; what will be rendered on screen, at this specific frame.&lt;/li&gt;
&lt;li&gt;📐 Reflow: It then builds the &lt;code class=&quot;language-text&quot;&gt;Layout&lt;/code&gt;, by converting those elements in box containers and calculating how much space they take on screen. Elements can affect others, for example the width of the &lt;code class=&quot;language-text&quot;&gt;body&lt;/code&gt; element often affects the width of its children, and so on.&lt;/li&gt;
&lt;li&gt;🖌 Paint: Then for any of these boxes, it renders vectors and then it rasterizes them to create a pixel-friendly view.&lt;/li&gt;
&lt;li&gt;🏞 If there are images, they need to be decoded according to their format and then resized.&lt;/li&gt;
&lt;li&gt;🎼 Composition: Sometimes a website has different layers that are simultaneously evaluated (For example you may have a fixed &lt;code class=&quot;language-text&quot;&gt;&amp;lt;div&gt;&lt;/code&gt; element). To optimize the performance, these layers are being transferred to the GPU.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now, as we change the DOM (for example hiding an element, or setting a new background image), the browser follows most of the steps above, for every single frame. And sometimes it drops frames to ensure the app will remain responsive.&lt;/p&gt;
&lt;p&gt;Modern browsers include Profiling tools to help you identify performance related issues.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/b190ce2199e739e9be97ef7604352739/de55f/devtools-profiling.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.15969581749049%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAAC+ElEQVR42p2Sy08TURTGa1woBmxCAsVqp+10pqWlUypTisirTOfR8pJHZ5LClKcSCbEt2FoReRkUH4mJRuPCLmDrxhgTF/xpTHI/b4sLY9zo4pdz7rnf9927OLbi6mJXubxx/OjR1udSqVKtUSwWq+VypfrkyU61VH5Mz5vV9fVCtUDr5mbpN8r1urFRqnmPqa/LpmmakUqlMTMzRczZDAwjg3Rag6Ko0LQ0kkkFsqLVq6qmoGrnKHQmy2q9Ui3RaIYsy4ZtaWkps3p/Hfv7O2dvX+9bR0eHVr5QsMycaeVyOWt5ednKmbPWwsKCZRiGpeu6lcnolklnOXPOymaz1uxs9mxubg5TU1MZmyRJ+rCcQljoJH5/ABzH0Rcl9MR7EAyG0dc3gG4xgoHBIVAt0ikNY6MjiHd3IxIR6nqe56iXRyAQ0G0N13t1LprE6Mw8eVjZwt21IrLzy9jeLeLVm6fYPqigUiljb28LsaFJtLVLcIUVVB6vUwrQzRU0s33EwQ/AGxBoIKPoLcI03LFpMpKrQBxeQWLsAcTRFbz/dooXX74jW3qFjz9OEUyYuHgtgQuOIeR3DrFx8AzzhU00uFXSxKpgOBrYyEh6a8cEWjvGCRPLoNGdhCuawUVHP+69+IT8uxP063nsnnxFSMrhimsQVzwS2PgMbkrzkKbXcNmVIHZfEte9IRroVXQ7PwI7nyYOYZKKZTSxGi65JOiFI9w/rMIXm0D+5Qd0qiaa3ENopPc17NwI/cgdNHgUctWnnge2MIJ+g4/BF+4nbOg2GH8cLj4Op09EV28KUsqAwxNFrzKJ9pgEJytSTTfV/OK8JzWfl6OBUUHQx8dUdArtROyKIhoJg/N5EfBz8LEeeNxMvWc9DJ174Od9f4MEAjxCIRq4uLKmP98vQ03cJLH4LYiiCJZlf60DX+fP/i/QtfGfB/qDYWOg/zY6IxES77lFhx3U6EdtJ/8B+sN2urdBw9bW1tZib26WGYZNOp1MsrXVWa//gVzL+gnLw52Ijo8yiwAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;devtools profiling&quot;
        title=&quot;&quot;
        src=&quot;/static/b190ce2199e739e9be97ef7604352739/e996b/devtools-profiling.png&quot;
        srcset=&quot;/static/b190ce2199e739e9be97ef7604352739/17741/devtools-profiling.png 263w,
/static/b190ce2199e739e9be97ef7604352739/52211/devtools-profiling.png 525w,
/static/b190ce2199e739e9be97ef7604352739/e996b/devtools-profiling.png 1050w,
/static/b190ce2199e739e9be97ef7604352739/087e3/devtools-profiling.png 1575w,
/static/b190ce2199e739e9be97ef7604352739/8079d/devtools-profiling.png 2100w,
/static/b190ce2199e739e9be97ef7604352739/de55f/devtools-profiling.png 3080w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;To understand the performance of your application and how it’s affected by your changes, you can use the profiling tools.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Keeping a web application responsive at any given moment, and especially performing a fast initial load, is critical for reaching an acceptable user experience.&lt;/p&gt;
&lt;p&gt;Ok that was it. Now you know why technologies like React exist. Now let’s see how Virtual DOM fixes those issues.&lt;/p&gt;
&lt;h2&gt;And the fun starts&lt;/h2&gt;
&lt;p&gt;Well, now you know.&lt;/p&gt;
&lt;p&gt;That’s why there is a need for UI libraries like React, Angular and Vue.js. These modern JS technologies provide solutions to write declarative code that looks and behaves like plain HTML, by managing DOM mutations in the background.&lt;/p&gt;
&lt;p&gt;For example, React utilizes plain &lt;code class=&quot;language-text&quot;&gt;render()&lt;/code&gt; functions that describe how the UI looks like, at any given &lt;em&gt;state&lt;/em&gt;. When the state changes, the function will be invoked again and its associated UI will be re-rendered.&lt;/p&gt;
&lt;p&gt;To automate this process, React keeps a clone of the DOM in memory. They call it &lt;strong&gt;Virtual DOM&lt;/strong&gt;. It’s way smaller than the actual DOM.&lt;/p&gt;
&lt;p&gt;React performs DOM mutations in memory, as an intermediate step. Then, it frequently updates the real DOM &lt;em&gt;only&lt;/em&gt; in the parts that have actually changed.&lt;/p&gt;
&lt;p&gt;To ensure this is performant enough (60fps), a pretty sophisticated “diffing” algorithm is used. The DOM updates are also being batched, to avoid expensive page &lt;em&gt;repaints&lt;/em&gt;, which are forcing the browser to re-evaluate the styles of the page for every single frame.&lt;/p&gt;
&lt;p&gt;Virtual DOM makes React extremely performant, while providing an incredible developer experience.&lt;/p&gt;
&lt;p&gt;This is not a React exclusive feature. &lt;a href=&quot;https://vuejs.org/v2/guide/reactivity.html&quot;&gt;Vue.js&lt;/a&gt; and &lt;a href=&quot;https://emberjs.com/&quot;&gt;Ember.js&lt;/a&gt; have their own implementations.&lt;/p&gt;
&lt;p&gt;Angular on the other side introduced a concept called &lt;a href=&quot;http://google.github.io/incremental-dom/#:~:text=Incremental%20DOM%20is%20a%20library,HTMLElement%20s%20and%20Text%20nodes.&quot;&gt;Incremental DOM.&lt;/a&gt; Compared to Virtual DOM, it has a low memory footprint and it is &lt;a href=&quot;https://en.wikipedia.org/wiki/Tree_shaking&quot;&gt;tree-shakeable&lt;/a&gt;. This makes it ideal for mobile application development. Although it’s considered slower, it can outperform the Virtual DOM in certain cases.&lt;/p&gt;
&lt;h2&gt;That’s all folks&lt;/h2&gt;
&lt;p&gt;We reviewed how DOM works under the hood and we explained why mutations are making a web application slow. Then we discovered how React handles DOM mutations with Virtual DOM.&lt;/p&gt;
&lt;p&gt;As a JavaScript developer who cares about your craft, you should be familiar with those topics. Learn the DOM api. Stop using your favorite framework or library for a while and focus on building an app with plain vanilla JS. This experiment will make you more confident using those tools.&lt;/p&gt;
&lt;p&gt;🏅 Achievement unlocked.&lt;/p&gt;
&lt;p&gt;Cover Credit: &lt;a href=&quot;https://unsplash.com/photos/567UmWZIYJ8&quot;&gt;@matthew_t_rader&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[The playlist that writes my code]]></title><description><![CDATA[Every month I curate the music that helps me concentrate, create and feel, in a music playlist series called "Cherry Pick".]]></description><link>http://www.nicotsou.com/music-for-creative-minds/</link><guid isPermaLink="false">http://www.nicotsou.com/music-for-creative-minds/</guid><pubDate>Tue, 20 Apr 2021 23:46:37 GMT</pubDate><content:encoded>&lt;p&gt;There are several people, including myself, who enjoy listening to music while working. If you also belong to this group, keep reading. I have something for you.&lt;/p&gt;
&lt;p&gt;Since music accompanies me for many hours per day, I get easily bored listening to the same tracks again and again. Radio is playing the same playlists every day. And you can’t escape from these tracks anyway. They play everywhere.&lt;/p&gt;
&lt;p&gt;Discovering good music is a difficult process. There are times streaming services suggest nice tracks, because the AI works as advertised. But most of the times the playlists they produce are monotonous and boring.&lt;/p&gt;
&lt;p&gt;In the last couple of years I collect the tracks that I like in Spotify. I curate songs that help me concentrate, that have something to say, that don’t distract you from working on your craft. New sounds from electronic, indie, soul, jazz and various other scenes.&lt;/p&gt;
&lt;p&gt;My series is called “Cherry pick” and I try to publish a new playlist every month, when time permits. Here’s a sneak preview of what to expect:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://open.spotify.com/playlist/0dgel6H5PCKuJqMc2KJBn5?si=62baa21159bd4d1c&quot;&gt;&lt;iframe src=&quot;https://open.spotify.com/embed/playlist/0dgel6H5PCKuJqMc2KJBn5&quot; width=&quot;100%&quot; height=&quot;400&quot; frameborder=&quot;0&quot; allowtransparency=&quot;true&quot; allow=&quot;encrypted-media&quot;&gt;&lt;/iframe&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Sounds interesting? Make sure you follow me on X so that you get notified when I release a new playlist.&lt;/p&gt;
&lt;p&gt;Do you have any good music to suggest? DM me the song or create your own public playlist. I would be really happy to listen to something new.&lt;/p&gt;
&lt;p&gt;Stay tuned for more. 🤗&lt;/p&gt;
&lt;p&gt;Cover Credit: &lt;a href=&quot;https://unsplash.com/photos/T3mKJXfdims&quot;&gt;@blocks&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Why I became a software engineer]]></title><description><![CDATA[Can you easily tell what your main driver was? Do you think it *still* drives you today or was there something you lost in the process?]]></description><link>http://www.nicotsou.com/why-I-became-a-software-engineer/</link><guid isPermaLink="false">http://www.nicotsou.com/why-I-became-a-software-engineer/</guid><pubDate>Sun, 21 Mar 2021 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;No, seriously now, why did you become a software engineer?&lt;/p&gt;
&lt;p&gt;Was it the beauty of the algorithms? The intelligence of the design patterns? The mysteriously blinking lights in server rooms? The revolution of technology and how it improves our lives? The happiness to see other people react to your creations? The agile manifest? The money they promised you? The feeling you helped the world become a better place? The need to have a job? The dirty hacks you invent to deal with something you don’t know? The fact you will never be without a job? The coffee you drink in your breaks? The dream to become the cockiest boss alive? Was it your mom?&lt;/p&gt;
&lt;p&gt;Can you easily tell what your main driver was? Do you think it &lt;em&gt;still&lt;/em&gt; drives you today or was there something you lost in the process?&lt;/p&gt;
&lt;p&gt;I have a story to share. It’s gonna take some time. I will let you prepare a beverage first. Today’s special hot chocolate with whipped cream. Ready? Ok, here we go…&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;My midlife crisis started earlier than usual. One day I woke up and all of a sudden various philosophical questions besieged my brain.&lt;/p&gt;
&lt;p&gt;Who am I? What am I good at? What I am &lt;em&gt;not&lt;/em&gt; good at? How can I contribute to this world and how far am I to make my dreams come true? Am I doing the right job? Am I gonna be successful? What is success anyway? Are we gonna be friends again?&lt;/p&gt;
&lt;p&gt;I felt like something was missing from my life. Something I wanted so badly and I didn’t know what. Nothing was giving me joy anymore. The same job. The same place. The same people. The same routine.&lt;/p&gt;
&lt;p&gt;In the beginning, I thought, it wasn’t really necessary to put myself in this trouble. I believed this wasn’t a healthy habit for me. I tried to escape these dark thoughts. Why do you have to feel bad when you are living like a king? You are a software engineer, you have a very nice job, you are working on high-scale projects and the list goes on. Why don’t you just post another selfie to tell the world how happy this burger makes you feel? You can find multiple ways to distract your brain. But can you escape these feelings?&lt;/p&gt;
&lt;p&gt;No, you can’t! The more you try to ignore a feeling, the stronger it gets. And you find yourself drowning in this endless feed, that never gives you any satisfaction anymore. I knew I had to &lt;em&gt;deal&lt;/em&gt; with it. I knew I had to &lt;em&gt;change&lt;/em&gt; something.&lt;/p&gt;
&lt;p&gt;So I did what everybody else does. I opened YouTube and I watched every self-improvement video ever created. You know this feeling when you are not motivated to do anything but you waste your time watching videos about motivation. We’ve all been there. It took me plenty of time to actually try the practices I was watching. And this is when the fun starts.&lt;/p&gt;
&lt;p&gt;I want to share with you what worked best for me. It’s an easy exercise you &lt;em&gt;can&lt;/em&gt; try at home. Spoiler alert: It turned out these thoughts I had from the very beginning were really healthy. I feel very thankful I had to go through all this. In fact, this blog would never be created otherwise.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/faea17746449a6e7215bb79ad0c28a38/857b3/alec-favale-empty-metro.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAgABBf/EABUBAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAHj6UgjH//EABoQAAICAwAAAAAAAAAAAAAAAAABAhIhMUH/2gAIAQEAAQUCwyqGR3w//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAGBAAAgMAAAAAAAAAAAAAAAAAACEgMVH/2gAIAQEABj8CwsSh/8QAHBABAAICAwEAAAAAAAAAAAAAAREhABBRcYGh/9oACAEBAAE/IQ4X8cSv3WQWkNCyu9f/2gAMAwEAAgADAAAAEODf/8QAFhEBAQEAAAAAAAAAAAAAAAAAARAh/9oACAEDAQE/EF2f/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPxA//8QAHRABAAICAgMAAAAAAAAAAAAAAQARITFBUWHR4f/aAAgBAQABPxCrLEZc+wYbaoaNFeQ+1MsAFVdr5Xua3rMcTzaLP//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Empty metro station&quot;
        title=&quot;&quot;
        src=&quot;/static/faea17746449a6e7215bb79ad0c28a38/9ecec/alec-favale-empty-metro.jpg&quot;
        srcset=&quot;/static/faea17746449a6e7215bb79ad0c28a38/5d001/alec-favale-empty-metro.jpg 263w,
/static/faea17746449a6e7215bb79ad0c28a38/7349d/alec-favale-empty-metro.jpg 525w,
/static/faea17746449a6e7215bb79ad0c28a38/9ecec/alec-favale-empty-metro.jpg 1050w,
/static/faea17746449a6e7215bb79ad0c28a38/39d31/alec-favale-empty-metro.jpg 1575w,
/static/faea17746449a6e7215bb79ad0c28a38/1f368/alec-favale-empty-metro.jpg 2100w,
/static/faea17746449a6e7215bb79ad0c28a38/857b3/alec-favale-empty-metro.jpg 6000w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/photos/nGJboLqJx3M&quot;&gt;Alec Favale&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Another day was about to end. I was home. Everybody was. The city was empty because of the pandemic. I had the windows closed because I was afraid of the zombies. Better to be prepared for anything. You can’t play games with 2020. I was bored and lost in self-improvement blogs. Suddenly I read about this mind-blowing idea.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I opened a portal through time and I asked my young self how he feels watching me in this moment of my life. Was he happy with my progress so far? Was this the future he was dreaming about?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It worked! I was able to see my teenage self. He was studying math in my green-painted room. My first thought was pretty obvious. Damn my hair needed an extreme makeover. Later on, I started talking to him.&lt;/p&gt;
&lt;p&gt;At first, he was scared. Not because I was able to communicate with him in the future, he was actually thrilled this is possible. He was shocked as I was giving him an update about his future. I will never forget the words he used to describe it.&lt;/p&gt;
&lt;p&gt;“Man what the f*ck are you doing? Are you f*cking crazy?”&lt;/p&gt;
&lt;p&gt;The guy had a point, so I let him speak. I listened very carefully to the things he had to say. In order to transfer his message, I have to go back to where all this started.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;em&gt;“Oh great, I know where this is going. He will write the story of his life again.”&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“It’s a personal blog, of course, I have to write about my life, what else should I write about?”&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“Please make it quick. It’s hard to keep my eyes open with all this drama.”&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“I will if you let me write. I thought I’m done with you when I stopped writing my previous blog. How the hell you have survived all these years?”&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“I thought you’d stopped writing blogs that nobody is willing to read, but I lost my faith the moment you started opening portals and talking to yourself.”&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“Ok mister, you got it. I will write the story of my whole life with all the unnecessary details I can think of. I will use as many words as I can. And you know what, you can’t escape reading it, because you are a part of me. In fact, you will read it multiple times until I am happy publishing it.”&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;”…”&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;“Yeah, remember this day the next time you will think to interrupt me again.”&lt;/em&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/5a46c7578a2c1a70193e7406d5592e8d/b82b3/jd-x-crt-monitor.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 66.53992395437263%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAAMBAgQF/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAH/2gAMAwEAAhADEAAAAeTfOyxZAf/EABoQAAEFAQAAAAAAAAAAAAAAABEAAQIQEhP/2gAIAQEAAQUCYDN9JZX/xAAVEQEBAAAAAAAAAAAAAAAAAAAQEf/aAAgBAwEBPwGn/8QAFREBAQAAAAAAAAAAAAAAAAAAEBH/2gAIAQIBAT8Bh//EABoQAAICAwAAAAAAAAAAAAAAABASAAERIVH/2gAIAQEABj8CmdqVa14P/8QAGRABAAMBAQAAAAAAAAAAAAAAAQARITFx/9oACAEBAAE/IViwKDNNY9lymv0MnZ//2gAMAwEAAgADAAAAEETv/8QAFhEBAQEAAAAAAAAAAAAAAAAAAREQ/9oACAEDAQE/EFQmf//EABcRAQADAAAAAAAAAAAAAAAAAAEQESH/2gAIAQIBAT8QBex//8QAGhABAAIDAQAAAAAAAAAAAAAAAQARMUFRIf/aAAgBAQABPxCqNsv07L2KKTBuCkcYU0QrA4ra8RVK5Z//2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;CRT Monitor&quot;
        title=&quot;&quot;
        src=&quot;/static/5a46c7578a2c1a70193e7406d5592e8d/9ecec/jd-x-crt-monitor.jpg&quot;
        srcset=&quot;/static/5a46c7578a2c1a70193e7406d5592e8d/5d001/jd-x-crt-monitor.jpg 263w,
/static/5a46c7578a2c1a70193e7406d5592e8d/7349d/jd-x-crt-monitor.jpg 525w,
/static/5a46c7578a2c1a70193e7406d5592e8d/9ecec/jd-x-crt-monitor.jpg 1050w,
/static/5a46c7578a2c1a70193e7406d5592e8d/39d31/jd-x-crt-monitor.jpg 1575w,
/static/5a46c7578a2c1a70193e7406d5592e8d/1f368/jd-x-crt-monitor.jpg 2100w,
/static/5a46c7578a2c1a70193e7406d5592e8d/b82b3/jd-x-crt-monitor.jpg 8256w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/photos/pj1aVH2tiLE&quot;&gt;JD X&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;When I was a kid, I had an obsession with electronic devices and especially those that had screens. SONY Trinitron TVs, VHS, calculators, CRT monitors, LCD panels and retina displays.&lt;/p&gt;
&lt;p&gt;I was observing UI interfaces in every possible format. When everybody was watching a trailer for a movie, I was checking the motion graphics. When everyone was refusing to switch from MS DOS to Windows, I was extremely excited with the user interface and the endless possibilities that came along with it. When everyone was bored waiting for a game to load on PlayStation, my mind was running on how they made this loading screen look so nice. It was just so cool!&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 922px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/a6d36cc2630b759a73f359e3da1a1d30/6da96/Macintosh.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 53.61216730038023%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsTAAALEwEAmpwYAAACBUlEQVR42o2S72tSURjHr6u/KKgIGr3qx4te1CqK2o9WwYJqtUhnXNztNiyVnLlMC8xl997pvYWDsS1XW5Y2c5um7hbRhsoolzVwRUWNDny7emcIY8sHHs6L830+5/t9OBS1UgClUU/UFd9Kk0szPiymuF9Lsv93URZ/FmX/D6W/Laal5Xzc90LRqXpKnVtVVUDNxzg3HRTNMBnPkovnD6Cj/SCuGk6i23CKcM5OfJgSEv/m1gRWXeQTXCzw0IYW9jrZoXNi6zkTjrK9OEZbCGPU4nPSH602sVZpVpr68ubRhIFuxN5LNNl9L4P6ngj2ddnQxpqI3dmtAMWwCmTq1gNSlb0UZsQwrW/CLp2R7B/4jj38AnZqFZeXreSWy4zCa2G8pJMkacO6QIZRX8wnHoR02mZsamgg9ac7sbmlHVuONGJ7q5aYe2h8SgojZQP/A1YiZGOeMTnsxJDURYYEPQY5PYZFFqMBM5mbvI/5aT5QE7AS4V3ENbqQ8mJ24i6Zi7qRm+rDfNyrgPpIIe1D5pW3vzaHFWD4znB63IYRnv4T5GnSf/sCERwdpV6ODlqQjXk9Ne0wFGI2ls7UY8vA++e9SI7ZSerpTTwpxfWxCPJXiPzMgdmI215j5ENlQZDXH+YdbXCZm786rzXl3DeOZz3WExmPtTUrOs7kXkrstjKQWf1t/gIjnUTjJYsHQQAAAABJRU5ErkJggg==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Macintosh&quot;
        title=&quot;&quot;
        src=&quot;/static/a6d36cc2630b759a73f359e3da1a1d30/6da96/Macintosh.png&quot;
        srcset=&quot;/static/a6d36cc2630b759a73f359e3da1a1d30/17741/Macintosh.png 263w,
/static/a6d36cc2630b759a73f359e3da1a1d30/52211/Macintosh.png 525w,
/static/a6d36cc2630b759a73f359e3da1a1d30/6da96/Macintosh.png 922w&quot;
        sizes=&quot;(max-width: 922px) 100vw, 922px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;School years were lazy. All I wanted to do was to go back home and turn on my PC. I loved video games. I wanted to play every video game ever created. It was fascinating to watch technology evolve so rapidly. From text-based adventures to Myst and Syberia. From green console terminals to 3dfx. Video games were pushing metal to the maximum. Plus, it was really a pleasure, to save the universe with your friends.&lt;/p&gt;
&lt;p&gt;I had an interesting hobby back then. I used to buy textbooks, flip their cover and create albums by cutting off pictures from magazines. Later on I started making my own fake computer magazines. Yes, as an only child, I did have plenty of time to spend. Other kids had imaginary friends, I had imaginary magazines!&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/f0b03ef8643f7390eeebd3387002e9b2/29114/Magazines.png&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 41.82509505703422%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAACVUlEQVR42n2RPUwTARTHD4whGgk6uBiNg4Obi7MOYuJmNISEqkBRvARLxWprwEqL5WqhKS0ttKXljn5QQO44+v154FnLncWStCgYo0FFsSrOxAjVp1RNOvlL/nnDe/kN748AQFlJypHfqEli//yQkf3osadfOyxPGYc9EeK4+nT2+W2GZSVsmpctvViSsCS5RywWV/SM0gflExOHbAxZhZSyLd2eeK/swIrx1saq5hp8VjXB4j30y6Q/EgsGgtCFaQuuqTA4QjObDLNQhbkdNTdcts0OYgAwm1WDDIyMNBpczihBUw/GxsfIIcLhU2u6May5YX1aIgJcWA/m1qZ8j9UelcsVYFBhBZVUDqLrNzdb2tv3OUPTAk8iAsG5JEzFwnpEYbYZtC4SUpnMVuoJ/yO3vAweioq2Nwvz4xfPQ+BKDfTfleQ7zXhQr8Vg2tC16qb9a73KO2+OI8huOuKryyzm4NXb97CwmO1D9G63rtPqghSf/hZNxL/HZlmg/d6ISS5a1wkugaJFCFKxaMOsVh4LefDDkz76PhWL9Dvdwwbr8GAr7nFKQyybiycfvwtEQzLEQnn7RuMsJDluiw6HC37mIQwSw16TqN5y9ayAFp6pptrqznna2hr3/gSo4LLZr+lnS2BzuguEcwSCDNNQ/DuOV6IouhPRGo1HxmnylMliOnEZRU926nTVMlXv0b89lZWW5Zubq3y59mF9Jf8J1HpzQdzRDY/4+QvbO5KEHcj/UCqV5f9SW1tbPOZ5fhefy/pTCxkukkiwYWYmzfL86T9Csij8BQ97W9ehsyb/AAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Old school magazines&quot;
        title=&quot;&quot;
        src=&quot;/static/f0b03ef8643f7390eeebd3387002e9b2/e996b/Magazines.png&quot;
        srcset=&quot;/static/f0b03ef8643f7390eeebd3387002e9b2/17741/Magazines.png 263w,
/static/f0b03ef8643f7390eeebd3387002e9b2/52211/Magazines.png 525w,
/static/f0b03ef8643f7390eeebd3387002e9b2/e996b/Magazines.png 1050w,
/static/f0b03ef8643f7390eeebd3387002e9b2/087e3/Magazines.png 1575w,
/static/f0b03ef8643f7390eeebd3387002e9b2/29114/Magazines.png 1920w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;And then the web arrives and changes everything.&lt;/p&gt;
&lt;p&gt;It was very slow to connect and files were taking hours to download. Images were considered a luxury. Platforms like Blogspot and WordPress were getting popular. It was easy for someone without any technical skills, like myself, to create their precious website.&lt;/p&gt;
&lt;p&gt;I started a personal blog about video games and technology using Yahoo! Geocities. For the first time, someone could &lt;em&gt;read&lt;/em&gt; my articles. It wasn’t a successful one, depending on the traffic, but it helped me to expose myself to many activities.&lt;/p&gt;
&lt;p&gt;I had to play many roles. Coding, storytelling, video editing, graphic design and the list goes on. I was constantly searching for ways to improve my craft. Reading people’s comments and adapting my work according to their feedback. I learned programming by myself before I started my IT studies. Or at least something I was calling programming.&lt;/p&gt;
&lt;p&gt;I also made some good friends in the process. People I never met in person until today. We had a lot of interests in common. You see, I couldn’t find schoolmates with the same curiosity about computers. And vice versa. It was strange to me that nobody was excited as I was.&lt;/p&gt;
&lt;p&gt;But I was still captivated by user interfaces. This time on the web. Something inside me was making that &lt;em&gt;click&lt;/em&gt; again.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It’s those pixels you can’t take your eyes off. All these little details that fit together perfectly.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It’s like a living piece of art. You start with an empty canvas. You combine different elements together to construct a composition. You choose the tools, and the language and basically you make decisions about every single element. You deal with the technical limitations and any other problems you may face along the way. You try to keep it modern and easy to navigate.&lt;/p&gt;
&lt;p&gt;I can’t explain what was keeping me up endless hours working on my blog, without getting anything in return. I didn’t care about money or fame. I didn’t care how many visitors I will reach. It was just my creative space. I was feeling good. I was myself.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/28ca15e180dc44a2f5189b3f827f67c8/857b3/florian-krumm-illuminated-keyboard.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 60.07604562737643%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAMABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAgABBf/EABUBAQEAAAAAAAAAAAAAAAAAAAEC/9oADAMBAAIQAxAAAAHkYAyoxX//xAAaEAABBQEAAAAAAAAAAAAAAAAAARAREjFB/9oACAEBAAEFAsNbllJP/8QAFREBAQAAAAAAAAAAAAAAAAAAARD/2gAIAQMBAT8BCf/EABURAQEAAAAAAAAAAAAAAAAAABAR/9oACAECAQE/Aaf/xAAUEAEAAAAAAAAAAAAAAAAAAAAg/9oACAEBAAY/Al//xAAcEAEAAgIDAQAAAAAAAAAAAAABABEhQTFRgZH/2gAIAQEAAT8hcitnUeDinbPZaF2MUbufU//aAAwDAQACAAMAAAAQ2B//xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAgBAwEBPxCCP//EABYRAQEBAAAAAAAAAAAAAAAAAAABEf/aAAgBAgEBPxCmv//EAB0QAQEAAgIDAQAAAAAAAAAAAAERAEEhYTFRcYH/2gAIAQEAAT8QFUygMDpMujQPB8e+8YKcenJO6D+YhcK1hvBc0F7c/9k=&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Keyboard illuminated&quot;
        title=&quot;&quot;
        src=&quot;/static/28ca15e180dc44a2f5189b3f827f67c8/9ecec/florian-krumm-illuminated-keyboard.jpg&quot;
        srcset=&quot;/static/28ca15e180dc44a2f5189b3f827f67c8/5d001/florian-krumm-illuminated-keyboard.jpg 263w,
/static/28ca15e180dc44a2f5189b3f827f67c8/7349d/florian-krumm-illuminated-keyboard.jpg 525w,
/static/28ca15e180dc44a2f5189b3f827f67c8/9ecec/florian-krumm-illuminated-keyboard.jpg 1050w,
/static/28ca15e180dc44a2f5189b3f827f67c8/39d31/florian-krumm-illuminated-keyboard.jpg 1575w,
/static/28ca15e180dc44a2f5189b3f827f67c8/1f368/florian-krumm-illuminated-keyboard.jpg 2100w,
/static/28ca15e180dc44a2f5189b3f827f67c8/857b3/florian-krumm-illuminated-keyboard.jpg 6000w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;
&lt;em&gt;Credit: &lt;a href=&quot;https://unsplash.com/photos/1osIUArK5oA&quot;&gt;Florian Krumm&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;My career as a software developer was a nice continuation of this story. It combines many of the things I like to do. It’s not only a job to me, it’s my hobby too. And I was lucky. Very lucky. It proved to be one of the best in-demand jobs.&lt;/p&gt;
&lt;p&gt;But trust me, it wasn’t easy to stay in this path. I made plenty of risky choices in my life to retain my ambition. You see, back then IT wasn’t the hottest place to work. Computers were neither cool nor popular as today. Today if you are a software engineer it’s considered kinda cool, back then it was the hobby of the geeks.&lt;/p&gt;
&lt;p&gt;This desire to make my hobby a profession forced me to take risky decisions in my life. I refused to work for our family business. I resigned from jobs in profitable companies, for positions that were closer to what I wanted to work on. There was a period I was working 12 hours a day, coding personal projects right after my regular job. For almost two years I was working (almost) without getting paid. But I didn’t care back then.&lt;/p&gt;
&lt;p&gt;I wanted to earn some experience. I knew this would be the best deal for me. I would never have to work anymore.&lt;/p&gt;
&lt;p&gt;Going for what you love is the best decision you will ever make in your life. It pays &lt;em&gt;a lot&lt;/em&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Now I know what was missing from my life. The reason I was not satisfied, even though I gained a lot. I lost my creativity.&lt;/p&gt;
&lt;p&gt;It took me 3 years to build this website. I had already the design in Sketch and it was just a matter of a weekend or two to write the code. Every time I was pushing myself to work on it, something was distracting me. I tried different technologies, different ways to organize myself, different hardware, and different writing styles. But I never managed to publish it. In the end, I was always giving up. I wasn’t satisfied with anything I was producing.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It wasn’t a project a kid was making with &lt;em&gt;love&lt;/em&gt;, persuading to become a successful developer. It was a project a “successful” developer was making with &lt;em&gt;fear&lt;/em&gt;, persuading not to be perceived as a kid. And guess what. He was constantly failing.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Now let’s take some lessons from my young self.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Find what you love to do. Put your heart and soul into it. Make it your profession. You need a job that embodies who you are. Expose yourself to new ideas, learn new things, meet people who share the same passion, and take more risks.&lt;/p&gt;
&lt;p&gt;If you haven’t found yet what drives you, I have a tip for you. Take an empty piece of paper. Write down what drives you, what are you curious about. What makes you lose the sense of time? What are the tasks you are willing to do that everybody else doesn’t?&lt;/p&gt;
&lt;p&gt;Now you have to find the courage and the time to work on it.&lt;/p&gt;
&lt;p&gt;Start simple. Schedule the time that you’ll spend working on your craft, without any interruptions. No internet, no phones, no messages and no people around you. One hour at a time is enough. Don’t set any goals. Just let yourself free.&lt;/p&gt;
&lt;p&gt;You will see your motivation will increase as time goes on. You will find more interesting things to work on. You will meet inspiring people and at the same time, you will inspire others. You will leave your comfort zone.&lt;/p&gt;
&lt;p&gt;Working on what you love will give you the courage to carry on. You will be confident to take risks you weren’t able to take before. To quit this boring job. To start a business. To produce something good. To be you.&lt;/p&gt;
&lt;p&gt;Scary isn’t it? Just give it a try. You don’t have anything to lose.&lt;/p&gt;
&lt;p&gt;The opposite of love is fear, not hate.&lt;/p&gt;
&lt;p&gt;Cover Credit: &lt;a href=&quot;https://unsplash.com/photos/9l_326FISzk&quot;&gt;Luca Bravo&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title><![CDATA[TLTR; Declarative UI]]></title><description><![CDATA[This is a custom description for SEO and Open Graph purposes, rather than the default generated excerpt. Simply add a description field to the frontmatter.]]></description><link>http://www.nicotsou.com/tltr-declarative-ui/</link><guid isPermaLink="false">http://www.nicotsou.com/tltr-declarative-ui/</guid><pubDate>Sat, 20 Mar 2021 23:46:37 GMT</pubDate><content:encoded>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Declarative programming&lt;/strong&gt; makes your code more concise, easier to read and reason about and reduces side effects.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;JavaScript has function scope, meaning that any variable you declare inside a function is not accessible outside this function:&lt;/p&gt;
&lt;p&gt;Procedural and object-oriented are forms of &lt;strong&gt;imperative programming&lt;/strong&gt;, which implement algorithms in explicit steps. React embraces functional programming which is a form of declarative programming.&lt;/p&gt;
&lt;p&gt;You describe &lt;em&gt;what&lt;/em&gt; the program must accomplish for solving a problem, rather than describing &lt;em&gt;how&lt;/em&gt; to accomplish it.&lt;/p&gt;
&lt;p&gt;Practical tips for writing declarative code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Avoid loops, use &lt;a href=&quot;https://javascript.info/array-methods&quot;&gt;array methods&lt;/a&gt; instead&lt;/li&gt;
&lt;li&gt;Avoid variables, try to compose functions instead&lt;/li&gt;
&lt;li&gt;Avoid mutating state, create new instances instead&lt;/li&gt;
&lt;li&gt;Avoid side effects, and ensure your functions are pure&lt;/li&gt;
&lt;li&gt;Work with the state, don’t manipulate DOM directly&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Programming declarative UI is difficult. It requires abstract DOM updates because touching the DOM is a side-effect.&lt;/p&gt;
&lt;p&gt;React is a declarative way of writing UIs. It gives you all the tools to write your markup in plain JavaScript functions. You care only about the application state and how this changes when certain events occur. Then it updates the DOM for you in an easy and predictable way.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;When you start your programming studies the first thing they teach you is variables, logical expressions and loops. Then you begin with procedural and then object-oriented programming. In other words, you learn how to imperatively write code.&lt;/p&gt;
&lt;p&gt;This is not necessarily a bad thing. You must get familiar with how computers understand our world. In fact, declarative approaches have some sort of imperative abstraction layer underneath.&lt;/p&gt;
&lt;p&gt;Once you start building commercial applications though things get more complex. Simplicity is king. It is essential for every developer in a team to follow the code and reason about. Our goal is to leave these machine instructions behind as much as we can and focus on writing code more perceivable by humans.&lt;/p&gt;
&lt;p&gt;In this article, I will explain what declarative programming is and how it applies to web applications. We will see some real-life examples to help you recognize the difference between imperative.&lt;/p&gt;
&lt;p&gt;Learning how to write declarative code completely changed my mind about programming. I hope the techniques in this article will help you as much as they helped me.&lt;/p&gt;
&lt;p&gt;Excited? So grab a cup of ☕ coffee and come back. I will be waiting.&lt;/p&gt;
&lt;h2&gt;Humans think declaratively&lt;/h2&gt;
&lt;p&gt;At the beginning of this chapter, I gave you the following instruction:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&gt; Grab a cup of coffee and come back&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is a declarative approach.&lt;/p&gt;
&lt;p&gt;The imperative approach would be something like this:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;text&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-text line-numbers&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;&gt; Go to the kitchen
&gt; Turn on the coffee machine
&gt; Create an instance of a cup
&gt; Put the cup under the coffee machine
&gt; Press the Espresso button
&gt; Wait for the process to finish
&gt; Turn off the machine
&gt; Take the cup of coffee
&gt; Come back to this screen&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We can put this algorithm to a &lt;code class=&quot;language-text&quot;&gt;makeCoffee()&lt;/code&gt; function or create a fancy &lt;code class=&quot;language-text&quot;&gt;Coffee.makeCoffee()&lt;/code&gt; method, but the result will be the same. My life would be much easier if I just told you &lt;em&gt;what&lt;/em&gt; to do instead of instructing you in the steps above.&lt;/p&gt;
&lt;p&gt;Now let’s have a look at how this translates to coding.&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/c8a091633ae71642e2be1fdff05b4b7c/8b619/laptop.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 79.84790874524715%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAQABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAQFAv/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAZ21XRwnB//EABgQAQEBAQEAAAAAAAAAAAAAAAIDAQAS/9oACAEBAAEFAlTUvPGGo9G2BOza/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPwE//8QAHxAAAQMDBQAAAAAAAAAAAAAAEQABAhAhIjFBYXGB/9oACAEBAAY/AjJX0RkPaZRLbcIl26X/xAAdEAACAgIDAQAAAAAAAAAAAAAAARExIUFRYXGR/9oACAEBAAE/IdRNLgTRN2V2KCZ6hlImSjR9B98xqP/aAAwDAQACAAMAAAAQow//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/ED//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/ED//xAAdEAEAAgICAwAAAAAAAAAAAAABABEhMUFhcYGx/9oACAEBAAE/ECwiMDodRxKgAqkcepYJDB3gHXiKVuYeaepG3ovj5uOgrpoCf//Z&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;Coding in a laptop&quot;
        title=&quot;&quot;
        src=&quot;/static/c8a091633ae71642e2be1fdff05b4b7c/9ecec/laptop.jpg&quot;
        srcset=&quot;/static/c8a091633ae71642e2be1fdff05b4b7c/5d001/laptop.jpg 263w,
/static/c8a091633ae71642e2be1fdff05b4b7c/7349d/laptop.jpg 525w,
/static/c8a091633ae71642e2be1fdff05b4b7c/9ecec/laptop.jpg 1050w,
/static/c8a091633ae71642e2be1fdff05b4b7c/39d31/laptop.jpg 1575w,
/static/c8a091633ae71642e2be1fdff05b4b7c/1f368/laptop.jpg 2100w,
/static/c8a091633ae71642e2be1fdff05b4b7c/8b619/laptop.jpg 5000w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;h2&gt;A declarative code example&lt;/h2&gt;
&lt;p&gt;We have the following array of months:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-javascript line-numbers&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; months &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;January&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;February&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&apos;March&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let’s imagine that we have to create a calendar similar to the one we find in iOS:&lt;/p&gt;
&lt;p&gt;&lt;span
      class=&quot;gatsby-resp-image-wrapper&quot;
      style=&quot;position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1050px; &quot;
    &gt;
      &lt;a
    class=&quot;gatsby-resp-image-link&quot;
    href=&quot;/static/230c4bd059b99da242fadea841545034/067bb/iOS-Calendar-iPad-dark-mode.jpg&quot;
    style=&quot;display: block&quot;
    target=&quot;_blank&quot;
    rel=&quot;noopener&quot;
  &gt;
    &lt;span
    class=&quot;gatsby-resp-image-background-image&quot;
    style=&quot;padding-bottom: 74.90494296577947%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAPABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAAECBf/EABQBAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhADEAAAAeClDI//xAAWEAEBAQAAAAAAAAAAAAAAAAAAESD/2gAIAQEAAQUCVcf/xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAEDAQE/AT//xAAUEQEAAAAAAAAAAAAAAAAAAAAQ/9oACAECAQE/AT//xAAUEAEAAAAAAAAAAAAAAAAAAAAg/9oACAEBAAY/Al//xAAaEAACAgMAAAAAAAAAAAAAAAAAETFRASFh/9oACAEBAAE/IX0ax7K2QZk//9oADAMBAAIAAwAAABDwz//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8QP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8QP//EAB0QAQADAAEFAAAAAAAAAAAAAAEAESExQVFxkbH/2gAIAQEAAT8Qu0uvEUKVz2iLjqDg+IiNevuK+Vz/2Q==&apos;); background-size: cover; display: block;&quot;
  &gt;&lt;/span&gt;
  &lt;img
        class=&quot;gatsby-resp-image-image&quot;
        alt=&quot;DevTools Profiler&quot;
        title=&quot;&quot;
        src=&quot;/static/230c4bd059b99da242fadea841545034/9ecec/iOS-Calendar-iPad-dark-mode.jpg&quot;
        srcset=&quot;/static/230c4bd059b99da242fadea841545034/5d001/iOS-Calendar-iPad-dark-mode.jpg 263w,
/static/230c4bd059b99da242fadea841545034/7349d/iOS-Calendar-iPad-dark-mode.jpg 525w,
/static/230c4bd059b99da242fadea841545034/9ecec/iOS-Calendar-iPad-dark-mode.jpg 1050w,
/static/230c4bd059b99da242fadea841545034/39d31/iOS-Calendar-iPad-dark-mode.jpg 1575w,
/static/230c4bd059b99da242fadea841545034/1f368/iOS-Calendar-iPad-dark-mode.jpg 2100w,
/static/230c4bd059b99da242fadea841545034/067bb/iOS-Calendar-iPad-dark-mode.jpg 2732w&quot;
        sizes=&quot;(max-width: 1050px) 100vw, 1050px&quot;
        style=&quot;width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;&quot;
        loading=&quot;lazy&quot;
        decoding=&quot;async&quot;
      /&gt;
  &lt;/a&gt;
    &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Our first step is to modify our array to have only the first three letters of the month’s name.&lt;/p&gt;
&lt;p&gt;Here’s a comparison between the two approaches:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-javascript line-numbers&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Imperative code&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getMonths&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;months&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;let&lt;/span&gt; shortMonths &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; months&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;length&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; i&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    shortMonths&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;months&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; shortMonths
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Declarative code&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;getMonths&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;months&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; months&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;month&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; month&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this second approach, we leverage the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;map()&lt;/code&gt;&lt;/a&gt; array method and thus we avoid looping over the array items.&lt;/p&gt;
&lt;p&gt;We avoid unnecessary variable declarations, which means less code to work with and this makes the overall approach easier to read.&lt;/p&gt;
&lt;p&gt;We avoid touching the DOM, which means our function has no &lt;a href=&quot;https://en.wikipedia.org/wiki/Side_effect_(computer_science)&quot;&gt;side-effects&lt;/a&gt;. It is also &lt;a href=&quot;https://en.wikipedia.org/wiki/Pure_function&quot;&gt;pure&lt;/a&gt;, with the same input it will always return the same output.&lt;/p&gt;
&lt;p&gt;Finally, we avoid using the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push&quot;&gt;&lt;code class=&quot;language-text&quot;&gt;array.push()&lt;/code&gt;&lt;/a&gt; method which mutates our array.&lt;/p&gt;
&lt;p&gt;It seems we’ve accomplished quite a lot in such a simple task, right? Imagine how easier our programs would be once we start applying these simple practices as much as we can.&lt;/p&gt;
&lt;p&gt;Now let’s see how this affects the UI.&lt;/p&gt;
&lt;h2&gt;Declarative UIs&lt;/h2&gt;
&lt;p&gt;HTML is &lt;a href=&quot;https://www.youtube.com/watch?v=4A2mWqLUpzw&quot;&gt;declarative&lt;/a&gt; by design. Consider the following markup:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;html&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-html line-numbers&quot;&gt;&lt;code class=&quot;language-html&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;ul&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token attr-value&quot;&gt;&lt;span class=&quot;token punctuation attr-equals&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;months&lt;span class=&quot;token punctuation&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;button&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;01&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;button&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;button&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;02&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;button&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;button&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;03&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;button&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;li&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
  ...
&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;ul&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You don’t need to explicitly describe step by step how the browser will be rendering this list on the screen.&lt;/p&gt;
&lt;p&gt;CSS works similarly, the style of the class &lt;code class=&quot;language-text&quot;&gt;.months&lt;/code&gt; will be applied automatically to all the matching DOM elements which have this class.&lt;/p&gt;
&lt;p&gt;The problem starts when you add interactivity.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/001fd08e3ceb051942b6ea58ae8c73aa/airbnb-calendar.gif&quot; alt=&quot;Airbnb calendar&quot;&gt;
&lt;em&gt;The Airbnb calendar allows you to select a date period&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Consider this Airbnb calendar. Two things happen when you click on a day. At first, we indicate the selected days with a black circle. Also the check in/out field gets updated with the new date range.&lt;/p&gt;
&lt;p&gt;Below is an example of this in modern JavaScript. For the sake of simplicity, we use the class &lt;code class=&quot;language-text&quot;&gt;.selected&lt;/code&gt; to indicate one selected day at a time.&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-javascript line-numbers&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Calendar&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token function&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;domId&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;renderCalendar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;domId&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gatsby-highlight-code-line&quot;&gt;    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;addEventListeners&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;domId&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;handleCalendarClick&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;selectedDay&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    event&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;preventDefault&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;updateView&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;selectedDay&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;updateInput&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;selectedDay&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;addEventListeners&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;domId&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    document
      &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;domId&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;querySelectorAll&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;li button&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; selectedDay &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; button&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;day
        button&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;addEventListener&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;click&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
          &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;handleCalendarClick&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;selectedDay&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;updateView&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;selectedDay&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; selector &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;button[data-day=&quot;&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;${&lt;/span&gt;selectedDay&lt;span class=&quot;token interpolation-punctuation punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;]&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;
    document&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;querySelector&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;classList&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;toggle&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;selected&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;updateInput&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;selectedDay&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    document&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;selectedDate&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;value &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; selectedDay
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;token function&quot;&gt;renderCalendar&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token parameter&quot;&gt;domId&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    document&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;domId&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;innerHTML &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token template-string&quot;&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;			&amp;lt;input id=&quot;selectedDate&quot; /&gt;&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;				&amp;lt;ul&gt;&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;					&amp;lt;li&gt;&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;						&amp;lt;button data-day=&quot;01&quot;&gt;01&amp;lt;/button&gt;&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;					&amp;lt;/li&gt;&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;					&amp;lt;li&gt;&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;						&amp;lt;button data-day=&quot;02&quot;&gt;02&amp;lt;/button&gt;&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;					&amp;lt;/li&gt;&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;					...&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;				&amp;lt;/ul&gt;&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;			&lt;/span&gt;&lt;span class=&quot;token template-punctuation string&quot;&gt;`&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;Try this example by yourself in &lt;a href=&quot;https://jsfiddle.net/vd6gqhco/90/&quot;&gt;JSFiddle&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;As you can see this approach is imperative:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A big part of the &lt;code class=&quot;language-text&quot;&gt;Calendar&lt;/code&gt; class explicitly modifies DOM elements.&lt;/li&gt;
&lt;li&gt;We depend on the &lt;code class=&quot;language-text&quot;&gt;data-day&lt;/code&gt; attribute to get the selected button&lt;/li&gt;
&lt;li&gt;We no longer give the browser &lt;em&gt;what&lt;/em&gt; to render, we instructively tell the browser &lt;em&gt;how&lt;/em&gt; to modify the DOM.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What if we could write our application like this:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SearchFilters&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;DateRangeInput&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;dateRange&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Calendar&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;dateRange&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token attr-name&quot;&gt;onChange&lt;/span&gt;&lt;span class=&quot;token script language-javascript&quot;&gt;&lt;span class=&quot;token script-punctuation punctuation&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;setDateRange&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;/&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token plain-text&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token tag&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;SearchFields&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Here’s how it works:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;Calendar&gt;&lt;/code&gt; displays a calendar and uses &lt;code class=&quot;language-text&quot;&gt;dateRange&lt;/code&gt; to mark the selected days. It also allows the user to modify their selection. This happens by calling the &lt;code class=&quot;language-text&quot;&gt;onChange()&lt;/code&gt; function with the updated &lt;code class=&quot;language-text&quot;&gt;dateRange&lt;/code&gt; value.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;DateRangeInput&gt;&lt;/code&gt; is a special kind of input that displays the selected date range. Similar to the calendar component it can also modify the selection as the user types.&lt;/li&gt;
&lt;li&gt;&lt;code class=&quot;language-text&quot;&gt;&amp;lt;SearchFilters&gt;&lt;/code&gt; is the container div that applies the layout.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Modern UI libraries let you do exactly&lt;/p&gt;
&lt;h2&gt;React&lt;/h2&gt;
&lt;p&gt;React went popular because it lets you work with the state of your UI and it abstracts all DOM updates in a fast and predictable way. In fact, the development experience is so good that people like to write CSS in JS as well.&lt;/p&gt;
&lt;p&gt;Think of it as a simple function:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;jsx&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-jsx line-numbers&quot;&gt;&lt;code class=&quot;language-jsx&quot;&gt;&lt;span class=&quot;token function&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;state&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You are responsible to describe how your UI is being rendered to the DOM by a given &lt;code class=&quot;language-text&quot;&gt;state&lt;/code&gt; input. This object contains properties or methods that let your UI function inform you about certain events, for example, a mouse click.&lt;/p&gt;
&lt;p&gt;For every event that occurs we update the state and this forces the application to render again:&lt;/p&gt;
&lt;div class=&quot;gatsby-highlight&quot; data-language=&quot;javascript&quot;&gt;&lt;pre style=&quot;counter-reset: linenumber NaN&quot; class=&quot;language-javascript line-numbers&quot;&gt;&lt;code class=&quot;language-javascript&quot;&gt;&lt;span class=&quot;token parameter&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; state &lt;span class=&quot;token parameter&quot;&gt;update&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&gt;&lt;/span&gt; render&lt;/code&gt;&lt;span aria-hidden=&quot;true&quot; class=&quot;line-numbers-rows&quot; style=&quot;white-space: normal; width: auto; left: 0;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This unidirectional data flow helps you build incredibly simple applications that are easy to test and maintain.&lt;/p&gt;
&lt;h2&gt;Remember this&lt;/h2&gt;
&lt;p&gt;Your aim as a React developer is to write declarative code as much as you can. It’s like learning how to drive a supercar.&lt;/p&gt;
&lt;p&gt;But how can we declaratively change the DOM and maintain good performance? What did you say? What is the DOM? OK this will take another coffee.&lt;/p&gt;</content:encoded></item><item><title><![CDATA[Hello World, Again!]]></title><description><![CDATA[Welcome to my world. A place where anti-patterns and dirty code create an unprecedented experience. A leap forward in boring engineering…]]></description><link>http://www.nicotsou.com/hello-world-again/</link><guid isPermaLink="false">http://www.nicotsou.com/hello-world-again/</guid><pubDate>Sun, 14 Feb 2021 22:12:00 GMT</pubDate><content:encoded>&lt;p&gt;Welcome to my world. A place where anti-patterns and dirty code create an unprecedented experience. A leap forward in boring engineering blogs, mixed with buzzwords and glossy images. This is how my narcissistic instincts come together in harmony. Yeah, you’ll be fine!&lt;/p&gt;
&lt;p&gt;This editorial is basically a celebration of my motivation. It took me 4 years to design this website. I’m not proud of my procrastination but I’m very happy it’s finally published. A part of me feels a relief. It’s like, I hear a voice in my head screaming “No way! He finally did it!“.&lt;/p&gt;
&lt;p&gt;I have no clue what to write as an introduction but I have a lot to share with you in the following posts. Sometimes I wake up in the middle of the night thinking about things. It always starts as a short paragraph and it ends up writing the story of my life. Lucky you!&lt;/p&gt;
&lt;p&gt;Since I’m a software engineer this blog will host technical articles, mostly related to the technologies I’m using on a daily basis. It’s also a collection of thoughts, step-by-step tutorials and references.&lt;/p&gt;
&lt;p&gt;I am curious how these articles will speak to you. If you have any questions or something to add, Twitter is the best way to reach out to me.&lt;/p&gt;</content:encoded></item></channel></rss>