<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[All Things Typescript]]></title><description><![CDATA[My name is Maina Wycliffe, and I am a web developer and typescript coach. Every week I break complex Typescript topics into small, easily understood, and ingestable bits that you can apply immediately in your journey to create awesome solutions.]]></description><link>https://www.allthingstypescript.dev</link><image><url>https://substackcdn.com/image/fetch/$s_!5AXN!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png</url><title>All Things Typescript</title><link>https://www.allthingstypescript.dev</link></image><generator>Substack</generator><lastBuildDate>Wed, 29 Apr 2026 05:57:11 GMT</lastBuildDate><atom:link href="https://www.allthingstypescript.dev/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Maina Wycliffe]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[allthingstypescript@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[allthingstypescript@substack.com]]></itunes:email><itunes:name><![CDATA[Maina Wycliffe]]></itunes:name></itunes:owner><itunes:author><![CDATA[Maina Wycliffe]]></itunes:author><googleplay:owner><![CDATA[allthingstypescript@substack.com]]></googleplay:owner><googleplay:email><![CDATA[allthingstypescript@substack.com]]></googleplay:email><googleplay:author><![CDATA[Maina Wycliffe]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Happy Holidays and New Year 2025]]></title><description><![CDATA[I appreciate your support in 2024, See you in 2025]]></description><link>https://www.allthingstypescript.dev/p/happy-holidays-and-new-year-2025</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/happy-holidays-and-new-year-2025</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Thu, 19 Dec 2024 15:41:46 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/6f7d1c62-a8bc-468e-9769-d004ffb05673_1280x853.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey Everyone,</p><p>This is Maina Wycliffe, the author of this newsletter, in case you don&#8217;t know me. As I write this, we are nearing the end of the year, about 11 days to go. I want to start by saying thank you to everyone who is subscribed to this newsletter and those who read my content from the bottom of my heart.</p><p>We have seen incredible growth this year alone, with over 2000 subscribers to this newsletter. I know this is probably not a large number, but to me, it&#8217;s the biggest I have ever been. I want to say thank you for your support, for reading my content, and for being there with me during my ups and downs.</p><p>In the latter half of the year, I haven&#8217;t been able to keep up with the content, and I want to resume that in 2025, with 2 - 3 high-quality articles every month instead of the usual weekly content, which I found hard to keep up with, and you can quickly get burned out. </p><p>That&#8217;s not the only reason I haven't been publishing regularly; over the last few months, I have been working on a Typescript Book. This has been taking up a lot of my time lately, but I am beginning to feel delighted with my progress. As we get closer to publishing it, I will release more details on the book early next year.</p><p>By reducing the number of articles to 2 to 3 per month, I aim to maintain a regular writing schedule for this newsletter while striking a balance between quality, my book, and my day job.</p><p>As we look to the future, I hope our collective journey of learning and growth will continue in 2025. I would like to take this opportunity to extend my warmest wishes to you all for a joyous holiday season and a happy new year.</p><p>Yours Truly,</p><p>Maina Wycliffe</p><p>All Things Typescript</p><p></p>]]></content:encoded></item><item><title><![CDATA[NodeJS adding experimental support for Typescript Code Execution]]></title><description><![CDATA[We take a quick look and brief at the experiment support for Typescript files in the latest version of Node JS (v22)]]></description><link>https://www.allthingstypescript.dev/p/nodejs-adding-experimental-support</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/nodejs-adding-experimental-support</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Tue, 12 Nov 2024 22:22:25 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/fd258514-e22f-45d9-9fcc-d63181c75314_3840x2160.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>One thing that set both Deno and <a href="https://www.allthingstypescript.dev/p/bun-one-tool-for-all-your-javascripttypescript">Bun</a> apart from Node was their ability to run Typescript files directly. This is because the type annotations aren&#8217;t part of Javascript. syntax, meaning they would throw a syntax error.</p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;8c3e0df7-831c-497d-ad40-fde17320c97c&quot;,&quot;duration&quot;:null}"></div><p>You needed to transpile the Typescript file to Javascript or use something like ts-node to do that for you. For instance, below, I am using the tsc compiler to transpile to Javascript.</p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;b2f46cee-e2de-4cab-98ed-93136b649c8d&quot;,&quot;duration&quot;:null}"></div><p>But as you can see, this can be tedious compared to something like Bun, which will execute Typescript directly.</p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;3d4349bd-cb1f-467b-9a0d-e27981802b54&quot;,&quot;duration&quot;:null}"></div><p>NodeJS is adding experiment support for stripping type from Typescript files so that we can execute the Typescript code.  NodeJS is introducing two new experimental flags; the first is for stripping Typescript types and is called <code>--experimental-strip-types</code>.</p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;cec7b588-70fd-4e79-8a4f-a80bbc8ae250&quot;,&quot;duration&quot;:null}"></div><p>The second one is <code>--experimental-transform-types</code> which is to transform typescript types for types such as enums that need to be converted to Javascript equivalent.</p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;e12f5f13-4489-4935-9f55-8f02eacf7abd&quot;,&quot;duration&quot;:null}"></div><p>Please note that the above is still experimental but a step in the right direction.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading All Things Typescript! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h3>Previous Articles</h3><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;55ac7776-95ee-48f1-83e2-b0d1eb0e0ff4&quot;,&quot;caption&quot;:&quot;In most issues for this newsletter, I have focussed on Typescript and its type system, which I will continue doing. This has been great for my readers (from the feedback I have received) but I have wanted to cover broader Typescript-related topics, such as today&#8217;s topic, after all this is All Things Typescript. I hope you will enjoy this topic and let m&#8230;&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Bun - The One Tool for All Your JavaScript/Typescript Project's Needs?&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-01-29T07:05:28.656Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/59df4cca-7a3d-49ab-8a87-5a1da1efa693_3840x2160.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/bun-one-tool-for-all-your-javascripttypescript&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:139489394,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:2,&quot;comment_count&quot;:2,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;f7d11ccd-3e90-4cb6-84e9-ae58f54b87d9&quot;,&quot;caption&quot;:&quot;In Typescript version 4.9, Typescript introduced the satisfies operator. Over the last few months, I have seen examples of it being used out in the world more often, and even I have found myself getting comfortable using it. In this issue, I want to do a deep dive and try and answer the following questions:&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;A Deep Dive into the satisfies operator in Typescript&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-02-24T20:07:07.093Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/150f856f-0aa6-4ab9-9b1b-34d4d178743a_3136x1788.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/a-deep-dive-into-the-typescript-satisfies&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:141990708,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:8,&quot;comment_count&quot;:6,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;b186724c-944c-46cf-a66d-74485ed079ae&quot;,&quot;caption&quot;:&quot;One of the more advanced topics in Typescript is Generics and understanding and adding them to your Typescript coding toolbox can be very powerful. As we continue with our journey of learning Typescript and building amazing projects, we want to have more and more tools that we can use to make our job easier, without making compromises when possible.&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;A Gentle Introduction to Generics in Typescript&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-03-11T05:58:29.780Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dcd0c857-8ca0-409a-bd1c-3154597b94c8_9048x5612.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/a-gentle-introduction-to-generics&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:142444537,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:5,&quot;comment_count&quot;:2,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;ed60df18-5faa-444d-97ea-866f07e9d0aa&quot;,&quot;caption&quot;:&quot;Over a decade ago, NodeJS was born, and not long after, Node Package Manager (NPM) was born, and the two have become synonymous with each other. Even most Javascript third-party package registries - different from package managers - yarn, bun, etc. - that have come after that, such as&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;JavaScript Registry (JSR): A Better Alternative to NPM &#129335;&#127998;?&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-06-17T07:24:58.801Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fc0848bf-4833-4066-bf74-5998438c1173_3840x2160.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/a-brief-introduction-to-javascript&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:144104812,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:6,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;08d6a1a2-670a-44b5-87ad-9562f02ac930&quot;,&quot;caption&quot;:&quot;We have all been there, and we have come across the following error:&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Avoid using Type Assertions in TypeScript&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2023-10-23T06:58:15.432Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1c1a6c85-8ccc-46d1-b380-9cdd2caf3007_888x499.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/avoid-using-type-assertions-in-typescript&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:138198930,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:4,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div>]]></content:encoded></item><item><title><![CDATA[June 2024 Monthly Newsletter -TypeScript 5.5 released with a huge upgrade]]></title><description><![CDATA[In this issue, I look at content from myself and other authors for the month of June, and also take a brief look at Typescript 5.5 notable features that were released last month]]></description><link>https://www.allthingstypescript.dev/p/june-2024-monthly-newsletter-typescript</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/june-2024-monthly-newsletter-typescript</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 08 Jul 2024 04:27:21 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/e6340281-aa8d-4eb4-9267-edb1ab2de57e_3840x2160.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey, Everyone. Another month has passed, and today is the day I look back on informative content I came across last month and news related to Typescript.</p><p>My name is Maina Wycliffe, and I love teaching Typescript to developers. My goal is to break down complex topics into small, understandable bits that you can apply in your day-to-day life as a developer.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><div><hr></div><h2>React Advanced London 2024</h2><p>I want to announce a special collaboration with <a href="https://gitnation.org/">GitNation</a>, the organizers of events such as JSNation and React Summit in Amsterdam and New York, React Day Berlin, and React Advanced in London.</p><p>React Advanced London 2024 is coming up later in the year (October), and as subscribers for All Things Typescript, you will get <strong>10%</strong> off for tickets using the code <strong><a href="https://ti.to/gitnation/react-advanced-london-2024/discount/AllThingsTypescript10">AllThingsTypescript10</a> </strong>for the conference.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FKWw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a08712-96dc-40c0-b196-515a4e20cfa4_1920x1080.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FKWw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a08712-96dc-40c0-b196-515a4e20cfa4_1920x1080.png 424w, https://substackcdn.com/image/fetch/$s_!FKWw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a08712-96dc-40c0-b196-515a4e20cfa4_1920x1080.png 848w, https://substackcdn.com/image/fetch/$s_!FKWw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a08712-96dc-40c0-b196-515a4e20cfa4_1920x1080.png 1272w, https://substackcdn.com/image/fetch/$s_!FKWw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a08712-96dc-40c0-b196-515a4e20cfa4_1920x1080.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FKWw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a08712-96dc-40c0-b196-515a4e20cfa4_1920x1080.png" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a0a08712-96dc-40c0-b196-515a4e20cfa4_1920x1080.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2356032,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!FKWw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a08712-96dc-40c0-b196-515a4e20cfa4_1920x1080.png 424w, https://substackcdn.com/image/fetch/$s_!FKWw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a08712-96dc-40c0-b196-515a4e20cfa4_1920x1080.png 848w, https://substackcdn.com/image/fetch/$s_!FKWw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a08712-96dc-40c0-b196-515a4e20cfa4_1920x1080.png 1272w, https://substackcdn.com/image/fetch/$s_!FKWw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a08712-96dc-40c0-b196-515a4e20cfa4_1920x1080.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In this conference, you learn more about React 19, Performance, Server Components, Senior-Driven Tech Mindset &amp; more! On top of that, you will get to network and reconnect with some of the top minds in the industry and engage in board games and, if you want to, parties and karaoke.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://ti.to/gitnation/react-advanced-london-2024/discount/AllThingsTypescript10&quot;,&quot;text&quot;:&quot;Get your ticket today&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://ti.to/gitnation/react-advanced-london-2024/discount/AllThingsTypescript10"><span>Get your ticket today</span></a></p><div><hr></div><h2>Typescript 5.5 Released</h2><p>The latest version of Typescript (version 5.5) was released in June and has a very exciting update that all Typescript developers are excited about. Typescript has improved Type inference and now can narrow Types based on control flow within arrays when you use the array filter method, which wasn&#8217;t possible before. </p><p>Before Typescript 5.5., if you filtered an array to, let&#8217;s say, remove undefined values inside an array, Typescript wasn&#8217;t able to infer that the type needed to be narrowed as the undefined values have been removed. This has changed, as demonstrated in the video clip I created below when the beta version was released.</p><div id="youtube2-ugeQfRdzLRk" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;ugeQfRdzLRk&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/ugeQfRdzLRk?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><p>Other notable features include:</p><ul><li><p>Control flow narrowing for Constant Indexed Accesses</p></li><li><p>Import types into JS Docs (JS Files)</p></li><li><p>Type-checking in regular expressions</p></li><li><p><a href="https://github.com/tc39/proposal-set-methods">ESM Set new methods</a> such as <code>union</code> and <code>intersection</code> are now supported.</p></li></ul><p>And there is more, which you can learn by reading the announcement blog <a href="https://devblogs.microsoft.com/typescript/announcing-typescript-5-5/">here</a>.</p><p>Next week, I will take a deep dive into some of the exciting features of Typescript 5.5.</p><div><hr></div><h2>From All Things Typescript</h2><p>I want to apologize for not publishing as regularly as in previous months, but I will resume publishing regularly in June, starting next week when I take a deep dive into Typescript 5.5.</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;365c9550-3ed5-4995-bfa4-9473d23a95ac&quot;,&quot;caption&quot;:&quot;Over a decade ago, NodeJS was born, and not long after, Node Package Manager (NPM) was born, and the two have become synonymous with each other. Even most Javascript third-party package registries - different from package managers - yarn, bun, etc. - that have come after that, such as&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;JavaScript Registry (JSR): A Better Alternative to NPM &#129335;&#127998;?&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-06-17T07:24:58.801Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fc0848bf-4833-4066-bf74-5998438c1173_3840x2160.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/a-brief-introduction-to-javascript&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:144104812,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:6,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div><hr></div><p>I want to give a shout-out to CSS Isn&#8217;t Magic by Stas Melnikov. Most of us struggle with understanding CSS concepts, and If you want to change that and take your CSS game to the next level, please subscribe to the newsletter.</p><div class="embedded-publication-wrap" data-attrs="{&quot;id&quot;:610708,&quot;name&quot;:&quot;CSS isn't magic&quot;,&quot;logo_url&quot;:null,&quot;base_url&quot;:&quot;https://cssisntmagic.substack.com&quot;,&quot;hero_text&quot;:&quot;the tips that help to break the stereotype CSS is magic&quot;,&quot;author_name&quot;:&quot;Stas Melnikov&quot;,&quot;show_subscribe&quot;:true,&quot;logo_bg_color&quot;:&quot;#ffffff&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="EmbeddedPublicationToDOMWithSubscribe"><div class="embedded-publication show-subscribe"><a class="embedded-publication-link-part" native="true" href="https://cssisntmagic.substack.com?utm_source=substack&amp;utm_campaign=publication_embed&amp;utm_medium=web"><span class="embedded-publication-name">CSS isn't magic</span><div class="embedded-publication-hero-text">the tips that help to break the stereotype CSS is magic</div><div class="embedded-publication-author-name">By Stas Melnikov</div></a><form class="embedded-publication-subscribe" method="GET" action="https://cssisntmagic.substack.com/subscribe?"><input type="hidden" name="source" value="publication-embed"><input type="hidden" name="autoSubmit" value="true"><input type="email" class="email-input" name="email" placeholder="Type your email..."><input type="submit" class="button primary" value="Subscribe"></form></div></div><h2>From Other Authors</h2><h4><a href="https://itnext.io/how-to-write-api-response-types-with-typescript-f8152ddd43dd">How To Write API Response Types with TypeScript by Artem Holinka</a></h4><p>Organize TypeScript types for API responses to ensure a great development experience, code maintenance, and avoidance of obvious coding errors.</p><p><a href="https://itnext.io/how-to-write-api-response-types-with-typescript-f8152ddd43dd">Link</a></p><h4><a href="https://dev.to/mattlewandowski93/7-use-cases-for-javascript-proxies-3b29">7 Use Cases for Javascript Proxies by Matt Lewandowski</a></h4><p>JavaScript's Proxy object is a useful tool that opens up a world of possibilities, allowing you to create some really useful behaviors in your applications. When combined with TypeScript, Proxy enhances your ability to manage and manipulate objects and functions in ways you might not have thought possible. In this article, we'll explore the incredible utility of Proxies through practical examples.</p><p><a href="https://dev.to/mattlewandowski93/7-use-cases-for-javascript-proxies-3b29">Link</a></p><h4><a href="https://storybook.js.org/blog/visual-testing-is-the-greatest-trick-in-ui-development/">Visual testing: The greatest trick in UI development by Michael Shilman</a></h4><p>In UI development, making sure everything looks right is as important as making sure it works. Visual tests are image snapshot tests that solve for this.</p><p>However, somewhat surprisingly, they can also replace the most brittle part of many UI unit tests: asserting on details of the UI. In many cases this can replace the unit test entirely, allowing you to test more with far less code.</p><p><a href="https://storybook.js.org/blog/visual-testing-is-the-greatest-trick-in-ui-development/">Link</a></p><h4><a href="https://deno.com/blog/deno-bites-ts-intro">A Gentle Intro to TypeScript by Jo Franchetti</a></h4><p>This article is an excellent article for developers who are just getting started with Typescript and wondering why Typescript exists and the fundamentals of Typescript.</p><p><a href="https://deno.com/blog/deno-bites-ts-intro">Link</a></p><h4><a href="https://dev.to/alexmercedcoder/introduction-to-sorting-algorithms-in-javascript-b60">Introduction to Sorting Algorithms in JavaScript by Alex Merced</a></h4><p>Sorting algorithms are fundamental to computer science and programming. They are essential tools for organizing data in a meaningful order, whether it&#8217;s numerical, alphabetical, or based on any other criteria. For JavaScript developers, understanding these algorithms is crucial, as they often need to manipulate and sort data efficiently within their applications. This blog aims to provide an introduction to some of the most common sorting algorithms implemented in JavaScript, highlighting their mechanics and when to use them.</p><p><a href="https://dev.to/alexmercedcoder/introduction-to-sorting-algorithms-in-javascript-b60">Link</a></p><h4><a href="https://johnnyreilly.com/dual-publishing-esm-cjs-modules-with-tsup-and-are-the-types-wrong">Dual Publishing ESM and CJS Modules with tsup and Are the Types Wrong? by John Reilly</a></h4><p>If you need to publish a package that supports both ECMAScript modules (ESM) and CommonJS modules (CJS), you can use <code>tsup</code> to do so. This post will show you how to do that and how to ensure that the types are correct using the tool <code>Are the Types Wrong?</code>.</p><p><a href="https://johnnyreilly.com/dual-publishing-esm-cjs-modules-with-tsup-and-are-the-types-wrong">Link</a></p><h4><a href="https://dev.to/jps27cse/understanding-javascript-object-accessors-3i25">Understanding JavaScript Object Accessors by Jack Pritom Soren</a></h4><p>JavaScript is a versatile and powerful programming language used extensively in web development. One of its key features is the ability to define objects, which can encapsulate properties and methods. Among the various ways to interact with these objects, accessors play a crucial role. This blog post will delve into the concept of JavaScript object accessors, explaining what they are, how they work, and why they are beneficial.</p><p><a href="https://dev.to/jps27cse/understanding-javascript-object-accessors-3i25">Link</a></p><h4><a href="https://dev.to/wasp/ive-been-writing-typescript-without-understanding-it-5ef4">I've been writing TypeScript without understanding it by vincanger</a></h4><p>The other day, I was stuck with a bug in some code that was handling optimistic updates, so I asked my colleague Filip for some help. Filip, a TypeScript wizard, mentioned that the satisfies keyword would be part of the solution I was looking for.</p><p>Satisfies? What the heck is that? And why had I never heard of it before? I mean, I&#8217;ve been using TypeScript for some time now, so I was surprised I didn&#8217;t know it myself.</p><p><a href="https://dev.to/wasp/ive-been-writing-typescript-without-understanding-it-5ef4">Link</a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><div><hr></div><p>That's it for this issue. Thank you for getting this far. If you enjoyed this article and would like to support my work, please share and like this issue and consider sharing All Things Typescript with friends and colleagues.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&quot;,&quot;text&quot;:&quot;Share All Things Typescript&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share"><span>Share All Things Typescript</span></a></p><p>Did you know you can hire me to coach your team and help them improve their Typescript skills? If this interests you, please get in touch with me to discuss it.</p><div class="directMessage button" data-attrs="{&quot;userId&quot;:43214086,&quot;userName&quot;:&quot;Maina Wycliffe&quot;,&quot;canDm&quot;:null,&quot;dmUpgradeOptions&quot;:null,&quot;isEditorNode&quot;:true}" data-component-name="DirectMessageToDOM"></div><p>And until next time, please keep on learning.</p>]]></content:encoded></item><item><title><![CDATA[JavaScript Registry (JSR): A Better Alternative to NPM 🤷🏾?]]></title><description><![CDATA[We are going to look at what JSR is and what makes different from NPM and how backward compatible it is with NPM]]></description><link>https://www.allthingstypescript.dev/p/a-brief-introduction-to-javascript</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/a-brief-introduction-to-javascript</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 17 Jun 2024 07:24:58 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/fc0848bf-4833-4066-bf74-5998438c1173_3840x2160.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Over a decade ago, NodeJS was born, and not long after, Node Package Manager (NPM) was born, and the two have become synonymous with each other. Even most Javascript third-party package registries - different from package managers - yarn, <a href="https://www.allthingstypescript.dev/p/bun-one-tool-for-all-your-javascripttypescript">bun</a>, etc. - that have come after that, such as <a href="https://github.com/features/packages">GitHub Package Manager</a>, the key differentiating factor has been value proposition (i.e., private repositories) rather than functionality - most offer the same features as NPM.</p><h3>What&#8217;s a Registry?</h3><p>Before discussing JSR, let&#8217;s talk about what a registry is. A registry is a centralized place to find and discover 3rd-party packages. Currently, NPM is the defacto package manager for Javascript and Typescript. With millions of Javascript libraries (packages), both public and private, that you can find and use.</p><blockquote><p>npm is the world's largest software registry. Open source developers from every continent use npm to share and borrow packages, and many organizations use npm to manage private development as well. - <a href="https://docs.npmjs.com/about-npm">NPMJS</a></p></blockquote><p>Using 3rd party libraries saves a lot of time during development and borrows code from other developers to help solve common problems like developing web apps, where we have a few options such as React, Angular, Vue, etc. Most of these 3rd party libraries also benefit from being open source, with multiple contributors, and being consumed by countless people, which means they are battle-tested and improved quality. </p><p>Registries such as <a href="https://docs.npmjs.com/about-npm">NPMJS</a> and <a href="https://unpkg.com/">UNPKG</a>, among others, help with the discovery and easy management of packages.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to typescript with free weekly lessons</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>What is JSR?</h2><p>JSR is brought to us by Deno and is a modern package repository for Javascript and Typescript (NodeJS, Bun, Deno, etc.). It is built to be fully backward compatible with NPM. JSR aims to bring in some much-needed standards for package publishing by development.</p><p>When I watched the introduction to JSR video by Ryan Dalh (creator of both Node and Deno) at DevWorld earlier this year, the one thing that stuck with me was how he described JSR. JSR is to NPM, as Typescript is to Javascript. You can watch the whole video <a href="https://youtu.be/MFCn4ce5dVc?si=dOsCfSYt9pmSOsnS">here</a>; it&#8217;s worth every second.</p><p>As a package author, there are many things to consider before publishing a package. Do you choose ESModules, or CommonJS, or both? This impacts how the end user consumes your package and can be very frustrating for both the author (most of which are doing it for free) and consumers. </p><p>JSR looks to bring some much-needed enforced standardizations like ES Modules only support so that developers ship their code the way they wrote instead of compiling it before pushing it to the repository.</p><h3>So, what makes JSR Different</h3><p>There are a few key features that JSR prides itself on:</p><h4>First-Party Typescript Support</h4><p>Let&#8217;s say you have built a library you want to ship to developers via NPM. You have to transpile it to Javascript and also generate Typescript <a href="https://www.typescriptlang.org/docs/handbook/2/type-declarations.html">declaration files</a> (<code>.d.ts</code>). This means that when you want to <strong><a href="https://code.visualstudio.com/docs/editor/editingevolved#_go-to-definition">go to the definition</a></strong> for an NPM package, instead of landing on the actual code you want, you end up landing inside the Typescript declaration file. While this is useful, sometimes you want to look at the code and see the implementation details.</p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;ade9f8d1-bf2b-45de-b83f-a89fe43b4e18&quot;,&quot;duration&quot;:null}"></div><p><em>As you can see from the above video, when we go to the definition for the useReactTable hook, instead of landing on the package code, we end up in the Type declaration files instead.</em> </p><p>However, by supporting Typescript out of the box, JSR encourages developers to ship Typescript code directly instead of using Javascript and Type Declaration. This has the added benefit of easy documentation generation and improved performance for your code editor. You can learn more about this <a href="https://jsr.io/docs/about-slow-types">here</a>.</p><h4>ESM Modules only Packages</h4><p>Another key differentiator compared to NPM is that JSR doesn&#8217;t support CommonJS; it only supports <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules">ESModules</a> - <code>import</code> and <code>exports</code>, not <code>require</code> syntax.</p><p>Javascript has had, for a while now, two competing module standards - ESModules and CommonJS- and I will be writing a comprehensive write-up between the two and others, as well as the journey of how we got here in the future.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><div><hr></div><h4>Compatibility with NPM</h4><blockquote><p>JSR is a superset of npm </p></blockquote><p>Another key feature of JSR is that it is fully backward and compatible with NPM packages; you can consume both NPM and JSR packages. As a package developer, you can reference NPM packages to packages you publish to JSR. This is huge, as it means all NPM packages are available for you and aren&#8217;t limited to packages available in JSR.</p><p>This reminds me why I think BunJS is special: its compatibility with node. You don&#8217;t have to change your NodeJS code to run it with Bun. You can learn more about it in a previous issue <a href="https://www.allthingstypescript.dev/p/bun-one-tool-for-all-your-javascripttypescript">here</a>.</p><blockquote><p><strong>Please note</strong>: when installing a JSR package in an NPM project, JSR will transpile the Typescript project, before being installed locally (<em>inside the </em><code>node_modules</code><em> directory</em>). </p></blockquote><h4>Documentation Generation</h4><p>JSR will generate documentation for your packages from your JS Docs automatically when you publish your packages; there is no need to maintain a separate docs site for package maintainers. You can learn more about this <a href="https://jsr.io/docs/writing-docs">here</a>.</p><h4>All packages are scoped</h4><p>Another key difference between JSR and NPM is that all packages are scoped; for instance, instead of just typescript, Microsoft would have to decide whether to scope it under Microsoft - <code>@microsoft/typescript</code> or something else. To prevent abuse, JSR has reserved some common scopes, such as Google, Microsoft, etc., that can be used maliciously if they fall into the wrong hands.</p><div><hr></div><p><strong>If you like my content and want to support my work, please consider supporting me (</strong><em><strong>you can buy me a double latte, the juice that powers my creativity and determination &#9749;&#65039;</strong></em><strong>) through <a href="https://github.com/sponsors/mainawycliffe/">Github Sponsors</a>.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://github.com/sponsors/mainawycliffe/&quot;,&quot;text&quot;:&quot;&#9749;&#65039; Consider buying me a cup of coffee&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://github.com/sponsors/mainawycliffe/"><span>&#9749;&#65039; Consider buying me a cup of coffee</span></a></p><div><hr></div><h2>How to get started with JSR</h2><p>When I tried my hand at JSR for the first time, I was impressed by how easy it was to get started. It&#8217;s easy to start with JSR, and you can adopt it in your project today, just like with other NPM-compatible Javascript package registries.</p><p>First, once you find a package you intend to use, select your package manager - npm, pnpm, bun, deno, etc. JSR will give you the command you need to install the package in your environment, as shown below:</p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;c60634c6-b3b4-494b-a813-b3db0d4a8d9f&quot;,&quot;duration&quot;:null}"></div><p>If you are on NPM like most developers will be, JSR will add JSR (<code>@jsr:registry=https://npm.jsr.io</code>) as the registry in the NPM config file (<code>.npmrc</code>) and then install the package inside the <code>node_modules</code> just like a normal NPM package. </p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;525e91ac-5f25-47df-a137-8bde2e2e8542&quot;,&quot;duration&quot;:null}"></div><p>Finally, you can consume the package just like any other NPM package in your project, and both NPM and JSR packages can be used in the same project, as shown below.</p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;6e8fbc7c-a62f-4ec2-aae7-52be7b48e940&quot;,&quot;duration&quot;:null}"></div><p>Easy, right?</p><h3>Conclusion</h3><p>In this issue, we looked at JSR and what makes JSR different from NPM. We looked at the key differentiators of the Javascript repository and what set it apart compared to other package managers, such as first-class support for Typescript and ES Modules packages.</p><p>On top of that, JSR looks to generate documentation for the packages published in the repository, which is fully backward compatible with NPM. You can start using it today.</p><div><hr></div><p>That's it for this issue. Thank you for getting this far. If you enjoyed this issue and would like to support my work, please share and like this issue and consider sharing All Things Typescript with friends and colleagues.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&quot;,&quot;text&quot;:&quot;Share All Things Typescript&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share"><span>Share All Things Typescript</span></a></p><p>Did you know you can hire me to coach your team and help them improve their Typescript skills? If so, please get in touch with me to discuss it.</p><div class="directMessage button" data-attrs="{&quot;userId&quot;:43214086,&quot;userName&quot;:&quot;Maina Wycliffe&quot;,&quot;canDm&quot;:null,&quot;dmUpgradeOptions&quot;:null,&quot;isEditorNode&quot;:true}" data-component-name="DirectMessageToDOM"></div><p>And until next time, please keep on learning.</p>]]></content:encoded></item><item><title><![CDATA[May 2024 Monthly Newsletter - using the in operator for type narrowing]]></title><description><![CDATA[In this issue, I look at content from myself and other authors for the month of May and I also add in Typescript type, on how you can use the in operator for type narrowing over type assertions.]]></description><link>https://www.allthingstypescript.dev/p/may-2024-monthly-newsletter-using</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/may-2024-monthly-newsletter-using</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 03 Jun 2024 04:56:52 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!GE_5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6fe62ae2-d6d1-45b1-8aec-4a47c745a8ac_10704x10632.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey, Everyone. Another month has passed, and today is the day I look back on informative content I came across last month and news related to Typescript.</p><p>My name is Maina Wycliffe, and I love teaching Typescript to developers. My goal is to break down complex topics into small, understandable bits that you can apply in your day-to-day life as a developer.</p><p>If you want to learn Typescript, please subscribe to this newsletter.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><p>Now that that self-plug is out of the way (subtle) let&#8217;s get to today&#8217;s issue.</p><div><hr></div><h3>Typescript Tip &#128161;</h3><p>This month was thin on official Typescript news, so instead of that, I will leave you with a Typescript tip instead.</p><p>Many developers habitually reach out for <a href="https://www.allthingstypescript.dev/p/avoid-using-type-assertions-in-typescript">type assertions</a> when checking whether an object with a union of types has specific fields. The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in">in-operator</a> is perfect for this, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GE_5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6fe62ae2-d6d1-45b1-8aec-4a47c745a8ac_10704x10632.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GE_5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6fe62ae2-d6d1-45b1-8aec-4a47c745a8ac_10704x10632.png 424w, https://substackcdn.com/image/fetch/$s_!GE_5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6fe62ae2-d6d1-45b1-8aec-4a47c745a8ac_10704x10632.png 848w, https://substackcdn.com/image/fetch/$s_!GE_5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6fe62ae2-d6d1-45b1-8aec-4a47c745a8ac_10704x10632.png 1272w, https://substackcdn.com/image/fetch/$s_!GE_5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6fe62ae2-d6d1-45b1-8aec-4a47c745a8ac_10704x10632.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GE_5!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6fe62ae2-d6d1-45b1-8aec-4a47c745a8ac_10704x10632.png" width="1200" height="1191.7582417582419" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6fe62ae2-d6d1-45b1-8aec-4a47c745a8ac_10704x10632.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:1446,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:17675441,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!GE_5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6fe62ae2-d6d1-45b1-8aec-4a47c745a8ac_10704x10632.png 424w, https://substackcdn.com/image/fetch/$s_!GE_5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6fe62ae2-d6d1-45b1-8aec-4a47c745a8ac_10704x10632.png 848w, https://substackcdn.com/image/fetch/$s_!GE_5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6fe62ae2-d6d1-45b1-8aec-4a47c745a8ac_10704x10632.png 1272w, https://substackcdn.com/image/fetch/$s_!GE_5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6fe62ae2-d6d1-45b1-8aec-4a47c745a8ac_10704x10632.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>From All Things Typescript</h3><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;09505ab7-83e9-4da9-be27-e3dcfd763500&quot;,&quot;caption&quot;:&quot;For a while now, I have meant to write about this issue to collect and convey my thoughts on Schema First Type design in Typescript. I will be giving this talk for the This is Learning virtual conference, and I thought it would be amazing to have an issue accompanying the talk&#8212;killing two birds with one stone.&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Schema First Type Design in Typescript&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-05-20T05:51:47.628Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9b9aec0f-0f62-4147-a27f-59163af9e84d_3840x2160.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/schema-first-type-design-in-typescript&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:144358637,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:3,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;30439898-9413-4956-b0ad-0c11224fc828&quot;,&quot;caption&quot;:&quot;Typescript usually has two schools of thought&#8212;enums and unions&#8212;with strong supporters on both sides of the argument. In today's issue, I want to avoid that debate and focus solely on Enums, specifically a different, lesser-known version of enums: const enums.&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Why do we have const enums in Typescript?&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-05-27T07:04:53.517Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dabc46f3-9002-4ca7-894a-2b56708f05e5_3840x2160.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/why-do-we-have-const-enums-in-typescript&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:135813058,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:2,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div><hr></div><p><strong>If you like my content and want to support my work, please consider supporting me (</strong><em><strong>you can buy me a double latte, the juice that powers my creativity and determination &#9749;&#65039;</strong></em><strong>) through <a href="https://github.com/sponsors/mainawycliffe/">Github Sponsors</a>.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://github.com/sponsors/mainawycliffe/&quot;,&quot;text&quot;:&quot;&#9749;&#65039; Consider buying me a cup of coffee&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://github.com/sponsors/mainawycliffe/"><span>&#9749;&#65039; Consider buying me a cup of coffee</span></a></p><div><hr></div><h3>Book Recommendation</h3><h4>Learning Typescript by Josh Goldberg</h4><p>This is an amazing book, especially if you are starting Typescript. It&#8217;s well laid out, and the concepts are broken down into small, easy-to-understand concepts that are easy to follow and grasp different concepts in Typescript.</p><p>You can learn more about the book <a href="https://www.learningtypescript.com/">here</a>.</p><div><hr></div><h3>From Other Authors</h3><h4><a href="https://blog.appsignal.com/2024/05/08/effective-testing-in-javascript.html">Effective Testing in JavaScript by Ashley Davis</a></h4><p>This article emphasizes the importance of testing, advocating for a proactive approach where testing is frequent and early, ensuring code remains functional. The author shares personal insights and techniques to make testing less laborious while still effective, ultimately underscoring the goal of delivering reliable code that meets customer needs.</p><p><a href="https://blog.appsignal.com/2024/05/08/effective-testing-in-javascript.html">Link</a></p><h4><a href="https://thenewstack.io/automatically-generate-types-for-your-postgresql-database/">Automatically Generate Types for Your PostgreSQL Database by Paul Scanlon</a></h4><p>SQL can be typesafe, in the JavaScript sense of the word. Paul Scanlon demonstrates why devs shouldn't be afraid to work with "raw SQL."</p><p><a href="https://thenewstack.io/automatically-generate-types-for-your-postgresql-database/">Link</a></p><h4><a href="https://blog.ohansemmanuel.com/the-typescript-feature-we-all-wanted/">The Typescript feature we all wanted by Ohans Emmanuel</a></h4><p>Inferred Type Predicates are now available in Typescript 5.5 Beta. In this article, I&#8217;ll explain the much-awaited feature and how it&#8217;ll help you write more type-safe code.</p><p><a href="https://blog.ohansemmanuel.com/the-typescript-feature-we-all-wanted/">Link</a></p><h4><a href="https://www.freecodecamp.org/news/a-guide-to-the-node-js-event-loop/">A Guide to the Node.js Event Loop by Musab Habeeb</a></h4><p>The Node.js event loop is a crucial mechanism in Node.js that makes Node.js programs run concurrently and asynchronously. Mastering the Node.js event loop helps a Node.js developer understand how Node.js programs run under the hood.</p><p>In this article, you will learn the basics of the event loop, starting with threads and processes, then how the JavaScript event loop works, and finally, how the Node.js event loop works.</p><p><a href="https://www.freecodecamp.org/news/a-guide-to-the-node-js-event-loop/">Link</a></p><h4><a href="https://deno.com/blog/document-javascript-package">How to document your JavaScript package Andy Jiang, Luca Casonato, Jo Franchetti, and Leo Kettmeir</a></h4><p>This article emphasizes the importance of writing clear, concise JSDocs. It outlines best practices for creating JSDocs, including providing good type information, using tags to add detail, and including examples to demonstrate usage. It also discusses the significance of keeping documentation up-to-date with code changes and auditing JSDoc comments to ensure quality. The goal is to help users quickly understand how to use a package with minimal effort, enhancing the package's success within the community.</p><p><a href="https://deno.com/blog/document-javascript-package">Link</a></p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><div><hr></div><p>That's it for this issue. Thank you for getting this far. If you enjoyed this article and would like to support my work, please share and like this issue and consider sharing All Things Typescript with friends and colleagues.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&quot;,&quot;text&quot;:&quot;Share All Things Typescript&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share"><span>Share All Things Typescript</span></a></p><p>Did you know you can hire me to coach your team and help them improve their Typescript skills? If this interests you, please get in touch with me to discuss it.</p><div class="directMessage button" data-attrs="{&quot;userId&quot;:43214086,&quot;userName&quot;:&quot;Maina Wycliffe&quot;,&quot;canDm&quot;:null,&quot;dmUpgradeOptions&quot;:null,&quot;isEditorNode&quot;:true}" data-component-name="DirectMessageToDOM"></div><p>And until next time, please keep on learning.</p>]]></content:encoded></item><item><title><![CDATA[Why do we have const enums in Typescript?]]></title><description><![CDATA[We explore enums in Typescript and their behavior at runtime, and why const enums exists and their different characteristics behavior at runtime as compared to regular enums.]]></description><link>https://www.allthingstypescript.dev/p/why-do-we-have-const-enums-in-typescript</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/why-do-we-have-const-enums-in-typescript</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 27 May 2024 07:04:53 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/dabc46f3-9002-4ca7-894a-2b56708f05e5_3840x2160.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Typescript usually has two schools of thought&#8212;enums and unions&#8212;with strong supporters on both sides of the argument. In today's issue, I want to avoid that debate and focus solely on Enums, specifically a different, lesser-known version of enums: const enums.</p><p>Before we go any further, let&#8217;s briefly talk about what enums are: Enums allow developers to define a list of named constants representing distinct states within their application. For instance, if you wanted to have a switch button, the values can either be on or off, and an enum is perfect to represent this state, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5APT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27037c0d-c06e-438a-9894-09afdc299f83_2596x972.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5APT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27037c0d-c06e-438a-9894-09afdc299f83_2596x972.png 424w, https://substackcdn.com/image/fetch/$s_!5APT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27037c0d-c06e-438a-9894-09afdc299f83_2596x972.png 848w, https://substackcdn.com/image/fetch/$s_!5APT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27037c0d-c06e-438a-9894-09afdc299f83_2596x972.png 1272w, https://substackcdn.com/image/fetch/$s_!5APT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27037c0d-c06e-438a-9894-09afdc299f83_2596x972.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5APT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27037c0d-c06e-438a-9894-09afdc299f83_2596x972.png" width="1456" height="545" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/27037c0d-c06e-438a-9894-09afdc299f83_2596x972.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:545,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:90549,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5APT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27037c0d-c06e-438a-9894-09afdc299f83_2596x972.png 424w, https://substackcdn.com/image/fetch/$s_!5APT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27037c0d-c06e-438a-9894-09afdc299f83_2596x972.png 848w, https://substackcdn.com/image/fetch/$s_!5APT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27037c0d-c06e-438a-9894-09afdc299f83_2596x972.png 1272w, https://substackcdn.com/image/fetch/$s_!5APT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27037c0d-c06e-438a-9894-09afdc299f83_2596x972.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>And you can use the above enum, just like any type in Typescript.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WYOL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa492c25d-76d4-4406-971d-1f169d99127b_2596x1380.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WYOL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa492c25d-76d4-4406-971d-1f169d99127b_2596x1380.png 424w, https://substackcdn.com/image/fetch/$s_!WYOL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa492c25d-76d4-4406-971d-1f169d99127b_2596x1380.png 848w, https://substackcdn.com/image/fetch/$s_!WYOL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa492c25d-76d4-4406-971d-1f169d99127b_2596x1380.png 1272w, https://substackcdn.com/image/fetch/$s_!WYOL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa492c25d-76d4-4406-971d-1f169d99127b_2596x1380.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WYOL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa492c25d-76d4-4406-971d-1f169d99127b_2596x1380.png" width="1456" height="774" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a492c25d-76d4-4406-971d-1f169d99127b_2596x1380.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:774,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:174377,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WYOL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa492c25d-76d4-4406-971d-1f169d99127b_2596x1380.png 424w, https://substackcdn.com/image/fetch/$s_!WYOL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa492c25d-76d4-4406-971d-1f169d99127b_2596x1380.png 848w, https://substackcdn.com/image/fetch/$s_!WYOL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa492c25d-76d4-4406-971d-1f169d99127b_2596x1380.png 1272w, https://substackcdn.com/image/fetch/$s_!WYOL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa492c25d-76d4-4406-971d-1f169d99127b_2596x1380.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>To understand the difference between enums and const enums, we first need to understand their behavior after compilation before the behavior is very similar.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption"><strong>If you want to learn Typescript with weekly lessons, please subscribe.</strong></p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><p>In the book <a href="https://effectivetypescript.com/">Effective Typescript by Don Vanderkom</a> (a strongly recommended read), the author says that typescript has no impact on Javascript runtime. This is because Types are removed during transpilation, leaving you with good old Javascript. You can learn more about this relationship in this <a href="https://www.allthingstypescript.dev/p/typescript-and-javascript-two-sides">issue</a>, which we covered a few weeks ago.</p><div class="pullquote"><p>Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript&#8212;<a href="https://www.typescriptlang.org/docs/handbook/enums.html">Official TS Docs</a>.</p></div><p>Of course, this isn&#8217;t always the case, and Enums are one of those exceptions, with Enums getting converted into objects instead of being removed, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wZoN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd196029c-9729-4c0e-8ace-00bd42c42e6b_3840x1356.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wZoN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd196029c-9729-4c0e-8ace-00bd42c42e6b_3840x1356.png 424w, https://substackcdn.com/image/fetch/$s_!wZoN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd196029c-9729-4c0e-8ace-00bd42c42e6b_3840x1356.png 848w, https://substackcdn.com/image/fetch/$s_!wZoN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd196029c-9729-4c0e-8ace-00bd42c42e6b_3840x1356.png 1272w, https://substackcdn.com/image/fetch/$s_!wZoN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd196029c-9729-4c0e-8ace-00bd42c42e6b_3840x1356.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wZoN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd196029c-9729-4c0e-8ace-00bd42c42e6b_3840x1356.png" width="1456" height="514" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d196029c-9729-4c0e-8ace-00bd42c42e6b_3840x1356.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:514,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:221934,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wZoN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd196029c-9729-4c0e-8ace-00bd42c42e6b_3840x1356.png 424w, https://substackcdn.com/image/fetch/$s_!wZoN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd196029c-9729-4c0e-8ace-00bd42c42e6b_3840x1356.png 848w, https://substackcdn.com/image/fetch/$s_!wZoN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd196029c-9729-4c0e-8ace-00bd42c42e6b_3840x1356.png 1272w, https://substackcdn.com/image/fetch/$s_!wZoN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd196029c-9729-4c0e-8ace-00bd42c42e6b_3840x1356.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>So, when we pass an enum, the object that is produced during the transpilation process is referenced, as shown below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Ijx9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F873effa0-d6be-4b07-9b2e-68feebd58837_4584x1684.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Ijx9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F873effa0-d6be-4b07-9b2e-68feebd58837_4584x1684.png 424w, https://substackcdn.com/image/fetch/$s_!Ijx9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F873effa0-d6be-4b07-9b2e-68feebd58837_4584x1684.png 848w, https://substackcdn.com/image/fetch/$s_!Ijx9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F873effa0-d6be-4b07-9b2e-68feebd58837_4584x1684.png 1272w, https://substackcdn.com/image/fetch/$s_!Ijx9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F873effa0-d6be-4b07-9b2e-68feebd58837_4584x1684.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Ijx9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F873effa0-d6be-4b07-9b2e-68feebd58837_4584x1684.png" width="1456" height="535" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/873effa0-d6be-4b07-9b2e-68feebd58837_4584x1684.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:535,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:360217,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Ijx9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F873effa0-d6be-4b07-9b2e-68feebd58837_4584x1684.png 424w, https://substackcdn.com/image/fetch/$s_!Ijx9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F873effa0-d6be-4b07-9b2e-68feebd58837_4584x1684.png 848w, https://substackcdn.com/image/fetch/$s_!Ijx9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F873effa0-d6be-4b07-9b2e-68feebd58837_4584x1684.png 1272w, https://substackcdn.com/image/fetch/$s_!Ijx9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F873effa0-d6be-4b07-9b2e-68feebd58837_4584x1684.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Another thing to note about enums is that the values of each element in an Enum are numbers - <em>incrementing from 0, from the top down, unless the first number is indicated</em> - instead of the values we entered above, so in our case, <code>ON</code> would be <code>0</code> and <code>Off</code> would be <code>1</code>. Below is a case where the initial number is indicated.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tkSk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe576ab10-214d-4318-888b-34499462c1a1_3840x1356.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tkSk!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe576ab10-214d-4318-888b-34499462c1a1_3840x1356.png 424w, https://substackcdn.com/image/fetch/$s_!tkSk!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe576ab10-214d-4318-888b-34499462c1a1_3840x1356.png 848w, https://substackcdn.com/image/fetch/$s_!tkSk!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe576ab10-214d-4318-888b-34499462c1a1_3840x1356.png 1272w, https://substackcdn.com/image/fetch/$s_!tkSk!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe576ab10-214d-4318-888b-34499462c1a1_3840x1356.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tkSk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe576ab10-214d-4318-888b-34499462c1a1_3840x1356.png" width="1456" height="514" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e576ab10-214d-4318-888b-34499462c1a1_3840x1356.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:514,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:229389,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tkSk!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe576ab10-214d-4318-888b-34499462c1a1_3840x1356.png 424w, https://substackcdn.com/image/fetch/$s_!tkSk!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe576ab10-214d-4318-888b-34499462c1a1_3840x1356.png 848w, https://substackcdn.com/image/fetch/$s_!tkSk!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe576ab10-214d-4318-888b-34499462c1a1_3840x1356.png 1272w, https://substackcdn.com/image/fetch/$s_!tkSk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe576ab10-214d-4318-888b-34499462c1a1_3840x1356.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">In this case, On is <code>100</code> and Off is <code>101</code></figcaption></figure></div><p>This isn&#8217;t always the desired behavior, but it can be fixed by explicitly adding the value of each element in the enum, such as having the value be a string.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tGP5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccfd5ecd-90ca-4daa-beb8-8a400b2210d9_3840x1356.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tGP5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccfd5ecd-90ca-4daa-beb8-8a400b2210d9_3840x1356.png 424w, https://substackcdn.com/image/fetch/$s_!tGP5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccfd5ecd-90ca-4daa-beb8-8a400b2210d9_3840x1356.png 848w, https://substackcdn.com/image/fetch/$s_!tGP5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccfd5ecd-90ca-4daa-beb8-8a400b2210d9_3840x1356.png 1272w, https://substackcdn.com/image/fetch/$s_!tGP5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccfd5ecd-90ca-4daa-beb8-8a400b2210d9_3840x1356.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tGP5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccfd5ecd-90ca-4daa-beb8-8a400b2210d9_3840x1356.png" width="1456" height="514" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ccfd5ecd-90ca-4daa-beb8-8a400b2210d9_3840x1356.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:514,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:222128,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tGP5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccfd5ecd-90ca-4daa-beb8-8a400b2210d9_3840x1356.png 424w, https://substackcdn.com/image/fetch/$s_!tGP5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccfd5ecd-90ca-4daa-beb8-8a400b2210d9_3840x1356.png 848w, https://substackcdn.com/image/fetch/$s_!tGP5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccfd5ecd-90ca-4daa-beb8-8a400b2210d9_3840x1356.png 1272w, https://substackcdn.com/image/fetch/$s_!tGP5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fccfd5ecd-90ca-4daa-beb8-8a400b2210d9_3840x1356.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">String Enums</figcaption></figure></div><p>Or even an expression, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!n5hD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b2d4089-87e3-4dfc-a57c-f404e7cd08af_2596x1380.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!n5hD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b2d4089-87e3-4dfc-a57c-f404e7cd08af_2596x1380.png 424w, https://substackcdn.com/image/fetch/$s_!n5hD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b2d4089-87e3-4dfc-a57c-f404e7cd08af_2596x1380.png 848w, https://substackcdn.com/image/fetch/$s_!n5hD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b2d4089-87e3-4dfc-a57c-f404e7cd08af_2596x1380.png 1272w, https://substackcdn.com/image/fetch/$s_!n5hD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b2d4089-87e3-4dfc-a57c-f404e7cd08af_2596x1380.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!n5hD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b2d4089-87e3-4dfc-a57c-f404e7cd08af_2596x1380.png" width="1456" height="774" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3b2d4089-87e3-4dfc-a57c-f404e7cd08af_2596x1380.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:774,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:140687,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!n5hD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b2d4089-87e3-4dfc-a57c-f404e7cd08af_2596x1380.png 424w, https://substackcdn.com/image/fetch/$s_!n5hD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b2d4089-87e3-4dfc-a57c-f404e7cd08af_2596x1380.png 848w, https://substackcdn.com/image/fetch/$s_!n5hD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b2d4089-87e3-4dfc-a57c-f404e7cd08af_2596x1380.png 1272w, https://substackcdn.com/image/fetch/$s_!n5hD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3b2d4089-87e3-4dfc-a57c-f404e7cd08af_2596x1380.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Computed Enums</figcaption></figure></div><div><hr></div><p><strong>If you like my content and want to support my work, please consider supporting me (</strong><em><strong>you can buy me a double latte, the juice that powers my creativity and determination &#9749;&#65039;</strong></em><strong>) through <a href="https://github.com/sponsors/mainawycliffe/">Github Sponsors</a>.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://github.com/sponsors/mainawycliffe/&quot;,&quot;text&quot;:&quot;&#9749;&#65039; Consider buying me a cup of coffee&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://github.com/sponsors/mainawycliffe/"><span>&#9749;&#65039; Consider buying me a cup of coffee</span></a></p><div><hr></div><h3>Const Enums</h3><p>This isn&#8217;t always desired, and that's where const enums come in. They are just like regular enums, but they are prefixed by the const keyword before their declaration. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1REU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa486d3c-7fe9-4894-bf1e-6a0ee11c37e0_2596x972.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1REU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa486d3c-7fe9-4894-bf1e-6a0ee11c37e0_2596x972.png 424w, https://substackcdn.com/image/fetch/$s_!1REU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa486d3c-7fe9-4894-bf1e-6a0ee11c37e0_2596x972.png 848w, https://substackcdn.com/image/fetch/$s_!1REU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa486d3c-7fe9-4894-bf1e-6a0ee11c37e0_2596x972.png 1272w, https://substackcdn.com/image/fetch/$s_!1REU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa486d3c-7fe9-4894-bf1e-6a0ee11c37e0_2596x972.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1REU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa486d3c-7fe9-4894-bf1e-6a0ee11c37e0_2596x972.png" width="1456" height="545" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/aa486d3c-7fe9-4894-bf1e-6a0ee11c37e0_2596x972.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:545,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:122597,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1REU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa486d3c-7fe9-4894-bf1e-6a0ee11c37e0_2596x972.png 424w, https://substackcdn.com/image/fetch/$s_!1REU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa486d3c-7fe9-4894-bf1e-6a0ee11c37e0_2596x972.png 848w, https://substackcdn.com/image/fetch/$s_!1REU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa486d3c-7fe9-4894-bf1e-6a0ee11c37e0_2596x972.png 1272w, https://substackcdn.com/image/fetch/$s_!1REU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa486d3c-7fe9-4894-bf1e-6a0ee11c37e0_2596x972.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The other important difference is that const enum values get inlined instead of producing an object, taking us back to zero impact on the JS runtime, as const enums do not generate objects after transpilation.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qEcI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff77637d1-4c90-4ff2-b540-6bcb14a05328_3840x2020.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qEcI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff77637d1-4c90-4ff2-b540-6bcb14a05328_3840x2020.png 424w, https://substackcdn.com/image/fetch/$s_!qEcI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff77637d1-4c90-4ff2-b540-6bcb14a05328_3840x2020.png 848w, https://substackcdn.com/image/fetch/$s_!qEcI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff77637d1-4c90-4ff2-b540-6bcb14a05328_3840x2020.png 1272w, https://substackcdn.com/image/fetch/$s_!qEcI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff77637d1-4c90-4ff2-b540-6bcb14a05328_3840x2020.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qEcI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff77637d1-4c90-4ff2-b540-6bcb14a05328_3840x2020.png" width="1456" height="766" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f77637d1-4c90-4ff2-b540-6bcb14a05328_3840x2020.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:766,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:380366,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qEcI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff77637d1-4c90-4ff2-b540-6bcb14a05328_3840x2020.png 424w, https://substackcdn.com/image/fetch/$s_!qEcI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff77637d1-4c90-4ff2-b540-6bcb14a05328_3840x2020.png 848w, https://substackcdn.com/image/fetch/$s_!qEcI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff77637d1-4c90-4ff2-b540-6bcb14a05328_3840x2020.png 1272w, https://substackcdn.com/image/fetch/$s_!qEcI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff77637d1-4c90-4ff2-b540-6bcb14a05328_3840x2020.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Generally speaking, regular enums are valid solutions, but in those cases where you don&#8217;t want to pay the penalty of extra code generated by using regular enums, const enums are a very good solution, as they don&#8217;t generate any code for their support.</p><p>As you can see, no more <code>Switch</code> object is being created. Of course, just like with regular enums, we still have the same issue of enum element values being incremental numbers unless otherwise specified.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!cPKU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0717c12b-6555-4618-a803-7da73dea4c5d_3840x2020.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!cPKU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0717c12b-6555-4618-a803-7da73dea4c5d_3840x2020.png 424w, https://substackcdn.com/image/fetch/$s_!cPKU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0717c12b-6555-4618-a803-7da73dea4c5d_3840x2020.png 848w, https://substackcdn.com/image/fetch/$s_!cPKU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0717c12b-6555-4618-a803-7da73dea4c5d_3840x2020.png 1272w, https://substackcdn.com/image/fetch/$s_!cPKU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0717c12b-6555-4618-a803-7da73dea4c5d_3840x2020.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!cPKU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0717c12b-6555-4618-a803-7da73dea4c5d_3840x2020.png" width="1456" height="766" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0717c12b-6555-4618-a803-7da73dea4c5d_3840x2020.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:766,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:408945,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!cPKU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0717c12b-6555-4618-a803-7da73dea4c5d_3840x2020.png 424w, https://substackcdn.com/image/fetch/$s_!cPKU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0717c12b-6555-4618-a803-7da73dea4c5d_3840x2020.png 848w, https://substackcdn.com/image/fetch/$s_!cPKU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0717c12b-6555-4618-a803-7da73dea4c5d_3840x2020.png 1272w, https://substackcdn.com/image/fetch/$s_!cPKU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0717c12b-6555-4618-a803-7da73dea4c5d_3840x2020.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Conclusion</h3><p>In this issue, we examined the difference between enums and const enums. Unlike regular enums, const enums don&#8217;t generate the javascript object; the code is inlined. This means that you are not paying the penalty for extra code generation that you get when using regular enums. </p><p>While this is normally not a concern for most projects, in some cases, it can have a negative impact, leading to large JS code being produced and longer build / transpilation times. This can easily be solved by using const enums, which would be easier to refactor compared to alternative solutions such as unions.</p><div><hr></div><p>That's it for this issue. Thank you for getting this far. If you enjoyed this article and would like to support my work, please share and like this issue and consider sharing All Things Typescript with friends and colleagues.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&quot;,&quot;text&quot;:&quot;Share All Things Typescript&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share"><span>Share All Things Typescript</span></a></p><p>Did you know you can hire me to coach your team and help them improve their Typescript skills? If so, please contact me to discuss it.</p><div class="directMessage button" data-attrs="{&quot;userId&quot;:43214086,&quot;userName&quot;:&quot;Maina Wycliffe&quot;,&quot;canDm&quot;:null,&quot;dmUpgradeOptions&quot;:null,&quot;isEditorNode&quot;:true}" data-component-name="DirectMessageToDOM"></div><p>And until next time, please keep on learning.</p>]]></content:encoded></item><item><title><![CDATA[Schema First Type Design in Typescript]]></title><description><![CDATA[In this issue, I lay down my thoughts and ideas on the concept of schema first design approach in Typescript, where we generate our Typescript types from schemas which act as a single source of Truth.]]></description><link>https://www.allthingstypescript.dev/p/schema-first-type-design-in-typescript</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/schema-first-type-design-in-typescript</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 20 May 2024 05:51:47 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/9b9aec0f-0f62-4147-a27f-59163af9e84d_3840x2160.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>For a while now, I have meant to write about this issue to collect and convey my thoughts on Schema First Type design in Typescript. I will be giving this talk for the This is Learning virtual conference, and I thought it would be amazing to have an issue accompanying the talk&#8212;killing two birds with one stone.</p><p>I have also covered this topic in the relationship between two libraries - Valibot and Zod- and this will be a broader look, as we can achieve this with a lot more.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><p>So, without further ado, let&#8217;s get going:</p><h3>Schema-first type design?</h3><p>Instead of designing our types in Typescript, this idea is to start somewhere else and then generate static types. </p><p>Typescript types, generally, represent the state of our data, defining what we expect our data to be. So, for instance, if we had a list of users, we expected to have a type describing our user with all the fields we needed for that specific user. This is the data that Typescript takes, and type checks out code against it.</p><p>The one issue with Typescript is that during the transpilation/compilation (choose your poison) process, these Types are stripped away as Typescript gets transpiled to Javascript. This also means we don&#8217;t have access to the type of definition we spent a lot of time designing at runtime.</p><p>So, if we need runtime validation, such as user input validation, we would need a different tool to achieve that. Validating user input and external data is important; we have tools like data validation schemas to help us with this.</p><h3>Problem?</h3><p>So, to recap, we have Types and schema validation schemas that basically define the same data. One is used for type-checking, while the other is used for data validation at runtime. </p><p>This is a lot of duplication, and it can lead to a situation where our schemas and our types are out of sync as your codebase scales, which could have unforeseen consequences.</p><div><hr></div><p><strong>If you like my content and want to support my work, please consider supporting me (</strong><em><strong>you can buy me a double latte, the juice that powers my creativity and determination &#9749;&#65039;</strong></em><strong>) through <a href="https://github.com/sponsors/mainawycliffe/">Github Sponsors</a>.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://github.com/sponsors/mainawycliffe/&quot;,&quot;text&quot;:&quot;&#9749;&#65039; Consider buying me a cup of coffee&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://github.com/sponsors/mainawycliffe/"><span>&#9749;&#65039; Consider buying me a cup of coffee</span></a></p><div><hr></div><h3>Solution</h3><p>So, what are the solutions? What if we could start validation schemas and then generate a typescript? Typescript schemas have several benefits, such as being more detailed and fine-grained. </p><p>With schemas, we can check data that fits a certain pattern, such as email, phone, etc., among other checks, which is difficult to achieve with different types. So, the only thing we need to do is generate static types.</p><p>We have a few options for schema tools that we can use as our single source of truth for our Typescript types. In this issue, I will briefly examine GraphQL and Zod/Valibot as alternatives to GraphQL.</p><h4>GraphQL</h4><p>GraphQL is strongly typed and can be a great source of truth for describing data for our API. It has strong data validation built in and can be extended. While we can&#8217;t use a GraphQL schema directly for types, we can  use it as a basis to generate Typescript types that we can use within our application, reducing code duplication and having a single source of truth. </p><p>This can be achieved using tools such as <a href="https://the-guild.dev/graphql/codegen">graphql-codegen</a>; we can generate types and add more annotations to the schema to provide better types that reflect what we want our types to look like but centrally located.</p><p>In addition, you can use the GraphQL code generation tool to generate JSON schemas for data and form validation. This would allow us to achieve a single source of truth for our application and not worry about the effects of code duplication we get by maintaining different schemas and types.</p><h4>Schema Data Validation Libraries - Zod and Valibot</h4><p>While GraphQL is great, not everyone uses it. Zod and Valibot allow us to define schema validation schemas for data validation in our application. We can also get static types from both, which Typescript can use for type-checking within your application.</p><p>Here is an example of me using Zod to get Typescript Types from the schema.</p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;9eca7a0c-fbb3-4d7f-a565-615a484972b4&quot;,&quot;duration&quot;:null}"></div><p>The beauty of the above code snippet is that we can use it to validate data at runtime in places like forms. On top of that, by parsing the data, we get automatic type narrowing. Zod will throw an error if it can&#8217;t validate the data&#8212;remember to handle that.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ahWC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeeaf4f2-be35-42a9-ac9b-b2ae83c1fe2e_3572x1872.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ahWC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeeaf4f2-be35-42a9-ac9b-b2ae83c1fe2e_3572x1872.png 424w, https://substackcdn.com/image/fetch/$s_!ahWC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeeaf4f2-be35-42a9-ac9b-b2ae83c1fe2e_3572x1872.png 848w, https://substackcdn.com/image/fetch/$s_!ahWC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeeaf4f2-be35-42a9-ac9b-b2ae83c1fe2e_3572x1872.png 1272w, https://substackcdn.com/image/fetch/$s_!ahWC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeeaf4f2-be35-42a9-ac9b-b2ae83c1fe2e_3572x1872.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ahWC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeeaf4f2-be35-42a9-ac9b-b2ae83c1fe2e_3572x1872.png" width="1456" height="763" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/deeaf4f2-be35-42a9-ac9b-b2ae83c1fe2e_3572x1872.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:763,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:981691,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ahWC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeeaf4f2-be35-42a9-ac9b-b2ae83c1fe2e_3572x1872.png 424w, https://substackcdn.com/image/fetch/$s_!ahWC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeeaf4f2-be35-42a9-ac9b-b2ae83c1fe2e_3572x1872.png 848w, https://substackcdn.com/image/fetch/$s_!ahWC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeeaf4f2-be35-42a9-ac9b-b2ae83c1fe2e_3572x1872.png 1272w, https://substackcdn.com/image/fetch/$s_!ahWC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeeaf4f2-be35-42a9-ac9b-b2ae83c1fe2e_3572x1872.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The same can be achieved using Valibot, which I covered in a previous issue for All Things Typescript earlier this year.</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;89bab37b-57e2-4269-94eb-3d912ae14ed8&quot;,&quot;caption&quot;:&quot;Let&#8217;s start with a simple hypothesis. Let&#8217;s say you have a form for your users, for instance, a sign-in form. You need a validation schema and a Typescript type, representing the shape of the form data. Why a validation schema, you may ask? This is because data coming from untrusted sources should always be validated, for instance, form data, ensuring t&#8230;&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Schema First Typescript Types Design with Valibot&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-02-12T06:19:15.080Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7b449bb1-5363-415b-8f8b-01ccd839aa96_1280x853.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/schema-first-typescript-types-design&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:141422661,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:2,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>Zod and Valibot are not the only two tools that can help you achieve this, but for this issue, I wanted to convey an idea and am not confident enough to recommend tools that I have tried and used previously. Still, there are a lot of good options out there for you to consider, and I will be taking a look at each in upcoming issues.</p><h3>Conclusion</h3><p>In this issue, I laid down my thoughts on the concept of schema first design in Typescript. This is a concept where instead of designing Typescript types ourselves, we start with a schema such as a GraphQL schema or a data validation schemas and generic static types form them. This reduces code duplication and ensures we have a single source of truth for our types, ensuring that our code scales in the future.</p><p>Let me know what you think in the comment section below.</p><div><hr></div><p>That's it for this issue. Thank you for getting this far. If you enjoyed this article and would like to support my work, please share and like this issue and consider sharing All Things Typescript with friends and colleagues.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&quot;,&quot;text&quot;:&quot;Share All Things Typescript&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share"><span>Share All Things Typescript</span></a></p><p>Did you know you can also hire me to coach your team and help them improve their Typescript skills? If this sounds interesting to you, please consider getting in touch so we can work something out.</p><div class="directMessage button" data-attrs="{&quot;userId&quot;:43214086,&quot;userName&quot;:&quot;Maina Wycliffe&quot;,&quot;canDm&quot;:null,&quot;dmUpgradeOptions&quot;:null,&quot;isEditorNode&quot;:true}" data-component-name="DirectMessageToDOM"></div><p>And until next time, please keep on learning.</p>]]></content:encoded></item><item><title><![CDATA[April 2024 Monthly Newsletter - TypeScript 5.5 Beta with a huge feature]]></title><description><![CDATA[In April we saw the release of Typescript 5.5, and on top of that, the Deno team announced a JavaScript Registry (JSR) that's optimized for Typescript and JavaScript]]></description><link>https://www.allthingstypescript.dev/p/april-2024-monthly-newsletter-typescript</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/april-2024-monthly-newsletter-typescript</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 06 May 2024 06:17:59 GMT</pubDate><enclosure url="https://substackcdn.com/image/youtube/w_728,c_limit/ugeQfRdzLRk" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey, Everyone. Another month has passed, and today is the day I look back on informative content I came across last month and news related to Typescript.</p><p>My name is Maina Wycliffe, and I love teaching Typescript to developers. My goal is to break down complex topics into small, understandable bits that you can apply in your day-to-day life as a developer.</p><h2>My First Typescript Workshop</h2><p>I am excited to announce that I am working on a series of virtual Typescript workshops. The first will be in a couple of months&#8212;either later in July or early August.</p><p>The first workshop I will be doing is about a topic I am very passionate about - Schema first-type design in Typescript (I will be doing a talk in relationship to this next week for the <a href="https://til-conf.netlify.app/">This is Learning virtual conference</a> (please join). </p><p>I aim to teach how to use tools such as <a href="https://www.allthingstypescript.dev/p/using-zod-schemas-as-source-of-truth?utm_source=publication-search">Zod</a> and <a href="https://www.allthingstypescript.dev/p/schema-first-typescript-types-design">Valibot</a> to design schemas from which you can infer Typescript static. This will create a single source of truth together with static types.</p><p>Let me know if you are interested in joining my first workshop by answering the survey below:</p><div class="poll-embed" data-attrs="{&quot;id&quot;:172160}" data-component-name="PollToDOM"></div><p>I will send out a notification with details on the workshop and instructions on how to sign up.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><h2>Typescript 5.5 Beta Released</h2><p>The beta version of Typescript 5.5 was released last month, and I will review the features that I found rather interesting and exciting. </p><p>Some of the exciting improvements revolve around better type inference. For instance, one that I find personally exciting is control flow type narrowing when arrays are involved. </p><p>Currently, if you filter an array to, let&#8217;s say, remove undefined values inside an array, Typescript isn&#8217;t able to infer that the type needs to be narrowed.</p><p>This changes in the next version of Typescript, which can now narrow based on the control flow inside the filter method, as shown below.</p><div id="youtube2-ugeQfRdzLRk" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;ugeQfRdzLRk&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/ugeQfRdzLRk?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><p>This is exciting, isn&#8217;t it? Let me know if you want me to explore this further in a future issue of All Things Typescript.</p><p>Other notable features include:</p><ul><li><p>Import types into JS Docs (JS Files)</p></li><li><p>Type-checking in regular expressions</p></li></ul><p>And there is more, which you can learn by reading the announcement blog <a href="https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-beta/">here</a>.</p><h2>From All Things Typescript</h2><p>This month, I wrapped up the series around Generics and explored Typescript&#8217;s relationship with Javascript.</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;7bed9245-19ab-4164-a5ac-c559879dd6c0&quot;,&quot;caption&quot;:&quot;In the last two issues, we started exploring generics, where we looked at the fundamentals of generics and how to create generics constraints. I believe this has given you a good foundation for working with generics in Typescript. You can find the last two issues below:&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Making Generics Types Optional - Generics Fundamentals in Typescript&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-04-08T07:56:26.548Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7fcf7dc3-4ac4-41ea-a600-63a30d272b3f_3980x2184.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/making-generics-types-optional-generics&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:142908175,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:4,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;15d1a297-af5f-42de-a510-0cf35a71bb22&quot;,&quot;caption&quot;:&quot;The first draft of this issue was written almost a year ago, in a hotel room in Amsterdam. I didn&#8217;t think I was going to be publishing, but I thought why not? Any, I digress, I hope you are going to enjoy it. For a while now, I have been to write an issue that explores the relationship between Typescript and Javascript. While Javascript can exist without&#8230;&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Typescript and JavaScript - Two Sides of the Same Coin&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-04-15T05:10:38.049Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/78c5da7e-2506-4579-b81d-8b845132742f_5904x3320.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/typescript-and-javascript-two-sides&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:129113790,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:2,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;dea07c1d-006b-49e4-832a-6e5651a4dba0&quot;,&quot;caption&quot;:&quot;One important mental model to form in Typescript is the relationship between types and their subtypes. Typescript is based on structural sub-typing, where when type-checking whether one type can be assigned to another, it checks on the members/properties present, and if all members are present, it allows the assignment.&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Types and Subtypes Relationship in Typescript &quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-04-29T06:45:44.351Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6fafa3df-4b63-4f51-b279-91a600992102_3840x2160.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/types-and-subtypes-relationship-in&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:113714535,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:5,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div><hr></div><p><strong>If you like my content and want to support my work, please consider supporting me (</strong><em><strong>you can buy me a double latte, the juice that powers my creativity and determination &#9749;&#65039;</strong></em><strong>) through <a href="https://github.com/sponsors/mainawycliffe/">Github Sponsors</a>.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://github.com/sponsors/mainawycliffe/&quot;,&quot;text&quot;:&quot;&#9749;&#65039; Consider buying me a cup of coffee&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://github.com/sponsors/mainawycliffe/"><span>&#9749;&#65039; Consider buying me a cup of coffee</span></a></p><div><hr></div><h2>From Other Authors</h2><h4><a href="https://deno.com/blog/intro-to-tsconfig">An intro to TSConfig for JavaScript Developers by Jo Franchetti</a></h4><p>Have you ever wanted to understand the tsconfig. Jo Franchetti goes over the tsconfig, the different settings available, and its capabilities in this post.</p><p><a href="https://deno.com/blog/intro-to-tsconfig">Link</a></p><h4><a href="https://deno.com/blog/jsr_open_beta">Introducing JSR - the JavaScript Registry</a></h4><p>The Deno team introduced a new Javascript Registry optimized for Typescript and ESM Modules. In a series of posts, the registry discusses what JSR is and isn&#8217;t and why you should consider using it.</p><p>Here is a list of the most interesting blog posts about the registry from the Deno team.</p><ul><li><p>I<a href="https://deno.com/blog/jsr_open_beta">ntroducing JSR - the JavaScript Registry</a></p></li><li><p><a href="https://deno.com/blog/how-we-built-jsr?ref=dailydev">How we built JSR</a></p></li><li><p><a href="https://deno.com/blog/jsr-is-not-another-package-manager">JSR Is Not Another Package Manager</a></p></li></ul><h4><a href="https://blog.jetbrains.com/qodana/2024/04/improving-code-quality-in-javascript-projects-with-qodana/">Improving Code Quality in JavaScript Projects With Qodana by Maksim Grushchenko and Kerry Beetge</a></h4><p>JetBrains Qodana is a powerful static code analysis platform that enables teams to analyze code quality and find issues before runtime. This article discusses how to utilize Qodan to improve code quality for JS projects.</p><p><a href="https://blog.jetbrains.com/qodana/2024/04/improving-code-quality-in-javascript-projects-with-qodana/">Link</a></p><h4><a href="https://dev.to/symplymuslimah12/understanding-use-strict-in-javascript-4ik3">Understanding 'Use strict' in JavaScript by Muslimat Mojeed</a></h4><p>Strict mode is a special mode in JavaScript that allows developers to opt into a set of rules and restrictions that are not enforced by default.<br>Strict mode in JavaScript is like having a helpful friend who keeps an eye out for mistakes while you're coding.</p><p><a href="https://dev.to/symplymuslimah12/understanding-use-strict-in-javascript-4ik3">Link</a></p><h4><a href="https://www.danielfullstack.com/article/this-typescript-trick-will-blow-your-mind">This TypeScript Trick Will Blow Your Mind by Daniel Craciun</a></h4><p>Daniel Craciun takes us through extracting types from arrays by creating a custom utility type.</p><p><a href="https://www.danielfullstack.com/article/this-typescript-trick-will-blow-your-mind">Link</a></p><h4><a href="https://frontendmasters.com/blog/node-js-debugging-in-chrome-devtools/">Node.js Debugging in Chrome DevTools by Frontend Masters</a></h4><p>Learn how you can utilize Chrome DevTools to debut NodeJS and Javascript applications.</p><p><a href="https://frontendmasters.com/blog/node-js-debugging-in-chrome-devtools/">Link</a></p><div><hr></div><p>That's it for this issue. Thank you for getting this far. If you enjoyed this article and would like to support my work, please share and like this issue and consider sharing All Things Typescript with friends and colleagues.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&quot;,&quot;text&quot;:&quot;Share All Things Typescript&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share"><span>Share All Things Typescript</span></a></p><p>Did you know you can hire me to coach your team and help them improve their Typescript skills? If this interests you, please contact me to discuss it.</p><div class="directMessage button" data-attrs="{&quot;userId&quot;:43214086,&quot;userName&quot;:&quot;Maina Wycliffe&quot;,&quot;canDm&quot;:null,&quot;dmUpgradeOptions&quot;:null,&quot;isEditorNode&quot;:true}" data-component-name="DirectMessageToDOM"></div><p>And until next time, please keep on learning.</p>]]></content:encoded></item><item><title><![CDATA[Types and Subtypes Relationship in Typescript ]]></title><description><![CDATA[In this issue, we explore the relationship between types and their subtypes in Typescript.]]></description><link>https://www.allthingstypescript.dev/p/types-and-subtypes-relationship-in</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/types-and-subtypes-relationship-in</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 29 Apr 2024 06:45:44 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/6fafa3df-4b63-4f51-b279-91a600992102_3840x2160.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>One important mental model to form in Typescript is the relationship between types and their subtypes. Typescript is based on structural sub-typing, where when type-checking whether one type can be assigned to another, it checks on the members/properties present, and if all members are present, it allows the assignment.</p><p>This is unlike nominal typing, where type compatibility and equivalence are based on the type declaration. For instance, in the eyes of Typescript, the two types below are the same:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Bgg6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b6bd00-6310-4a25-aff6-bbb8853a0350_3276x1184.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Bgg6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b6bd00-6310-4a25-aff6-bbb8853a0350_3276x1184.png 424w, https://substackcdn.com/image/fetch/$s_!Bgg6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b6bd00-6310-4a25-aff6-bbb8853a0350_3276x1184.png 848w, https://substackcdn.com/image/fetch/$s_!Bgg6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b6bd00-6310-4a25-aff6-bbb8853a0350_3276x1184.png 1272w, https://substackcdn.com/image/fetch/$s_!Bgg6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b6bd00-6310-4a25-aff6-bbb8853a0350_3276x1184.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Bgg6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b6bd00-6310-4a25-aff6-bbb8853a0350_3276x1184.png" width="1456" height="526" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/20b6bd00-6310-4a25-aff6-bbb8853a0350_3276x1184.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:526,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:715673,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Bgg6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b6bd00-6310-4a25-aff6-bbb8853a0350_3276x1184.png 424w, https://substackcdn.com/image/fetch/$s_!Bgg6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b6bd00-6310-4a25-aff6-bbb8853a0350_3276x1184.png 848w, https://substackcdn.com/image/fetch/$s_!Bgg6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b6bd00-6310-4a25-aff6-bbb8853a0350_3276x1184.png 1272w, https://substackcdn.com/image/fetch/$s_!Bgg6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F20b6bd00-6310-4a25-aff6-bbb8853a0350_3276x1184.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You can learn more about structural typing <a href="https://www.allthingstypescript.dev/p/if-it-looks-like-a-duck-walks-like">here</a>.</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;a73cb6db-590e-48a6-8ff0-1180cd2e57a4&quot;,&quot;caption&quot;:&quot;Good morning; I hope you had a fantastic weekend. On a personal note, I had a fantastic week last week, and I am very excited to say that I am now a Google Developer Expert for Angular.&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;If it looks like a duck, walks like a duck, quack like a duck... All Things Typescript Newsletter - Issue #21&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2022-08-29T07:32:19.370Z&quot;,&quot;cover_image&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/f76dccbd-aaf0-40f5-b34d-e753523caa0d_1920x1200.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/if-it-looks-like-a-duck-walks-like&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:70727987,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:1,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:false,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>I covered this a while ago, and I wanted to explore sub-types in Typescript in this issue. Unlike the above example, where all properties are the same across the two types, one Type has more members/properties than the other, i.e., is a sub-type of the other. Due to structural typing, the two Types have a relationship, which we will explore in this issue.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><div><hr></div><p>We are going to start with looking at an examples:</p><p>Take the following string literal type. </p><pre><code>type One = "One";</code></pre><p>The one <code>One</code> literal type is a subtype of string and can be used in any place where the input of type <code>one</code>  (keep in mind about <a href="https://www.allthingstypescript.dev/p/if-it-looks-like-a-duck-walks-like">structural typing</a>) or a string is required, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8PG7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6cc2be3a-57d4-4265-aeb1-ec0743cee388_3360x1956.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8PG7!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6cc2be3a-57d4-4265-aeb1-ec0743cee388_3360x1956.png 424w, https://substackcdn.com/image/fetch/$s_!8PG7!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6cc2be3a-57d4-4265-aeb1-ec0743cee388_3360x1956.png 848w, https://substackcdn.com/image/fetch/$s_!8PG7!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6cc2be3a-57d4-4265-aeb1-ec0743cee388_3360x1956.png 1272w, https://substackcdn.com/image/fetch/$s_!8PG7!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6cc2be3a-57d4-4265-aeb1-ec0743cee388_3360x1956.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8PG7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6cc2be3a-57d4-4265-aeb1-ec0743cee388_3360x1956.png" width="1456" height="848" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6cc2be3a-57d4-4265-aeb1-ec0743cee388_3360x1956.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:848,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1450254,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8PG7!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6cc2be3a-57d4-4265-aeb1-ec0743cee388_3360x1956.png 424w, https://substackcdn.com/image/fetch/$s_!8PG7!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6cc2be3a-57d4-4265-aeb1-ec0743cee388_3360x1956.png 848w, https://substackcdn.com/image/fetch/$s_!8PG7!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6cc2be3a-57d4-4265-aeb1-ec0743cee388_3360x1956.png 1272w, https://substackcdn.com/image/fetch/$s_!8PG7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6cc2be3a-57d4-4265-aeb1-ec0743cee388_3360x1956.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>However, if we used a string instead of a variable of type <code>One</code>, where <code>One</code> is required, then it would throw an error:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fIPD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ea38f8-c4ef-43dd-9e8e-d9a24d9235ed_3604x1084.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fIPD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ea38f8-c4ef-43dd-9e8e-d9a24d9235ed_3604x1084.png 424w, https://substackcdn.com/image/fetch/$s_!fIPD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ea38f8-c4ef-43dd-9e8e-d9a24d9235ed_3604x1084.png 848w, https://substackcdn.com/image/fetch/$s_!fIPD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ea38f8-c4ef-43dd-9e8e-d9a24d9235ed_3604x1084.png 1272w, https://substackcdn.com/image/fetch/$s_!fIPD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ea38f8-c4ef-43dd-9e8e-d9a24d9235ed_3604x1084.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fIPD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ea38f8-c4ef-43dd-9e8e-d9a24d9235ed_3604x1084.png" width="1456" height="438" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/36ea38f8-c4ef-43dd-9e8e-d9a24d9235ed_3604x1084.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:438,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:703825,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fIPD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ea38f8-c4ef-43dd-9e8e-d9a24d9235ed_3604x1084.png 424w, https://substackcdn.com/image/fetch/$s_!fIPD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ea38f8-c4ef-43dd-9e8e-d9a24d9235ed_3604x1084.png 848w, https://substackcdn.com/image/fetch/$s_!fIPD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ea38f8-c4ef-43dd-9e8e-d9a24d9235ed_3604x1084.png 1272w, https://substackcdn.com/image/fetch/$s_!fIPD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F36ea38f8-c4ef-43dd-9e8e-d9a24d9235ed_3604x1084.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This means that the type capability between the <code>One</code> type and string is one way; only <code>One</code> can be assigned to the string and not vice versa.</p><div class="pullquote"><p><strong>&#8220;A sub-type can be assigned to a variable of a type that the sub-type is derived from, but not vice versa.&#8221;</strong></p></div><p>It is important to understand that the relationship between a Type and its subtype is hierarchical, where a subtype is always narrower than the Type.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oHUe!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348af059-43fd-4f1e-9c0b-a18fc6e08e06_1280x800.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oHUe!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348af059-43fd-4f1e-9c0b-a18fc6e08e06_1280x800.jpeg 424w, https://substackcdn.com/image/fetch/$s_!oHUe!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348af059-43fd-4f1e-9c0b-a18fc6e08e06_1280x800.jpeg 848w, https://substackcdn.com/image/fetch/$s_!oHUe!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348af059-43fd-4f1e-9c0b-a18fc6e08e06_1280x800.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!oHUe!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348af059-43fd-4f1e-9c0b-a18fc6e08e06_1280x800.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oHUe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348af059-43fd-4f1e-9c0b-a18fc6e08e06_1280x800.jpeg" width="1280" height="800" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/348af059-43fd-4f1e-9c0b-a18fc6e08e06_1280x800.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:800,&quot;width&quot;:1280,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:179595,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!oHUe!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348af059-43fd-4f1e-9c0b-a18fc6e08e06_1280x800.jpeg 424w, https://substackcdn.com/image/fetch/$s_!oHUe!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348af059-43fd-4f1e-9c0b-a18fc6e08e06_1280x800.jpeg 848w, https://substackcdn.com/image/fetch/$s_!oHUe!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348af059-43fd-4f1e-9c0b-a18fc6e08e06_1280x800.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!oHUe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F348af059-43fd-4f1e-9c0b-a18fc6e08e06_1280x800.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The relationship between types and subtypes is hierarchical</figcaption></figure></div><p>Understanding this is important, as it allows you to form a better mental model of how the Typescript Type system works, especially type compatibility and structural typing in Typescript. </p><p>Another important reason to understand this is that it allows you to make your type more precise and narrower. Narrower types lead to increased type safety. For instance, a string literal type is always more precise than a string, and just because a function requires a string as an input, it doesn&#8217;t mean your variable needs to be a string; instead, you can prefer more narrow string literal types.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!nJ1O!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2fefcf3b-f5c5-4b8f-b008-082230af4571_3840x2160.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nJ1O!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2fefcf3b-f5c5-4b8f-b008-082230af4571_3840x2160.png 424w, https://substackcdn.com/image/fetch/$s_!nJ1O!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2fefcf3b-f5c5-4b8f-b008-082230af4571_3840x2160.png 848w, https://substackcdn.com/image/fetch/$s_!nJ1O!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2fefcf3b-f5c5-4b8f-b008-082230af4571_3840x2160.png 1272w, https://substackcdn.com/image/fetch/$s_!nJ1O!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2fefcf3b-f5c5-4b8f-b008-082230af4571_3840x2160.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!nJ1O!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2fefcf3b-f5c5-4b8f-b008-082230af4571_3840x2160.png" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2fefcf3b-f5c5-4b8f-b008-082230af4571_3840x2160.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1630608,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!nJ1O!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2fefcf3b-f5c5-4b8f-b008-082230af4571_3840x2160.png 424w, https://substackcdn.com/image/fetch/$s_!nJ1O!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2fefcf3b-f5c5-4b8f-b008-082230af4571_3840x2160.png 848w, https://substackcdn.com/image/fetch/$s_!nJ1O!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2fefcf3b-f5c5-4b8f-b008-082230af4571_3840x2160.png 1272w, https://substackcdn.com/image/fetch/$s_!nJ1O!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2fefcf3b-f5c5-4b8f-b008-082230af4571_3840x2160.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>A narrower version of a string is always better than a string, as a string could be anything from a single character to a whole book.</p><h3>Conclusion </h3><p>In this issue of All Things Typescript, we looked at subtypes in Typescript. Type compatibility in Typescript is based on structural sub-typing, where related types are compared solely by their members (<em>the content of a type</em>). </p><p>In this issue, we looked at subtypes in Typescript, how they relate to their parents&#8217; type, and how assigning subtypes to parent types is possible, while vice versa isn&#8217;t.</p><div><hr></div><p>That's it for this issue. Thank you for getting this far. If you enjoyed this article and would like to support my work, please share and like this issue and consider sharing All Things Typescript with friends and colleagues.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&quot;,&quot;text&quot;:&quot;Share All Things Typescript&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share"><span>Share All Things Typescript</span></a></p><p>Did you know you can hire me to coach your team and help them improve their Typescript skills? If this interests you, please contact me to work something out.</p><div class="directMessage button" data-attrs="{&quot;userId&quot;:43214086,&quot;userName&quot;:&quot;Maina Wycliffe&quot;,&quot;canDm&quot;:null,&quot;dmUpgradeOptions&quot;:null,&quot;isEditorNode&quot;:true}" data-component-name="DirectMessageToDOM"></div><p>And until next time, please keep on learning.</p>]]></content:encoded></item><item><title><![CDATA[Typescript and JavaScript - Two Sides of the Same Coin]]></title><description><![CDATA[We briefly explore the relationship between JavaScript and Typescript and why understanding both Typescript and JavaScript.]]></description><link>https://www.allthingstypescript.dev/p/typescript-and-javascript-two-sides</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/typescript-and-javascript-two-sides</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 15 Apr 2024 05:10:38 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/78c5da7e-2506-4579-b81d-8b845132742f_5904x3320.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><em>The first draft of this issue was written almost a year ago, in a hotel room in Amsterdam. I didn&#8217;t think I was going to be publishing, but I thought why not? Any, I digress, I hope you are going to enjoy it.</em></p><p>For a while now, I have been to write an issue that explores the relationship between Typescript and Javascript. While Javascript can exist without Typescript, Typescript can&#8217;t (as of the time of writing this).</p><p>As web developers, understanding Javascript is vital to developing better mental models for working with Typescript and building kick-ass applications. </p><p>The <strong>key</strong> difference between Typescript and Javascript is the existence of static types. Typescript has placed itself as Javascript but with static types. Even visiting their website, it&#8217;s right there with bold and large text.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_dpZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0349300e-c22d-4606-bde2-8505f3c9ed54_1130x367.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_dpZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0349300e-c22d-4606-bde2-8505f3c9ed54_1130x367.png 424w, https://substackcdn.com/image/fetch/$s_!_dpZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0349300e-c22d-4606-bde2-8505f3c9ed54_1130x367.png 848w, https://substackcdn.com/image/fetch/$s_!_dpZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0349300e-c22d-4606-bde2-8505f3c9ed54_1130x367.png 1272w, https://substackcdn.com/image/fetch/$s_!_dpZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0349300e-c22d-4606-bde2-8505f3c9ed54_1130x367.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_dpZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0349300e-c22d-4606-bde2-8505f3c9ed54_1130x367.png" width="1130" height="367" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0349300e-c22d-4606-bde2-8505f3c9ed54_1130x367.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:367,&quot;width&quot;:1130,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:44058,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_dpZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0349300e-c22d-4606-bde2-8505f3c9ed54_1130x367.png 424w, https://substackcdn.com/image/fetch/$s_!_dpZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0349300e-c22d-4606-bde2-8505f3c9ed54_1130x367.png 848w, https://substackcdn.com/image/fetch/$s_!_dpZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0349300e-c22d-4606-bde2-8505f3c9ed54_1130x367.png 1272w, https://substackcdn.com/image/fetch/$s_!_dpZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0349300e-c22d-4606-bde2-8505f3c9ed54_1130x367.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Typescript has evolved to strive  for feature parity with Javascript, but this wasn&#8217;t always the case (ahem, decorators and enums anyone).</p><p>By providing only the typing system, means adding Typescript to your project should have zero (almost, if you use enums) runtime impact. This is because, when Typescript is compiled, all the types are stripped away (after the useful tasks of type-checking) and you are left with plain old Javascript.</p><div class="pullquote"><p>Every Typescript Develeper is a Javascript Developer. Typescript is just Javascript with Types - me &#129318;&#127998; (2024)</p></div><p>Understanding Typescript is just one side of the equation, where we get to learn how our code behaves up to the compilation/transpilation process. And this is where understanding Javascript is also important.</p><div class="pullquote"><p>&#8220;By defining our Types, we declare our intentions and Typescript checks whether our code matches our intention. This can be very useful for reducing a whole class of bugs.&#8221;</p></div><p>This means it&#8217;s important for us to know the behavior to expect from our application, during the runtime. This means understanding Javascript and its peculiarity. For those of us who have been around for a while, we may have come across the famous banana meme:</p><pre><code>("b" + "a" + + "a" + "a").toLowerCase()</code></pre><p>The expression above results with the string banana, as shown below (you can try it in your browser right now):</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7DxU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd15b0b18-d357-413e-a9b8-6088d1c067c8_508x81.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7DxU!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd15b0b18-d357-413e-a9b8-6088d1c067c8_508x81.png 424w, https://substackcdn.com/image/fetch/$s_!7DxU!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd15b0b18-d357-413e-a9b8-6088d1c067c8_508x81.png 848w, https://substackcdn.com/image/fetch/$s_!7DxU!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd15b0b18-d357-413e-a9b8-6088d1c067c8_508x81.png 1272w, https://substackcdn.com/image/fetch/$s_!7DxU!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd15b0b18-d357-413e-a9b8-6088d1c067c8_508x81.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7DxU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd15b0b18-d357-413e-a9b8-6088d1c067c8_508x81.png" width="508" height="81" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d15b0b18-d357-413e-a9b8-6088d1c067c8_508x81.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:81,&quot;width&quot;:508,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:12265,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7DxU!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd15b0b18-d357-413e-a9b8-6088d1c067c8_508x81.png 424w, https://substackcdn.com/image/fetch/$s_!7DxU!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd15b0b18-d357-413e-a9b8-6088d1c067c8_508x81.png 848w, https://substackcdn.com/image/fetch/$s_!7DxU!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd15b0b18-d357-413e-a9b8-6088d1c067c8_508x81.png 1272w, https://substackcdn.com/image/fetch/$s_!7DxU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd15b0b18-d357-413e-a9b8-6088d1c067c8_508x81.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The trick is that when you try to sum a string, with a number, it results in the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN">NaN</a> value, which is a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN">global value that represents not a number</a> in Javascript, hence where the <strong>NaN</strong> comes from in our expression, to complete baNaNa. Even the following expression will result in <code>banana</code> as the value of the string.</p><pre><code>("b" + "a" + + "not a banana" + "a").toLowerCase()</code></pre><p>As shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EN6d!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8f357f3-f624-41be-b4ea-04e8ee2cd71e_642x86.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EN6d!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8f357f3-f624-41be-b4ea-04e8ee2cd71e_642x86.png 424w, https://substackcdn.com/image/fetch/$s_!EN6d!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8f357f3-f624-41be-b4ea-04e8ee2cd71e_642x86.png 848w, https://substackcdn.com/image/fetch/$s_!EN6d!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8f357f3-f624-41be-b4ea-04e8ee2cd71e_642x86.png 1272w, https://substackcdn.com/image/fetch/$s_!EN6d!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8f357f3-f624-41be-b4ea-04e8ee2cd71e_642x86.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EN6d!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8f357f3-f624-41be-b4ea-04e8ee2cd71e_642x86.png" width="642" height="86" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f8f357f3-f624-41be-b4ea-04e8ee2cd71e_642x86.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:86,&quot;width&quot;:642,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:13681,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EN6d!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8f357f3-f624-41be-b4ea-04e8ee2cd71e_642x86.png 424w, https://substackcdn.com/image/fetch/$s_!EN6d!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8f357f3-f624-41be-b4ea-04e8ee2cd71e_642x86.png 848w, https://substackcdn.com/image/fetch/$s_!EN6d!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8f357f3-f624-41be-b4ea-04e8ee2cd71e_642x86.png 1272w, https://substackcdn.com/image/fetch/$s_!EN6d!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8f357f3-f624-41be-b4ea-04e8ee2cd71e_642x86.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The above is fun and games, but goes to show you about things you can get away with in Javascript. This can be problematic, as you don&#8217;t want to make the mistake of summing strings and numbers when you meant numbers. And these are some of the problems that Typescript looks to address.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><p>I know you may be wondering where I am heading with this, but let&#8217;s explore the typing system:</p><h3>The Typing System</h3><p>In languages, programming languages, there are multiple ways in which we can categorize a language typing system, which allows us to make sense of the Typing system, giving us a glimpse into the behavior of a language when both writing code or our code is running.  </p><p>Understanding the typing system of the language we are using can help us add some important guardrails to prevent bugs we can foresee.</p><p>There are two broad ways of looking at a language Type system:</p><h4>Static Typing Vs Dynamic Typing</h4><p>In this kind of type system classification, the variable types are either static, meaning that the type of a variable is known from when the beginning, at the point it&#8217;s defined (<em>whether explicitly, or implicitly - think of type inference here</em>). While in a Dynamic Typing System, the variables are assigned their types at runtime, the concept. </p><p>In static typing, the language requires types to be known upfront and during the build/compile step, it will type check your code to ensure type safety while dynamic typed languages do not.</p><h4>Strongly Typed vs Weakly Typed</h4><p>On top of being either static or dynamic, the type system of a language can either be strong or weak. A strong typing system doesn&#8217;t allow for the type of a variable to be changed when it&#8217;s assigned, either statically or dynamically, while a weak typing system, will allow the variables&#8217; type to change, and can take different types as the variables get data assigned to it.</p><p>With weakly typed languages, variable type changes implicitly during their life-cycle while in strongly typed, this doesn&#8217;t happen. </p><h4>Where do Javascript and Typescript fall in this category?</h4><p>As we have said previously, Javascript and Typescript are tied at the hip, while Typescript provides a static typing system to Javascript, just because Typescript doesn&#8217;t exist at runtime, it inherently borrows some of the characteristics of the Javascript typing system - the runtime behavior. Web developers working with Typescript need to understand this.</p><div class="pullquote"><p>When you work with Typescript, you are by extension working with Javascript, and understanding Javascript is vital.</p></div><p>But before I can go on and say, that <strong>Typescript is good, and javascript bad</strong> (<em>oh, I know, no one likes hyperbole</em>), let&#8217;s first look at them as independent languages, which is kind of hard when one compiles into the other one.</p><p>Typescript is statically typed - your variable types, even when using any have to be assigned when you define the variable - this can be done either explicitly or implicitly, relying on the inference system, but the end goal is known upfront.</p><p>You are also not allowed to change the type of variable once annotated. A few things to keep in mind here is the <code>any</code> type, which can be assigned to any variable and vice versa - I would encourage you to disable implicit any in <code>tsconfig.json</code> and in such cases, provide annotations for variables where Typescript can&#8217;t infer types explicitly, or manually. </p><p>On the other hand, Javascript is dynamically typed and weakly typed, meaning a variable&#8217;s Type is determined at runtime based on the value it&#8217;s assigned and that type can change during the lifetime of a variable.</p><p>Remember when I said, Typescript borrows some of the characteristics of the Javascript type system? The reason I said this is because, When we compile Typescript to Javascript, we lose everything and our runtime Type system is that of Javascript rather than Typescript's desired behavior.</p><p>It is important to know and understand this and why you cannot trust any data from external sources - outside your code. User input data and API data should be validated to ensure that you are working with data that matches your expectations. </p><p>And I would highly discourage using type assertions (<em>another topic, for a future All Things Typescript issue</em>), and instead rely on schema validation libraries like <a href="https://www.allthingstypescript.dev/p/using-zod-schemas-as-source-of-truth">Zod</a> and <a href="https://www.allthingstypescript.dev/p/schema-first-typescript-types-design">Valibot</a> for this, which brings data validation to Javascript, at runtime.</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;6cdf606b-92cb-46fc-817b-ef08d8fadcba&quot;,&quot;caption&quot;:&quot;Let&#8217;s start with a simple hypothesis. Let&#8217;s say you have a form for your users, for instance, a sign-in form. You need a validation schema and a Typescript type, representing the shape of the form data. Why a validation schema, you may ask? This is because data coming from untrusted sources should always be validated, for instance, form data, ensuring t&#8230;&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Schema First Typescript Types Design with Valibot&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-02-12T06:19:15.080Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7b449bb1-5363-415b-8f8b-01ccd839aa96_1280x853.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/schema-first-typescript-types-design&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:141422661,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:2,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;7a505a84-1e63-4ba6-b3d7-6245323d7330&quot;,&quot;caption&quot;:&quot;Hey, Wycliffe here, and welcome to this week&#8217;s mid-week scoop. At the beginning of every week, I usually send out an article covering various Techniques and Lessons in Typescript to help you learn and build more Type-safe code in Typescript, such as&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Using Zod Schemas as Source of Truth for Typescript Types&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2023-01-23T18:30:32.487Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f4dc44a2-dccf-44c6-9bdb-19ce7bdab683_1280x853.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/using-zod-schemas-as-source-of-truth&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:97587944,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:2,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><h4>Why do we need Typescript?</h4><p>I know what you are wondering now, why do we need Typescript? Let's try and answer now by looking at what Typescript brings to the table. </p><p>Typescript adds a typing system for Javascript, allowing you to define the shape of your data and Typescript will type-check your code against the types. This ensures</p><p>By defining typescript, we are defining our intention or contract and then when we make a mistake, like passing the wrong type of data to a function, typescript prevents us from making that mistake.</p><p>This eliminates a whole class of bugs and is very beneficial, as most of the bugs can be caught at compile time before you even compile to Javascript. I find out that most of the bugs I make are typos which can be very devastating and Typescript helps guard against this very well.</p><p>Other benefits of Typescript, include auto-completion, which we get because we define the shape of our data and make the developer experience (DX) much better.</p><h3>Conclusion</h3><p>In this issue of All Things Typescript, I superficially explored the relationship between Javascript and Typescript, with the goal being to help developers improve their Typescript skills. </p><p>Both languages, are tied to the hip, Typescript is a superset of Javascript and apart from the typing system, Typescript looks to be TC39 compliant, meaning feature parity with Javascript.</p><div><hr></div><p>That's it for this issue and thank you for getting this far. If you enjoyed this article and would like to support my work, please share and like this issue and consider sharing All Things Typescript with friends and colleagues.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&quot;,&quot;text&quot;:&quot;Share All Things Typescript&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share"><span>Share All Things Typescript</span></a></p><p>Did you know you can also hire me to coach your team and help them level up their Typescript game? If this sounds interesting to you, please consider getting in touch and we can work out something.</p><div class="directMessage button" data-attrs="{&quot;userId&quot;:43214086,&quot;userName&quot;:&quot;Maina Wycliffe&quot;,&quot;canDm&quot;:null,&quot;dmUpgradeOptions&quot;:null,&quot;isEditorNode&quot;:true}" data-component-name="DirectMessageToDOM"></div><p>And until next time, please keep on learning.</p><p></p><p></p>]]></content:encoded></item><item><title><![CDATA[Making Generics Types Optional - Generics Fundamentals in Typescript]]></title><description><![CDATA[We continue exploring Typescript generics, by looking at making generic types optional by providing default or fallback types when none is provided or inferred.]]></description><link>https://www.allthingstypescript.dev/p/making-generics-types-optional-generics</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/making-generics-types-optional-generics</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 08 Apr 2024 07:56:26 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/7fcf7dc3-4ac4-41ea-a600-63a30d272b3f_3980x2184.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the last two issues, we started exploring generics, where we looked at the <a href="https://www.allthingstypescript.dev/p/generic-constraints-a-gentle-introduction">fundamentals of generics</a> and how to <a href="https://www.allthingstypescript.dev/p/generic-constraints-a-gentle-introduction">create generics constraints</a>. I believe this has given you a good foundation for working with generics in Typescript.</p><p>You can find the last two issues below:</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;9b6c57d7-92b4-42fd-b377-49d5d9a13605&quot;,&quot;caption&quot;:&quot;One of the more advanced topics in Typescript is Generics and understanding and adding them to your Typescript coding toolbox can be very powerful. As we continue with our journey of learning Typescript and building amazing projects, we want to have more and more tools that we can use to make our job easier, without making compromises when possible.&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;A Gentle Introduction to Generics in Typescript&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-03-11T05:58:29.780Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dcd0c857-8ca0-409a-bd1c-3154597b94c8_9048x5612.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/a-gentle-introduction-to-generics&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:142444537,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:5,&quot;comment_count&quot;:2,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:false,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;4382f5db-d734-40eb-b6a7-9dc7c572f074&quot;,&quot;caption&quot;:&quot;In the last issue two weeks ago, we learned about the fundamentals of Generics in Typescript and how we can use generics to make our lives easier as developers. You can find the last issue here. But to quickly recap the last issue, Generics allows us to write our code in a way the data types we are dealing with will be specified later on. In Typescript,&#8230;&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Generic Constraints - A Gentle Introduction to Generics in Typescript&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-03-25T07:46:15.377Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4f2e90f9-c389-4f85-9c33-83f867961bf0_9992x5620.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/generic-constraints-a-gentle-introduction&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:142671089,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:4,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:false,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div><hr></div><p>In this issue, I wanted to explore how we can make a generic Type optional by providing a default for the Type variable, that can be used when no Type is passed in and Typescript cannot infer the Type. Before we can look at generics, let&#8217;s draw some parallels with functions and how defaults work values for functional arguments.</p><p>Let&#8217;s take the following simple example.</p><pre><code>function doSomething(input: string) {
    // do something
}</code></pre><p>In the above example, the function <code>input</code> parameter is required. In some cases, we want to provide a fallback value, that will be used if the function is called without a value.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><p>For functions, the syntax to achieve this is the one shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_jZQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F939489a9-8a8a-4514-802b-2419af6862a2_2840x1164.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_jZQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F939489a9-8a8a-4514-802b-2419af6862a2_2840x1164.png 424w, https://substackcdn.com/image/fetch/$s_!_jZQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F939489a9-8a8a-4514-802b-2419af6862a2_2840x1164.png 848w, https://substackcdn.com/image/fetch/$s_!_jZQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F939489a9-8a8a-4514-802b-2419af6862a2_2840x1164.png 1272w, https://substackcdn.com/image/fetch/$s_!_jZQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F939489a9-8a8a-4514-802b-2419af6862a2_2840x1164.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_jZQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F939489a9-8a8a-4514-802b-2419af6862a2_2840x1164.png" width="1456" height="597" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/939489a9-8a8a-4514-802b-2419af6862a2_2840x1164.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:597,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:750035,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_jZQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F939489a9-8a8a-4514-802b-2419af6862a2_2840x1164.png 424w, https://substackcdn.com/image/fetch/$s_!_jZQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F939489a9-8a8a-4514-802b-2419af6862a2_2840x1164.png 848w, https://substackcdn.com/image/fetch/$s_!_jZQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F939489a9-8a8a-4514-802b-2419af6862a2_2840x1164.png 1272w, https://substackcdn.com/image/fetch/$s_!_jZQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F939489a9-8a8a-4514-802b-2419af6862a2_2840x1164.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In the above example, we are simply telling Javascript that if the <code>input</code> parameter isn&#8217;t provided or undefined, use <code>default-value</code> as the value for the variable. This makes the input string optional, and not required. </p><p>While Typescript will type-check the function&#8217;s <code>input</code> parameter if provided an incorrect Type or if we simply don&#8217;t provide  it, it also means that we need to provide the input every time we call the function. </p><p>By providing a default value, we made the parameter optional, but this also means we have also expanded the types from being just string to string and undefined and to consume it, we might need to narrow the type. You can learn more about type narrowing <a href="https://www.allthingstypescript.dev/p/narrowing-types-in-typescript">here</a>.</p><p>However, by providing a default value, it means we don&#8217;t need to provide it, so we don&#8217;t have to do the extra step of checking if the input is string or undefined, avoiding the extra step for narrowing types, as we already have a fallback value, when undefined.</p><div><hr></div><p><strong>If you like my content and want to support my work, please consider supporting me (</strong><em><strong>you can buy me a double latte, the juice that powers my creativity and determination &#9749;&#65039;</strong></em><strong>) through <a href="https://github.com/sponsors/mainawycliffe/">Github Sponsors</a>.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://github.com/sponsors/mainawycliffe/&quot;,&quot;text&quot;:&quot;&#9749;&#65039; Consider buying me a cup of coffee&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://github.com/sponsors/mainawycliffe/"><span>&#9749;&#65039; Consider buying me a cup of coffee</span></a></p><div><hr></div><h4>What does this have to do with generics?</h4><p>Default Types for generics types work similarly. We can achieve the same thing we did with function parameters with Generics, where we can provide a fallback type for the <strong>type variable</strong> if not provided and cannot be inferred.</p><blockquote><p><strong>A type variable</strong> is a special kind of variable that works on types rather than values.</p></blockquote><p>Let&#8217;s look at an example. Let&#8217;s create a simple utility type. Utility types utilize generics to help you do type transformation. Typescript has some in-built utility types that you can learn more about <a href="https://www.typescriptlang.org/docs/handbook/utility-types.html">here</a>.</p><p>Our utility type will take two types (<em>that are key-value objects</em>) and merge their properties into a single Type, with the properties from both Types. </p><p>Here is an example of what our implementation looks like:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EkMM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F893d2250-2efe-4a95-aca0-7b4d4dbeafa0_3596x940.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EkMM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F893d2250-2efe-4a95-aca0-7b4d4dbeafa0_3596x940.png 424w, https://substackcdn.com/image/fetch/$s_!EkMM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F893d2250-2efe-4a95-aca0-7b4d4dbeafa0_3596x940.png 848w, https://substackcdn.com/image/fetch/$s_!EkMM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F893d2250-2efe-4a95-aca0-7b4d4dbeafa0_3596x940.png 1272w, https://substackcdn.com/image/fetch/$s_!EkMM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F893d2250-2efe-4a95-aca0-7b4d4dbeafa0_3596x940.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EkMM!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F893d2250-2efe-4a95-aca0-7b4d4dbeafa0_3596x940.png" width="1200" height="314.010989010989" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/893d2250-2efe-4a95-aca0-7b4d4dbeafa0_3596x940.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:381,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:268060,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EkMM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F893d2250-2efe-4a95-aca0-7b4d4dbeafa0_3596x940.png 424w, https://substackcdn.com/image/fetch/$s_!EkMM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F893d2250-2efe-4a95-aca0-7b4d4dbeafa0_3596x940.png 848w, https://substackcdn.com/image/fetch/$s_!EkMM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F893d2250-2efe-4a95-aca0-7b4d4dbeafa0_3596x940.png 1272w, https://substackcdn.com/image/fetch/$s_!EkMM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F893d2250-2efe-4a95-aca0-7b4d4dbeafa0_3596x940.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">We will go into the details of the above example, in a future issue (hopefully next week), as we explore Mapped Types and other Types in Typescript. </figcaption></figure></div><p>To use our utility type, we just need to provide two object types and we get a single object with properties from both types that were passed in, as shown below:</p><div class="native-video-embed" data-component-name="VideoPlaceholder" data-attrs="{&quot;mediaUploadId&quot;:&quot;0137ceaf-864e-4e1b-b4b4-9dc4d35721d7&quot;,&quot;duration&quot;:null}"></div><p>You can find the above code <a href="https://www.typescriptlang.org/play?#code/C4TwDgpgBAshBOBzCB5ARgKwgY2AFXAgGcAeAQSggA9gIA7AEyKgCUcB7eBko4eASzqIANFACudANZ12AdzoA+UQCFKNek1YcuPPoJHipM+QoVQAvFADeAKCj2oAbQAK8dpHigogqJIgh2ADMoMgBdAC4Qxz8A4LCAbhsAXygAMms7Bxc3Dy8fGKCoZQii6P9C4sSkmxrQSChnBCJ2OgA5AEMAW2ILDIcoQP54Xg7uyN4BIUT+gBt2ka6Icb0p5NrCBqaWsmRe23725GXJxGmHBnY0Y-0q9frG4ZbeuCRUTBx8QlIH5rbFolEP22yAUNUCElw-CeFwAyuxusAABb6AAUgjAYmAkSBdAAlH0HOjMQA6Q4QZJAA">here</a>.</p><p>Our example above doesn&#8217;t do much, but let&#8217;s say that in the future the requirements of our project change and we want to expand it so that we can merge two or three types.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ee96!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5af7113a-6d2a-4e7b-ae72-8dae60f2c909_4852x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ee96!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5af7113a-6d2a-4e7b-ae72-8dae60f2c909_4852x1024.png 424w, https://substackcdn.com/image/fetch/$s_!ee96!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5af7113a-6d2a-4e7b-ae72-8dae60f2c909_4852x1024.png 848w, https://substackcdn.com/image/fetch/$s_!ee96!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5af7113a-6d2a-4e7b-ae72-8dae60f2c909_4852x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!ee96!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5af7113a-6d2a-4e7b-ae72-8dae60f2c909_4852x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ee96!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5af7113a-6d2a-4e7b-ae72-8dae60f2c909_4852x1024.png" width="1200" height="253.02197802197801" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5af7113a-6d2a-4e7b-ae72-8dae60f2c909_4852x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:307,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:312219,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ee96!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5af7113a-6d2a-4e7b-ae72-8dae60f2c909_4852x1024.png 424w, https://substackcdn.com/image/fetch/$s_!ee96!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5af7113a-6d2a-4e7b-ae72-8dae60f2c909_4852x1024.png 848w, https://substackcdn.com/image/fetch/$s_!ee96!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5af7113a-6d2a-4e7b-ae72-8dae60f2c909_4852x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!ee96!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5af7113a-6d2a-4e7b-ae72-8dae60f2c909_4852x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>The problem is that this would work with merging 3 Types, but to work with merging two Types, we would need to provide an empty object Type, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!AnMo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0575db5a-f9b3-4e01-bc09-6ae299cd7e60_2272x520.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!AnMo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0575db5a-f9b3-4e01-bc09-6ae299cd7e60_2272x520.png 424w, https://substackcdn.com/image/fetch/$s_!AnMo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0575db5a-f9b3-4e01-bc09-6ae299cd7e60_2272x520.png 848w, https://substackcdn.com/image/fetch/$s_!AnMo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0575db5a-f9b3-4e01-bc09-6ae299cd7e60_2272x520.png 1272w, https://substackcdn.com/image/fetch/$s_!AnMo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0575db5a-f9b3-4e01-bc09-6ae299cd7e60_2272x520.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!AnMo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0575db5a-f9b3-4e01-bc09-6ae299cd7e60_2272x520.png" width="728" height="166.5" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0575db5a-f9b3-4e01-bc09-6ae299cd7e60_2272x520.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:333,&quot;width&quot;:1456,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:97684,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!AnMo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0575db5a-f9b3-4e01-bc09-6ae299cd7e60_2272x520.png 424w, https://substackcdn.com/image/fetch/$s_!AnMo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0575db5a-f9b3-4e01-bc09-6ae299cd7e60_2272x520.png 848w, https://substackcdn.com/image/fetch/$s_!AnMo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0575db5a-f9b3-4e01-bc09-6ae299cd7e60_2272x520.png 1272w, https://substackcdn.com/image/fetch/$s_!AnMo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0575db5a-f9b3-4e01-bc09-6ae299cd7e60_2272x520.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Wouldn&#8217;t be nice if we could skip that part, and just provide our two types? And this is where Generic type variable defaults come to the rescue. </p><p>Just like function parameters, we can provide a default Type for our Type variable and essence making them optional, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tCvT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbae77c1-a24a-4c93-852e-0b54b240f738_5032x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tCvT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbae77c1-a24a-4c93-852e-0b54b240f738_5032x1024.png 424w, https://substackcdn.com/image/fetch/$s_!tCvT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbae77c1-a24a-4c93-852e-0b54b240f738_5032x1024.png 848w, https://substackcdn.com/image/fetch/$s_!tCvT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbae77c1-a24a-4c93-852e-0b54b240f738_5032x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!tCvT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbae77c1-a24a-4c93-852e-0b54b240f738_5032x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tCvT!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbae77c1-a24a-4c93-852e-0b54b240f738_5032x1024.png" width="1200" height="243.95604395604394" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bbae77c1-a24a-4c93-852e-0b54b240f738_5032x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:296,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:289117,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tCvT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbae77c1-a24a-4c93-852e-0b54b240f738_5032x1024.png 424w, https://substackcdn.com/image/fetch/$s_!tCvT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbae77c1-a24a-4c93-852e-0b54b240f738_5032x1024.png 848w, https://substackcdn.com/image/fetch/$s_!tCvT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbae77c1-a24a-4c93-852e-0b54b240f738_5032x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!tCvT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbae77c1-a24a-4c93-852e-0b54b240f738_5032x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>And now, our <code>MergeObjectTypes</code> utility type can accept either two or three Types and merge their properties into a single Type.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wpzb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eb96c1a-04d3-40ef-b5bb-4b3b4493e094_3204x772.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wpzb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eb96c1a-04d3-40ef-b5bb-4b3b4493e094_3204x772.png 424w, https://substackcdn.com/image/fetch/$s_!wpzb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eb96c1a-04d3-40ef-b5bb-4b3b4493e094_3204x772.png 848w, https://substackcdn.com/image/fetch/$s_!wpzb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eb96c1a-04d3-40ef-b5bb-4b3b4493e094_3204x772.png 1272w, https://substackcdn.com/image/fetch/$s_!wpzb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eb96c1a-04d3-40ef-b5bb-4b3b4493e094_3204x772.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wpzb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eb96c1a-04d3-40ef-b5bb-4b3b4493e094_3204x772.png" width="728" height="175.5" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4eb96c1a-04d3-40ef-b5bb-4b3b4493e094_3204x772.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:351,&quot;width&quot;:1456,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:170145,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wpzb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eb96c1a-04d3-40ef-b5bb-4b3b4493e094_3204x772.png 424w, https://substackcdn.com/image/fetch/$s_!wpzb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eb96c1a-04d3-40ef-b5bb-4b3b4493e094_3204x772.png 848w, https://substackcdn.com/image/fetch/$s_!wpzb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eb96c1a-04d3-40ef-b5bb-4b3b4493e094_3204x772.png 1272w, https://substackcdn.com/image/fetch/$s_!wpzb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4eb96c1a-04d3-40ef-b5bb-4b3b4493e094_3204x772.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>And we can go crazy now and add more type variables to merge 4, 5, or 10 even object properties while ensuring our utility types work with 2 to 10 types, and anywhere in between. </p><blockquote><p>If you are interested, see if you can modify the above example to work with 5 types.</p></blockquote><h4>Rules for Default Types</h4><p>Just like function parameters, there are a few rules to keep in mind for generic default Types.</p><ul><li><p>The first and most obvious one is that when you provide a default value, a Type variable is deemed as optional. </p><ul><li><p>You are required to provide the required type variables. </p></li><li><p>Remember, Type inference in most cases can help you, so you don&#8217;t provide the required Type variables.</p></li><li><p>If you don&#8217;t provide an optional Type variable, then the default type is used, unless Typescript can infer a different type based on usage.</p></li></ul></li><li><p>And just for functions, required type variables come first, followed by optional type variables.</p></li><li><p>If you are using a generics constraint, the default type must satisfy that constraint.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ikHN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1be2623-e3b1-4231-8fcd-1875642e8711_1556x604.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ikHN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1be2623-e3b1-4231-8fcd-1875642e8711_1556x604.png 424w, https://substackcdn.com/image/fetch/$s_!ikHN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1be2623-e3b1-4231-8fcd-1875642e8711_1556x604.png 848w, https://substackcdn.com/image/fetch/$s_!ikHN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1be2623-e3b1-4231-8fcd-1875642e8711_1556x604.png 1272w, https://substackcdn.com/image/fetch/$s_!ikHN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1be2623-e3b1-4231-8fcd-1875642e8711_1556x604.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ikHN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1be2623-e3b1-4231-8fcd-1875642e8711_1556x604.png" width="1456" height="565" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d1be2623-e3b1-4231-8fcd-1875642e8711_1556x604.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:565,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:88573,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ikHN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1be2623-e3b1-4231-8fcd-1875642e8711_1556x604.png 424w, https://substackcdn.com/image/fetch/$s_!ikHN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1be2623-e3b1-4231-8fcd-1875642e8711_1556x604.png 848w, https://substackcdn.com/image/fetch/$s_!ikHN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1be2623-e3b1-4231-8fcd-1875642e8711_1556x604.png 1272w, https://substackcdn.com/image/fetch/$s_!ikHN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd1be2623-e3b1-4231-8fcd-1875642e8711_1556x604.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div></li><li><p>For classes and interfaces, when extending either a class or an interface, you can introduce a default for an existing Type parameter and you may also introduce a new Typer variable, as long as it&#8217;s optional (has a type variable).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fX-y!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6b594b9-9118-4778-82f5-6a85a296acfb_2164x940.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fX-y!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6b594b9-9118-4778-82f5-6a85a296acfb_2164x940.png 424w, https://substackcdn.com/image/fetch/$s_!fX-y!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6b594b9-9118-4778-82f5-6a85a296acfb_2164x940.png 848w, https://substackcdn.com/image/fetch/$s_!fX-y!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6b594b9-9118-4778-82f5-6a85a296acfb_2164x940.png 1272w, https://substackcdn.com/image/fetch/$s_!fX-y!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6b594b9-9118-4778-82f5-6a85a296acfb_2164x940.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fX-y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6b594b9-9118-4778-82f5-6a85a296acfb_2164x940.png" width="1456" height="632" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b6b594b9-9118-4778-82f5-6a85a296acfb_2164x940.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:632,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:146376,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fX-y!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6b594b9-9118-4778-82f5-6a85a296acfb_2164x940.png 424w, https://substackcdn.com/image/fetch/$s_!fX-y!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6b594b9-9118-4778-82f5-6a85a296acfb_2164x940.png 848w, https://substackcdn.com/image/fetch/$s_!fX-y!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6b594b9-9118-4778-82f5-6a85a296acfb_2164x940.png 1272w, https://substackcdn.com/image/fetch/$s_!fX-y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6b594b9-9118-4778-82f5-6a85a296acfb_2164x940.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div></li></ul><h3>Conclusion</h3><p>In this issue, we looked at making generics Type variables optional by providing a default or fallback type. We drew parallels between Generics default types with functions parameters default values and how they behave almost the same, but with the key difference being one applies to variables while the other one applies to types. </p><p>To demonstrate how useful default generics types can be, we took a look at an example where we can create a custom utility type that we can use to merge properties for an object type, for either two or three types, and how we can utilize defaults to make this work seamless for either of the cases.</p><div><hr></div><p>That's it for this issue and thank you for getting this far. If you enjoyed this article and would like to support my work, please share and like this issue and consider sharing All Things Typescript with friends and colleagues.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&quot;,&quot;text&quot;:&quot;Share All Things Typescript&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share"><span>Share All Things Typescript</span></a></p><p>Did you know you can also hire me to coach your team and help them level up their Typescript game? If this sounds interesting to you, please consider getting in touch and we can work out something.</p><div class="directMessage button" data-attrs="{&quot;userId&quot;:43214086,&quot;userName&quot;:&quot;Maina Wycliffe&quot;,&quot;canDm&quot;:null,&quot;dmUpgradeOptions&quot;:null,&quot;isEditorNode&quot;:true}" data-component-name="DirectMessageToDOM"></div><p>And until next time, please keep on learning.</p><p></p>]]></content:encoded></item><item><title><![CDATA[March 2024 Monthly Newsletter - TypeScript 5.5 is going to be huge]]></title><description><![CDATA[Typescript 5.4 released, Typescript 5.5 around the corner with some exciting news and hand curated content for Typescript.]]></description><link>https://www.allthingstypescript.dev/p/feb-2024-monthly-newsletter-typescript-4cd</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/feb-2024-monthly-newsletter-typescript-4cd</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 01 Apr 2024 19:26:21 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/9b2bb306-829c-4c93-b6a3-8021f465745e_1476x830.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey Everyone, another month is gone, and today is the day I look at informative content that I came across in the last month and also news related to Typescript. </p><p>My name is Maina Wycliffe, and I love teaching Typescript to developers, my goal is to break down complex topics into small understandable bits that you can apply in your day-to-day life as a developer.</p><h3>Typescript News</h3><p>Over the last two issues, we have looked at the release process of Typescript 5.4 which was released last month, so I won&#8217;t focus on that on this issue, you can learn more about Typescript 5.4 <a href="https://devblogs.microsoft.com/typescript/announcing-typescript-5-4/">here</a>. </p><p>For this issue, I want to focus on the next version of Typescript which has the potential to be huge. First, before proceeding further, I want to state we are still early and some of the details might change as we get closer to the next issue. Now, that that is out of the way, let me focus on a few interesting changes:</p><ol><li><p>APIs marked in version 5 as deprecated are going to be removed, for more information, check out this <a href="https://github.com/microsoft/TypeScript/issues/51909">issue</a>.</p></li><li><p>Ability to add imports, when you copy-paste code from another TS file, the same imports in that file, for information, check out this issue.</p></li><li><p>Narrowing of generics indexed access type return type, for more information, take a look at this <a href="https://github.com/microsoft/TypeScript/issues/33014">issue</a>.</p></li><li><p>And many more such as providing syntax checking for regex, for more information, check out the road map <a href="https://github.com/microsoft/TypeScript/issues/57475">here</a>.</p></li></ol><p>Let me know if there are features you want me to cover in a future issue of All Things Typescript.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><div><hr></div><h2>From All Things Typescript</h2><p>This month, I wrote two articles that explain generics which I hope for those of you who have read them and found very useful.</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;af446a6f-3edc-42e0-8aa8-1693af3e25f6&quot;,&quot;caption&quot;:&quot;One of the more advanced topics in Typescript is Generics and understanding and adding them to your Typescript coding toolbox can be very powerful. As we continue with our journey of learning Typescript and building amazing projects, we want to have more and more tools that we can use to make our job easier, without making compromises when possible.&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;A Gentle Introduction to Generics in Typescript&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-03-11T05:58:29.780Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dcd0c857-8ca0-409a-bd1c-3154597b94c8_9048x5612.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/a-gentle-introduction-to-generics&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:142444537,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:5,&quot;comment_count&quot;:2,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;7ad3591a-9443-4603-8fad-9053842bf422&quot;,&quot;caption&quot;:&quot;In the last issue two weeks ago, we learned about the fundamentals of Generics in Typescript and how we can use generics to make our lives easier as developers. You can find the last issue here. But to quickly recap the last issue, Generics allows us to write our code in a way the data types we are dealing with will be specified later on. In Typescript,&#8230;&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Generic Constraints - A Gentle Introduction to Generics in Typescript&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-03-25T07:46:15.377Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4f2e90f9-c389-4f85-9c33-83f867961bf0_9992x5620.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/generic-constraints-a-gentle-introduction&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:142671089,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:3,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div><hr></div><h2>From other Authors</h2><h4><a href="https://fettblog.eu/typescript-never-and-error-handling/">The `never` type and error handling in TypeScript by Stefan Baumgartner</a></h4><p>This article by Stefan Baumgartner goes on to explain more about the never type, common misunderstandings about the never type, and how to correctly handle the never type for handling errors.</p><p>For more information, you can go through the article <a href="https://fettblog.eu/typescript-never-and-error-handling/">here</a>.</p><h4><a href="https://www.totaltypescript.com/react-apps-ts-performance">This Pattern Will Wreck Your React App's TS Performance by Matt Pocock</a></h4><p>In this article by Matt Pocock, he looks at a case where using one way of defining Typescript type can slow down your IDE performance compared to another way, especially in a large code base.</p><p>For more information, you can go through the article <a href="https://www.totaltypescript.com/react-apps-ts-performance">here</a>.</p><h4><a href="https://www.telerik.com/blogs/making-your-code-type-safe-zod">Making Your Code Type-Safe with Zod by Leonardo Maldonado</a></h4><p>In the past, I have made it clear I am a huge fan of tools like Zod which look to combine schema validation for forms, and runtime data validation, while also getting back to static types for you to use in Typescript. This article takes you through the process of setting up Zod and using it to make your code type-safe with Zod.</p><p>For more information, you can go through the article <a href="https://www.telerik.com/blogs/making-your-code-type-safe-zod">here</a>.</p><h4><a href="https://www.totaltypescript.com/react-apps-ts-performance">`any` Considered Harmful, Except For These Cases by Matt Pocock</a></h4><p>If there is anything I have spent a large amount of time doing in this newsletter, is discouraging you not to use any. In software engineering, it&#8217;s quite rare we have a definite answer the correct answer is always, it depends and this article by Matt Pocock is a great example of situations where it&#8217;s acceptable to use any.</p><p>For more information, you can go through the article <a href="https://www.totaltypescript.com/any-considered-harmful">here</a>.</p><h4><a href="https://claritydev.net/blog/intersection-types-in-typescript">Intersection Types In TypeScript by Alex Khomenko</a></h4><p>This article by Alex Khomenko takes you through an introduction and a detailed description of what the intersection types are and how you can add them to your toolbox and be comfortable using them in your day-to-day activities.</p><p>For more information, you can go through the article <a href="https://claritydev.net/blog/intersection-types-in-typescript">here</a>.</p><h4><a href="https://dev.to/this-is-learning/typescript-intermediates-type-generics-b84">TypeScript Intermediates - Type Generics by Corbin Crutchley</a></h4><p>This article by Corbin Crutchley is about generics and some real-world uses for generics. He goes into detail explaining a use case, and looks at alternative solutions, before explaining why generics are a good fit and why you might consider using generics for that use case.</p><p>For more information, you can go through the article <a href="https://dev.to/this-is-learning/typescript-intermediates-type-generics-b84">here</a>.</p><div><hr></div><p>That&#8217;s it for now and I will see you again at the end of April, where I will share some interesting reads that I will come across in my learning journey in April. Until then, I will continue teaching Typescript, and next week, we will take a look at Typescript Generics and default values, to wrap up my generics series.</p><p>If you found this issue useful, consider subscribing, if you haven&#8217;t and if you have (Thank you &#10084;&#65039;) please consider sharing All Things Typescript with your friends and colleagues.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&quot;,&quot;text&quot;:&quot;Share All Things Typescript&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share"><span>Share All Things Typescript</span></a></p><p>Until next, keep on learning.</p>]]></content:encoded></item><item><title><![CDATA[Generic Constraints - A Gentle Introduction to Generics in Typescript]]></title><description><![CDATA[In this issue, you will learn about Generic Constraint, it's importance and how to use Generic Constraint in your daily activities as a developer.]]></description><link>https://www.allthingstypescript.dev/p/generic-constraints-a-gentle-introduction</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/generic-constraints-a-gentle-introduction</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 25 Mar 2024 07:46:15 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/4f2e90f9-c389-4f85-9c33-83f867961bf0_9992x5620.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the last <a href="https://www.allthingstypescript.dev/p/a-gentle-introduction-to-generics">issue two weeks ago</a>, we learned about the fundamentals of Generics in Typescript and how we can use generics to make our lives easier as developers. You can find the last issue <a href="https://www.allthingstypescript.dev/p/a-gentle-introduction-to-generics">here</a>. </p><p>But to quickly recap the last issue, Generics allows us to write our code in a way the data types we are dealing with will be specified later on. In Typescript, we do this using the type variable specified inside <strong>Angle </strong><code>&lt;&gt;</code><strong> brackets</strong>, which we can then use within our application.</p><blockquote><p><strong>A type variable</strong> is a special kind of variable that works on types rather than values.</p></blockquote><p>Here is an example of a <code>sortArray</code> function, that can sort any array, accepting a <code>SortType</code> type variable, for the function input and output.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!XGi0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c8e4015-fa0b-483c-895a-2475e8bb09a9_2652x652.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!XGi0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c8e4015-fa0b-483c-895a-2475e8bb09a9_2652x652.png 424w, https://substackcdn.com/image/fetch/$s_!XGi0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c8e4015-fa0b-483c-895a-2475e8bb09a9_2652x652.png 848w, https://substackcdn.com/image/fetch/$s_!XGi0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c8e4015-fa0b-483c-895a-2475e8bb09a9_2652x652.png 1272w, https://substackcdn.com/image/fetch/$s_!XGi0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c8e4015-fa0b-483c-895a-2475e8bb09a9_2652x652.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!XGi0!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c8e4015-fa0b-483c-895a-2475e8bb09a9_2652x652.png" width="1200" height="295.05494505494505" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7c8e4015-fa0b-483c-895a-2475e8bb09a9_2652x652.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:358,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:106481,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!XGi0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c8e4015-fa0b-483c-895a-2475e8bb09a9_2652x652.png 424w, https://substackcdn.com/image/fetch/$s_!XGi0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c8e4015-fa0b-483c-895a-2475e8bb09a9_2652x652.png 848w, https://substackcdn.com/image/fetch/$s_!XGi0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c8e4015-fa0b-483c-895a-2475e8bb09a9_2652x652.png 1272w, https://substackcdn.com/image/fetch/$s_!XGi0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c8e4015-fa0b-483c-895a-2475e8bb09a9_2652x652.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p>In the above functions sort generic functions, we need to specify the type of array we are sorting by providing the <code>SortType</code> type variable, just before we provide the arguments for functions, as shown below.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!stml!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66aa33fd-0147-4436-bfc1-c27634476a1e_2040x484.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!stml!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66aa33fd-0147-4436-bfc1-c27634476a1e_2040x484.png 424w, https://substackcdn.com/image/fetch/$s_!stml!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66aa33fd-0147-4436-bfc1-c27634476a1e_2040x484.png 848w, https://substackcdn.com/image/fetch/$s_!stml!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66aa33fd-0147-4436-bfc1-c27634476a1e_2040x484.png 1272w, https://substackcdn.com/image/fetch/$s_!stml!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66aa33fd-0147-4436-bfc1-c27634476a1e_2040x484.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!stml!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66aa33fd-0147-4436-bfc1-c27634476a1e_2040x484.png" width="1200" height="284.34065934065933" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/66aa33fd-0147-4436-bfc1-c27634476a1e_2040x484.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:345,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:65280,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!stml!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66aa33fd-0147-4436-bfc1-c27634476a1e_2040x484.png 424w, https://substackcdn.com/image/fetch/$s_!stml!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66aa33fd-0147-4436-bfc1-c27634476a1e_2040x484.png 848w, https://substackcdn.com/image/fetch/$s_!stml!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66aa33fd-0147-4436-bfc1-c27634476a1e_2040x484.png 1272w, https://substackcdn.com/image/fetch/$s_!stml!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66aa33fd-0147-4436-bfc1-c27634476a1e_2040x484.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><p>And this can be done for classes and when defining other types using either interface or type, to learn more, check out the previous issue <a href="https://www.allthingstypescript.dev/p/a-gentle-introduction-to-generics">here</a>.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><div><hr></div><h3>What are generic constraints?</h3><p>Before we can try and define generic constraining, let&#8217;s define the word constraint, on its own. Stay with me here, I know this is a Typescript newsletter, not an English newsletter and I am probably the worst person to teach English, considering it&#8217;s probably my third language. I digress anyway.</p><p>According to the <a href="https://dictionary.cambridge.org/dictionary/english/constraint">Cambridge Dictionary</a> one of many definitions <strong>(I cherry-picked &#129335;&#127998;)</strong>, <strong>a constraint is something that controls what you do by keeping you within particular limits</strong>. The key part I want you to remember for this issue is <strong>keeping you within particular limits</strong>. </p><p>Back to Typescript and Generics, if we looked at the previous example we had, our <code>sortArray</code> function is meant to sort arrays, not other data types, which would probably require a different algorithm for arrays. </p><p>However, our function will accept array types as well as non-array types, and this is not the desired behavior. This isn&#8217;t type-safe, as the function will probably throw a runtime error if we attempt to sort a string data type when we wrote it to sort arrays.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IhVt!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3db215ce-9871-4579-933c-394ecb34bb29_2544x820.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IhVt!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3db215ce-9871-4579-933c-394ecb34bb29_2544x820.png 424w, https://substackcdn.com/image/fetch/$s_!IhVt!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3db215ce-9871-4579-933c-394ecb34bb29_2544x820.png 848w, https://substackcdn.com/image/fetch/$s_!IhVt!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3db215ce-9871-4579-933c-394ecb34bb29_2544x820.png 1272w, https://substackcdn.com/image/fetch/$s_!IhVt!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3db215ce-9871-4579-933c-394ecb34bb29_2544x820.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IhVt!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3db215ce-9871-4579-933c-394ecb34bb29_2544x820.png" width="1200" height="386.53846153846155" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3db215ce-9871-4579-933c-394ecb34bb29_2544x820.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:469,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:156691,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!IhVt!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3db215ce-9871-4579-933c-394ecb34bb29_2544x820.png 424w, https://substackcdn.com/image/fetch/$s_!IhVt!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3db215ce-9871-4579-933c-394ecb34bb29_2544x820.png 848w, https://substackcdn.com/image/fetch/$s_!IhVt!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3db215ce-9871-4579-933c-394ecb34bb29_2544x820.png 1272w, https://substackcdn.com/image/fetch/$s_!IhVt!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3db215ce-9871-4579-933c-394ecb34bb29_2544x820.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Typescript was meant to eliminate these kinds of errors, but we just made it even simpler to make them, and no way to know ahead of time.</p><p>And this is where generics constraints come in. They enable us to limit (restrict or constrain) the types that can be provided for our type variables. This way, our function will accept arrays, but it will not accept non-array types. We do this by using the extends keyword and providing the types we want to contain our type variable to.</p><p>For instance, in our <code>sortArray</code> function, we can restrict our type variable to be only an array of <code>string</code>, <code>number</code>, or <code>boolean</code>, as shown below.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6GSY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F139f2138-5671-4641-bbdf-f9ad6e16b7fd_3476x736.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6GSY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F139f2138-5671-4641-bbdf-f9ad6e16b7fd_3476x736.png 424w, https://substackcdn.com/image/fetch/$s_!6GSY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F139f2138-5671-4641-bbdf-f9ad6e16b7fd_3476x736.png 848w, https://substackcdn.com/image/fetch/$s_!6GSY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F139f2138-5671-4641-bbdf-f9ad6e16b7fd_3476x736.png 1272w, https://substackcdn.com/image/fetch/$s_!6GSY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F139f2138-5671-4641-bbdf-f9ad6e16b7fd_3476x736.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6GSY!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F139f2138-5671-4641-bbdf-f9ad6e16b7fd_3476x736.png" width="1200" height="253.84615384615384" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/139f2138-5671-4641-bbdf-f9ad6e16b7fd_3476x736.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:308,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:154540,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6GSY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F139f2138-5671-4641-bbdf-f9ad6e16b7fd_3476x736.png 424w, https://substackcdn.com/image/fetch/$s_!6GSY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F139f2138-5671-4641-bbdf-f9ad6e16b7fd_3476x736.png 848w, https://substackcdn.com/image/fetch/$s_!6GSY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F139f2138-5671-4641-bbdf-f9ad6e16b7fd_3476x736.png 1272w, https://substackcdn.com/image/fetch/$s_!6GSY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F139f2138-5671-4641-bbdf-f9ad6e16b7fd_3476x736.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>And now, when we try to pass in a string, as we previously did, Typescript catches the type error for us, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!oJi-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b1e7ae2-8582-45a2-bce3-a26f5b4dd57d_827x75.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!oJi-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b1e7ae2-8582-45a2-bce3-a26f5b4dd57d_827x75.png 424w, https://substackcdn.com/image/fetch/$s_!oJi-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b1e7ae2-8582-45a2-bce3-a26f5b4dd57d_827x75.png 848w, https://substackcdn.com/image/fetch/$s_!oJi-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b1e7ae2-8582-45a2-bce3-a26f5b4dd57d_827x75.png 1272w, https://substackcdn.com/image/fetch/$s_!oJi-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b1e7ae2-8582-45a2-bce3-a26f5b4dd57d_827x75.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!oJi-!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b1e7ae2-8582-45a2-bce3-a26f5b4dd57d_827x75.png" width="1200" height="108.82708585247885" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7b1e7ae2-8582-45a2-bce3-a26f5b4dd57d_827x75.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:75,&quot;width&quot;:827,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:24179,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!oJi-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b1e7ae2-8582-45a2-bce3-a26f5b4dd57d_827x75.png 424w, https://substackcdn.com/image/fetch/$s_!oJi-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b1e7ae2-8582-45a2-bce3-a26f5b4dd57d_827x75.png 848w, https://substackcdn.com/image/fetch/$s_!oJi-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b1e7ae2-8582-45a2-bce3-a26f5b4dd57d_827x75.png 1272w, https://substackcdn.com/image/fetch/$s_!oJi-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b1e7ae2-8582-45a2-bce3-a26f5b4dd57d_827x75.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Typescript now catches and warns us that we can not pass a <code>string</code> type as the Type variable as it doesn&#8217;t satisfy the type constraint we provided. This works with Type inference too, as discussed in the previous issue.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sceN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d024c7b-e614-4fda-8d8b-48e0492ad255_981x85.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sceN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d024c7b-e614-4fda-8d8b-48e0492ad255_981x85.png 424w, https://substackcdn.com/image/fetch/$s_!sceN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d024c7b-e614-4fda-8d8b-48e0492ad255_981x85.png 848w, https://substackcdn.com/image/fetch/$s_!sceN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d024c7b-e614-4fda-8d8b-48e0492ad255_981x85.png 1272w, https://substackcdn.com/image/fetch/$s_!sceN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d024c7b-e614-4fda-8d8b-48e0492ad255_981x85.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sceN!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d024c7b-e614-4fda-8d8b-48e0492ad255_981x85.png" width="1200" height="103.97553516819572" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8d024c7b-e614-4fda-8d8b-48e0492ad255_981x85.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:85,&quot;width&quot;:981,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:25648,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!sceN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d024c7b-e614-4fda-8d8b-48e0492ad255_981x85.png 424w, https://substackcdn.com/image/fetch/$s_!sceN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d024c7b-e614-4fda-8d8b-48e0492ad255_981x85.png 848w, https://substackcdn.com/image/fetch/$s_!sceN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d024c7b-e614-4fda-8d8b-48e0492ad255_981x85.png 1272w, https://substackcdn.com/image/fetch/$s_!sceN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8d024c7b-e614-4fda-8d8b-48e0492ad255_981x85.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>As you can see generic constraints allow us to achieve type safety by restricting the Types for our application. By using Typescript generics constraints, we can now write our generics code knowing that we are dealing with a known pool of types rather than all types available. In our case, we are only dealing with arrays and not non-array types.</p><p>In the previous example. we looked at constraining it to be an array, but we can constrain it to any type, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WtFL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce5ad5a7-7b61-4f58-b16b-8809fd1bc648_2868x1660.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WtFL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce5ad5a7-7b61-4f58-b16b-8809fd1bc648_2868x1660.png 424w, https://substackcdn.com/image/fetch/$s_!WtFL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce5ad5a7-7b61-4f58-b16b-8809fd1bc648_2868x1660.png 848w, https://substackcdn.com/image/fetch/$s_!WtFL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce5ad5a7-7b61-4f58-b16b-8809fd1bc648_2868x1660.png 1272w, https://substackcdn.com/image/fetch/$s_!WtFL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce5ad5a7-7b61-4f58-b16b-8809fd1bc648_2868x1660.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WtFL!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce5ad5a7-7b61-4f58-b16b-8809fd1bc648_2868x1660.png" width="1200" height="694.7802197802198" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ce5ad5a7-7b61-4f58-b16b-8809fd1bc648_2868x1660.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:843,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:284317,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WtFL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce5ad5a7-7b61-4f58-b16b-8809fd1bc648_2868x1660.png 424w, https://substackcdn.com/image/fetch/$s_!WtFL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce5ad5a7-7b61-4f58-b16b-8809fd1bc648_2868x1660.png 848w, https://substackcdn.com/image/fetch/$s_!WtFL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce5ad5a7-7b61-4f58-b16b-8809fd1bc648_2868x1660.png 1272w, https://substackcdn.com/image/fetch/$s_!WtFL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce5ad5a7-7b61-4f58-b16b-8809fd1bc648_2868x1660.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In the first case, we are constraining to only accept strings (and their subtypes), and in the second case, we want it to be a key-value object and in the last case, we are providing our type, to just demo the different ways you can constraint a Generic.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://github.com/sponsors/mainawycliffe/&quot;,&quot;text&quot;:&quot;&#9749;&#65039; Consider buying me a cup of coffee&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://github.com/sponsors/mainawycliffe/"><span>&#9749;&#65039; Consider buying me a cup of coffee</span></a></p><div><hr></div><h4>Type variable constrained by another Type Variable</h4><p>So far, we have dealt with simple but useful cases. I don&#8217;t want to go into some of the most advanced use cases of generics yet, because this is still a gentle introduction to generics but let&#8217;s look at another common scenario. </p><p>For instance, let&#8217;s say we want to create a function that returns a property from a key-value object, given its key, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qqu1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80d96bca-17ab-4f04-a197-5cb158136513_3692x652.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qqu1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80d96bca-17ab-4f04-a197-5cb158136513_3692x652.png 424w, https://substackcdn.com/image/fetch/$s_!qqu1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80d96bca-17ab-4f04-a197-5cb158136513_3692x652.png 848w, https://substackcdn.com/image/fetch/$s_!qqu1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80d96bca-17ab-4f04-a197-5cb158136513_3692x652.png 1272w, https://substackcdn.com/image/fetch/$s_!qqu1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80d96bca-17ab-4f04-a197-5cb158136513_3692x652.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qqu1!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80d96bca-17ab-4f04-a197-5cb158136513_3692x652.png" width="1200" height="211.8131868131868" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/80d96bca-17ab-4f04-a197-5cb158136513_3692x652.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:257,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:133430,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qqu1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80d96bca-17ab-4f04-a197-5cb158136513_3692x652.png 424w, https://substackcdn.com/image/fetch/$s_!qqu1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80d96bca-17ab-4f04-a197-5cb158136513_3692x652.png 848w, https://substackcdn.com/image/fetch/$s_!qqu1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80d96bca-17ab-4f04-a197-5cb158136513_3692x652.png 1272w, https://substackcdn.com/image/fetch/$s_!qqu1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F80d96bca-17ab-4f04-a197-5cb158136513_3692x652.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>As you can see, we are using a Generic constraint to ensure that the first argument is always a key-value object, but we have a slight problem. We would like the second function argument - <code>key</code> - to be only a key that exists inside the object we pass in. At the moment, we can pass anything and this could lead to undesired characteristics from our application. </p><p>We can fix this issue by adding a second type variable that we can call <code>Key</code> and whose type will be constrained to the keys of the object, for this we can use the <code>keyof</code> operator to get the keys of the first type variable, as shown below.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!HtuH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a5a606b-4623-4527-8e9f-8722660dc4ca_4444x736.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!HtuH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a5a606b-4623-4527-8e9f-8722660dc4ca_4444x736.png 424w, https://substackcdn.com/image/fetch/$s_!HtuH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a5a606b-4623-4527-8e9f-8722660dc4ca_4444x736.png 848w, https://substackcdn.com/image/fetch/$s_!HtuH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a5a606b-4623-4527-8e9f-8722660dc4ca_4444x736.png 1272w, https://substackcdn.com/image/fetch/$s_!HtuH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a5a606b-4623-4527-8e9f-8722660dc4ca_4444x736.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!HtuH!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a5a606b-4623-4527-8e9f-8722660dc4ca_4444x736.png" width="1200" height="198.62637362637363" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7a5a606b-4623-4527-8e9f-8722660dc4ca_4444x736.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:241,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:173608,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!HtuH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a5a606b-4623-4527-8e9f-8722660dc4ca_4444x736.png 424w, https://substackcdn.com/image/fetch/$s_!HtuH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a5a606b-4623-4527-8e9f-8722660dc4ca_4444x736.png 848w, https://substackcdn.com/image/fetch/$s_!HtuH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a5a606b-4623-4527-8e9f-8722660dc4ca_4444x736.png 1272w, https://substackcdn.com/image/fetch/$s_!HtuH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a5a606b-4623-4527-8e9f-8722660dc4ca_4444x736.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>And now, if you try to provide keys that aren&#8217;t present in the object in the first argument, Typescript will warn you.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!P0Pa!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffca32e60-fca2-46c6-8170-bdba5e7643ed_1000x370.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!P0Pa!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffca32e60-fca2-46c6-8170-bdba5e7643ed_1000x370.png 424w, https://substackcdn.com/image/fetch/$s_!P0Pa!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffca32e60-fca2-46c6-8170-bdba5e7643ed_1000x370.png 848w, https://substackcdn.com/image/fetch/$s_!P0Pa!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffca32e60-fca2-46c6-8170-bdba5e7643ed_1000x370.png 1272w, https://substackcdn.com/image/fetch/$s_!P0Pa!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffca32e60-fca2-46c6-8170-bdba5e7643ed_1000x370.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!P0Pa!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffca32e60-fca2-46c6-8170-bdba5e7643ed_1000x370.png" width="1200" height="444" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fca32e60-fca2-46c6-8170-bdba5e7643ed_1000x370.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:370,&quot;width&quot;:1000,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:56995,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!P0Pa!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffca32e60-fca2-46c6-8170-bdba5e7643ed_1000x370.png 424w, https://substackcdn.com/image/fetch/$s_!P0Pa!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffca32e60-fca2-46c6-8170-bdba5e7643ed_1000x370.png 848w, https://substackcdn.com/image/fetch/$s_!P0Pa!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffca32e60-fca2-46c6-8170-bdba5e7643ed_1000x370.png 1272w, https://substackcdn.com/image/fetch/$s_!P0Pa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffca32e60-fca2-46c6-8170-bdba5e7643ed_1000x370.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>As you can see, we get a warning when we provide a key that doesn&#8217;t exist and Typescript provides suggestions for keys you can use.</p><h4>Bonus</h4><p>There is one final thing we can do to make the function more complete and type-safe. As you can see, our function return type is unknown, which doesn&#8217;t tell us much and we would need to narrow it before using it. But we can use our type variables - the object and the key passed in, to look up the type of that property in the object.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!-XhV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6adb19f3-5b9d-4522-aa83-0df7d74c37a1_4516x652.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!-XhV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6adb19f3-5b9d-4522-aa83-0df7d74c37a1_4516x652.png 424w, https://substackcdn.com/image/fetch/$s_!-XhV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6adb19f3-5b9d-4522-aa83-0df7d74c37a1_4516x652.png 848w, https://substackcdn.com/image/fetch/$s_!-XhV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6adb19f3-5b9d-4522-aa83-0df7d74c37a1_4516x652.png 1272w, https://substackcdn.com/image/fetch/$s_!-XhV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6adb19f3-5b9d-4522-aa83-0df7d74c37a1_4516x652.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!-XhV!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6adb19f3-5b9d-4522-aa83-0df7d74c37a1_4516x652.png" width="1200" height="173.07692307692307" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6adb19f3-5b9d-4522-aa83-0df7d74c37a1_4516x652.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:210,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:149233,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!-XhV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6adb19f3-5b9d-4522-aa83-0df7d74c37a1_4516x652.png 424w, https://substackcdn.com/image/fetch/$s_!-XhV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6adb19f3-5b9d-4522-aa83-0df7d74c37a1_4516x652.png 848w, https://substackcdn.com/image/fetch/$s_!-XhV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6adb19f3-5b9d-4522-aa83-0df7d74c37a1_4516x652.png 1272w, https://substackcdn.com/image/fetch/$s_!-XhV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6adb19f3-5b9d-4522-aa83-0df7d74c37a1_4516x652.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>So, we are combining the type variables provided for the <code>getProperty</code> function, where we are annotating the return type of the function to be the type of the property for the key provided.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jJ2l!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c841ad7-3f2a-4fd8-8ab9-46fa41241e45_900x201.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jJ2l!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c841ad7-3f2a-4fd8-8ab9-46fa41241e45_900x201.png 424w, https://substackcdn.com/image/fetch/$s_!jJ2l!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c841ad7-3f2a-4fd8-8ab9-46fa41241e45_900x201.png 848w, https://substackcdn.com/image/fetch/$s_!jJ2l!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c841ad7-3f2a-4fd8-8ab9-46fa41241e45_900x201.png 1272w, https://substackcdn.com/image/fetch/$s_!jJ2l!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c841ad7-3f2a-4fd8-8ab9-46fa41241e45_900x201.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!jJ2l!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c841ad7-3f2a-4fd8-8ab9-46fa41241e45_900x201.png" width="1200" height="268" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8c841ad7-3f2a-4fd8-8ab9-46fa41241e45_900x201.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:201,&quot;width&quot;:900,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:32396,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!jJ2l!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c841ad7-3f2a-4fd8-8ab9-46fa41241e45_900x201.png 424w, https://substackcdn.com/image/fetch/$s_!jJ2l!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c841ad7-3f2a-4fd8-8ab9-46fa41241e45_900x201.png 848w, https://substackcdn.com/image/fetch/$s_!jJ2l!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c841ad7-3f2a-4fd8-8ab9-46fa41241e45_900x201.png 1272w, https://substackcdn.com/image/fetch/$s_!jJ2l!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c841ad7-3f2a-4fd8-8ab9-46fa41241e45_900x201.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>In Typescript, this is known as indexed access types. Indexed access types allow us to look up specific properties of other types. You can learn more about this in a previous issue <a href="https://www.allthingstypescript.dev/p/indexed-access-types-in-typescript">here</a>.</p><div><hr></div><p><strong>If you like my content and want to support my work, please consider supporting me (</strong><em><strong>you can buy me a double latte, the juice that powers my creativity and determination &#9749;&#65039;</strong></em><strong>) through <a href="https://github.com/sponsors/mainawycliffe/">Github Sponsors</a>.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://github.com/sponsors/mainawycliffe/&quot;,&quot;text&quot;:&quot;&#9749;&#65039; Consider buying me a cup of coffee&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://github.com/sponsors/mainawycliffe/"><span>&#9749;&#65039; Consider buying me a cup of coffee</span></a></p><div><hr></div><h3>Conclusion</h3><p>In today&#8217;s issue, we discussed generic constraints and why they are important in Typescript for increased type safety and improved developer experience. Generic constraints enable us to provide guard rails to the generics types so that we can be certain that the type of data that can be passed in is what is expected leading to enhanced type safety.</p><p>This hasn&#8217;t been an easy issue to write, breaking down the concepts into something easy to understand and I hope you found it very illuminating. </p><p>So, what&#8217;s next? <strong>In the next couple of issues, we will look at default types for generics types and mapped types in Typescript, but not next week, because we have our <a href="https://www.allthingstypescript.dev/s/monthly-newsletter">monthly newsletter</a></strong>.</p><div><hr></div><p>As always, I am looking for suggestions for future issues, such as any library you would like me to cover in the future. If you have any, feel free to add a comment below. </p><p>You can also share and like this issue to help reach more people. We are getting closer to <strong>2000 Subscribers</strong> and I will appreciate any help you can give.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/p/generic-constraints-a-gentle-introduction?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/p/generic-constraints-a-gentle-introduction?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><p>That&#8217;s it from me and until next week, keep on learning.</p>]]></content:encoded></item><item><title><![CDATA[A Gentle Introduction to Generics in Typescript]]></title><description><![CDATA[Have you ever looked at Typescript generics and wondered what they are? This post is going to help understand Typescript generics and how to use them.]]></description><link>https://www.allthingstypescript.dev/p/a-gentle-introduction-to-generics</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/a-gentle-introduction-to-generics</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 11 Mar 2024 05:58:29 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/dcd0c857-8ca0-409a-bd1c-3154597b94c8_9048x5612.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>One of the more advanced topics in Typescript is Generics and understanding and adding them to your Typescript coding toolbox can be very powerful. As we continue with our journey of learning Typescript and building amazing projects, we want to have more and more tools that we can use to make our job easier, without making compromises when possible.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><h3>What are generics?</h3><p>Let&#8217;s see a useful case on why generics can be very important and even crucial. Let&#8217;s say we want to build a function to sort strings. It would look something like this.</p><pre><code>function sortString(str: string[]): string[] {
    // sort algorithm
}</code></pre><p>But after a while, the requirements of our project change, and we need to be able to sort numbers as well. The algorithm to sort the strings and numbers is the same but to have it strongly typed we would need to create a second function for that as well.</p><pre><code>function sortNumber(num: number[]): number[] {
    // sort algorithm
}</code></pre><p>In the future, the requirements of our project could change and we could end up sorting different types of variables. As you can see, we could end up duplicating the original function, more and more, as we need to be able to sort different types of arrays.</p><h4>Can we use the <code>any</code> Type to solve this problem</h4><p>Yes, we could. But this comes with one huge disadvantage, compared to having multiple functions, we lose type-safety and type precision, and we certainly don&#8217;t want this.</p><p>You can learn more about why you should avoid using the <code>any</code> type in one of my previous issues <a href="https://www.allthingstypescript.dev/p/why-avoid-the-any-type-in-typescript">here</a>.</p><pre><code><code>function sort(num: any[]): any[] {
//                  ^ DON'T DO THIS
    // sort algorithm
}</code></code></pre><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://github.com/sponsors/mainawycliffe/&quot;,&quot;text&quot;:&quot;&#9749;&#65039; Consider buying me a cup of coffee&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://github.com/sponsors/mainawycliffe/"><span>&#9749;&#65039; Consider buying me a cup of coffee</span></a></p><h4>Is there a better solution?</h4><p>Before I can answer that, let&#8217;s recap. <em>So, we want a solution that allows us to write a single sort function, without compromising type-safety and losing type precision</em>. Back to the question, yes, there is a better solution.</p><p>What if, instead of writing a sort function with static types for arguments and return values as we did previously, we could write a function whose types can be specified when the function is being called?</p><p>So, in our case, we would allow the caller of our sort function to specify the type of function <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments">argument</a> (the array to be sorted) and output (sorted array).</p><p>This is exactly what generics allow us to achieve in programming, and <a href="https://en.wikipedia.org/wiki/Generic_programming">Wikipedia</a> has a very good definition of it.</p><div class="pullquote"><p>Generic programming is a style of computer programming in which algorithms are written in terms of data types to-be-specified-later that are then instantiated when needed for specific types provided as parameters. </p></div><p>To break the above down in relationship to our sort function requirement, we want to write our sort function algorithm, without tying that algorithm to a specific type - <code>string</code>, <code>number</code>, <code>boolean</code>, etc. but also allow the caller to let us know the type that we can use for type-checking.</p><h4>So, how do they work?</h4><p>In Typescript, we use generics by providing the type variable inside angle brackets (<code>&lt;Type&gt;</code>). A type variable is a special kind of variable that works on types rather than values. You have free reign in naming the type variables, just like the naming of other variables.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rjR8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb49dec4e-5984-427e-8865-a486c9355f5b_600x453.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rjR8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb49dec4e-5984-427e-8865-a486c9355f5b_600x453.jpeg 424w, https://substackcdn.com/image/fetch/$s_!rjR8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb49dec4e-5984-427e-8865-a486c9355f5b_600x453.jpeg 848w, https://substackcdn.com/image/fetch/$s_!rjR8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb49dec4e-5984-427e-8865-a486c9355f5b_600x453.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!rjR8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb49dec4e-5984-427e-8865-a486c9355f5b_600x453.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rjR8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb49dec4e-5984-427e-8865-a486c9355f5b_600x453.jpeg" width="600" height="453" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b49dec4e-5984-427e-8865-a486c9355f5b_600x453.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:453,&quot;width&quot;:600,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rjR8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb49dec4e-5984-427e-8865-a486c9355f5b_600x453.jpeg 424w, https://substackcdn.com/image/fetch/$s_!rjR8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb49dec4e-5984-427e-8865-a486c9355f5b_600x453.jpeg 848w, https://substackcdn.com/image/fetch/$s_!rjR8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb49dec4e-5984-427e-8865-a486c9355f5b_600x453.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!rjR8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb49dec4e-5984-427e-8865-a486c9355f5b_600x453.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>For instance, in functions, we provide the type variable, in angle brackets (<code>&lt;Type&gt;</code>), in between the function name and brackets used to specify the arguments. We will see about generic classes, types, and interfaces later on. The type specified here can then be used within the function, or for arguments and return types, as shown below.</p><pre><code>function sort&lt;<strong>SortType</strong>&gt;(genericInput: <strong>SortType</strong>[]): <strong>SortType</strong>[] {
~~~~~~~~~~~~~~   ^ Type Variable
    return genericInput.sort();
}</code></pre><p>In the above function, we are specifying the type variable, which is the <code>SortType</code> type, and then for the function argument, we use <code>SortType</code> as the type of the first function argument and output. And this is how we use the above function:</p><pre><code>const x = sort&lt;string&gt;(["1", "2", "3"]);</code></pre><p>We are providing the string type, as the type variable and now Typescript can use the information to type-check the function arguments and outputs.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!szne!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa73fe8cc-8372-46a8-a4cd-e29e50f65ebc_357x54.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!szne!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa73fe8cc-8372-46a8-a4cd-e29e50f65ebc_357x54.png 424w, https://substackcdn.com/image/fetch/$s_!szne!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa73fe8cc-8372-46a8-a4cd-e29e50f65ebc_357x54.png 848w, https://substackcdn.com/image/fetch/$s_!szne!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa73fe8cc-8372-46a8-a4cd-e29e50f65ebc_357x54.png 1272w, https://substackcdn.com/image/fetch/$s_!szne!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa73fe8cc-8372-46a8-a4cd-e29e50f65ebc_357x54.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!szne!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa73fe8cc-8372-46a8-a4cd-e29e50f65ebc_357x54.png" width="357" height="54" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a73fe8cc-8372-46a8-a4cd-e29e50f65ebc_357x54.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:54,&quot;width&quot;:357,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:12156,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!szne!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa73fe8cc-8372-46a8-a4cd-e29e50f65ebc_357x54.png 424w, https://substackcdn.com/image/fetch/$s_!szne!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa73fe8cc-8372-46a8-a4cd-e29e50f65ebc_357x54.png 848w, https://substackcdn.com/image/fetch/$s_!szne!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa73fe8cc-8372-46a8-a4cd-e29e50f65ebc_357x54.png 1272w, https://substackcdn.com/image/fetch/$s_!szne!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa73fe8cc-8372-46a8-a4cd-e29e50f65ebc_357x54.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This works for a variety of types:</p><pre><code>const nums = sort&lt;number&gt;([1, 2, 4, 5])
//            ^? function sort(input: number[]): number[]

const str = sort&lt;string&gt;(["str", "str2"])
//            ^? function sort(input: string[]): string

// we can even mix up things now
const both = sort&lt;string | number&gt;([1, 2, "str"])
//            ^? function sort(input: (string | number)[]): (string | number)[]     </code></pre><p>If we try a different type other than the one provided in our generic type variable, Typescript is going to throw an error, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WUVG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2559d0ee-b6c7-40e3-9233-f44b9efa8225_778x85.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WUVG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2559d0ee-b6c7-40e3-9233-f44b9efa8225_778x85.png 424w, https://substackcdn.com/image/fetch/$s_!WUVG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2559d0ee-b6c7-40e3-9233-f44b9efa8225_778x85.png 848w, https://substackcdn.com/image/fetch/$s_!WUVG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2559d0ee-b6c7-40e3-9233-f44b9efa8225_778x85.png 1272w, https://substackcdn.com/image/fetch/$s_!WUVG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2559d0ee-b6c7-40e3-9233-f44b9efa8225_778x85.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WUVG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2559d0ee-b6c7-40e3-9233-f44b9efa8225_778x85.png" width="778" height="85" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2559d0ee-b6c7-40e3-9233-f44b9efa8225_778x85.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:85,&quot;width&quot;:778,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:20230,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WUVG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2559d0ee-b6c7-40e3-9233-f44b9efa8225_778x85.png 424w, https://substackcdn.com/image/fetch/$s_!WUVG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2559d0ee-b6c7-40e3-9233-f44b9efa8225_778x85.png 848w, https://substackcdn.com/image/fetch/$s_!WUVG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2559d0ee-b6c7-40e3-9233-f44b9efa8225_778x85.png 1272w, https://substackcdn.com/image/fetch/$s_!WUVG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2559d0ee-b6c7-40e3-9233-f44b9efa8225_778x85.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption"><em><strong>Typescript will type-check the function arguments against the Type variable we provided.</strong></em></figcaption></figure></div><p>And just like functions arguments, you can have multiple type variables:</p><pre><code>function manyGenerics&lt;<strong>A</strong>, <strong>B</strong>, <strong>C</strong>, <strong>D</strong>&gt;(a: <strong>A</strong>, b: <strong>B</strong>, c: <strong>C</strong>, d: <strong>D</strong>): [<strong>A</strong>, <strong>B</strong>, <strong>C</strong>, <strong>D</strong>] {
    return [a,b,c,d]
}</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!DN-K!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3752623-8362-4023-ab7a-b319ccb6fd54_500x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DN-K!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3752623-8362-4023-ab7a-b319ccb6fd54_500x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!DN-K!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3752623-8362-4023-ab7a-b319ccb6fd54_500x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!DN-K!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3752623-8362-4023-ab7a-b319ccb6fd54_500x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!DN-K!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3752623-8362-4023-ab7a-b319ccb6fd54_500x500.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DN-K!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3752623-8362-4023-ab7a-b319ccb6fd54_500x500.jpeg" width="500" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d3752623-8362-4023-ab7a-b319ccb6fd54_500x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:500,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!DN-K!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3752623-8362-4023-ab7a-b319ccb6fd54_500x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!DN-K!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3752623-8362-4023-ab7a-b319ccb6fd54_500x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!DN-K!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3752623-8362-4023-ab7a-b319ccb6fd54_500x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!DN-K!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3752623-8362-4023-ab7a-b319ccb6fd54_500x500.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Do I have to provide the type variable every time?</h4><p>The answer is no, and this is because Typescript can use Type <a href="https://www.allthingstypescript.dev/p/all-things-typescript-newsletter-issue-13-type-inference-in-typescript-1211063">inference</a> so you don&#8217;t have to provide the functions generic type with every call.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FH73!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39aaa4e2-bc8c-4fd1-a321-9eb9ad0b065c_564x61.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FH73!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39aaa4e2-bc8c-4fd1-a321-9eb9ad0b065c_564x61.png 424w, https://substackcdn.com/image/fetch/$s_!FH73!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39aaa4e2-bc8c-4fd1-a321-9eb9ad0b065c_564x61.png 848w, https://substackcdn.com/image/fetch/$s_!FH73!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39aaa4e2-bc8c-4fd1-a321-9eb9ad0b065c_564x61.png 1272w, https://substackcdn.com/image/fetch/$s_!FH73!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39aaa4e2-bc8c-4fd1-a321-9eb9ad0b065c_564x61.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FH73!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39aaa4e2-bc8c-4fd1-a321-9eb9ad0b065c_564x61.png" width="564" height="61" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39aaa4e2-bc8c-4fd1-a321-9eb9ad0b065c_564x61.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:61,&quot;width&quot;:564,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:14310,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!FH73!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39aaa4e2-bc8c-4fd1-a321-9eb9ad0b065c_564x61.png 424w, https://substackcdn.com/image/fetch/$s_!FH73!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39aaa4e2-bc8c-4fd1-a321-9eb9ad0b065c_564x61.png 848w, https://substackcdn.com/image/fetch/$s_!FH73!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39aaa4e2-bc8c-4fd1-a321-9eb9ad0b065c_564x61.png 1272w, https://substackcdn.com/image/fetch/$s_!FH73!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39aaa4e2-bc8c-4fd1-a321-9eb9ad0b065c_564x61.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>Typescript will use the type inferred for our generic input, as the type for our variable type, which can save you from having to always specify the type variable.</p><p>This is so good that many developers use generics daily without realizing it. For instance, the <code>Array.sort()</code> functions and other <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array">Array methods</a> are generic functions.</p><p>For instance, the following <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map">Array.map</a> method:</p><pre><code>[1, 5, 3, 11, 6 ].map(x =&gt; x.toString());</code></pre><p>It can also be written like this, with the type variable provided.</p><pre><code>[1, 5, 3, 11, 6].map&lt;string&gt;(x =&gt; x.toString());</code></pre><h3>Where else can we use generics?</h3><p>So far, we have seen generics in function space. But we can use generic in a variety of places, not just functions. Let&#8217;s see some of this:</p><h4>Classes and Methods</h4><p>Just like functions, when defining a class, we can request a type variable(s) for the class, which we can then use within the class.</p><pre><code>class SomeClass&lt;<strong>Type</strong>&gt;{
// ~~~~~~~~~~~~~ ^ Type Variable

    property: <strong>Type</strong>;
    //         ^ We can use the Generic type for types of properties

    doSomething(input: <strong>Type</strong>): Type {
    //                  ^ and for methods input
    }
}</code></pre><p>And on top of that, we can provide type variables for the methods themselves, if need be:</p><pre><code>class SomeClass{
    doSomething&lt;<strong>Type</strong>&gt;(input: <strong>Type</strong>): Type {
        //       ^? Type Variable
    }
}</code></pre><h4>Types and Interfaces Definition for Key-Value Objects</h4><p>When defining types for key-value objects using either <a href="https://www.allthingstypescript.dev/p/type-vs-interface-in-typescript-all">interface or type</a>, you can use generics to provide types for some of the properties.</p><pre><code>type Person&lt;<strong>Type</strong>&gt; = {
//            ^ Type Variable
    name: string;
    id: string;
    properties: <strong>Type</strong>;
}</code></pre><p>We can do the same with interfaces:</p><pre><code>interface Person&lt;<strong>Type</strong>&gt; {
//                ^ Type Variable
    name: string;
    id: string;
    properties: <strong>Type</strong>;
}</code></pre><h3>What&#8217;s next?</h3><p>So far, we have had a very brief introduction to Typescript generics, where we looked at creating Generics in Typescript and how we can use them. Generics are powerful and are very useful and in our next issue, next week, we are going to look at constraining our generic type.</p><p>See you next week and subscribe to not miss the next issue.</p><div><hr></div><p><strong>If you like my content and want to support my work, please consider supporting me (</strong><em><strong>you can buy me a double latte, the juice that powers my creativity and determination &#9749;&#65039;</strong></em><strong>) through <a href="https://github.com/sponsors/mainawycliffe/">Github Sponsors</a>.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://github.com/sponsors/mainawycliffe/&quot;,&quot;text&quot;:&quot;&#9749;&#65039; Consider buying me a cup of coffee&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://github.com/sponsors/mainawycliffe/"><span>&#9749;&#65039; Consider buying me a cup of coffee</span></a></p><div><hr></div><h3>Conclusion</h3><p>In this issue, we looked at Typescript generics. We looked at a simple example of a sorting function, that enabled us to see in which situations generics can be very useful and how generics can help us write less code without compromising type safety. </p><p>In the next issue, we will continue looking at generics and some advanced techniques that we can do with generics to even lead to more Type safety. </p><p>So, until next, keep on learning.</p>]]></content:encoded></item><item><title><![CDATA[Feb 2024 Monthly Newsletter - TypeScript 5.4 RC and more]]></title><description><![CDATA[A hand-curated list of Articles and Videos from All Things Typescript and other authors for the month of February 2024.]]></description><link>https://www.allthingstypescript.dev/p/feb-2024-monthly-newsletter-typescript</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/feb-2024-monthly-newsletter-typescript</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 04 Mar 2024 05:17:05 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/d8f80071-10da-402d-9902-a464ad782d64_3840x2160.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey everyone, my name is Maina Wycliffe. Every month, I curate a list of resources related to Typescript (and related stacks) that I find interesting and informative in the month. In this issue, we take a look at some of the interesting content I came across in the last month.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><h2>Announcements</h2><h4><a href="https://devblogs.microsoft.com/typescript/announcing-typescript-5-4-rc/">Typescript 5.4 RC Released </a></h4><p>We are getting closer to the release of Typescript 5.4 and in February, the release candidate was announced.</p><p>Here is a summary of the features to expect:</p><ul><li><p>Assignment Narrowing is Preserved in Closures</p></li><li><p>New <strong>NoInfer</strong> Utility Type</p></li><li><p>Type Declarations for <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy">Object.groupBy</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/groupBy">Map.groupBy</a></p></li><li><p>Support for require() calls in <code>--moduleResolution</code> bundler and <code>--module preserve</code></p></li><li><p>Checked Import Attributes and Assertions</p></li><li><p>Quick Fix for Adding Missing Parameters</p></li></ul><p><em>Let me know if there are any features you want me to take a look at in the next few issues of All Things Typescript.</em></p><p>Please check out the release blog post <a href="https://devblogs.microsoft.com/typescript/announcing-typescript-5-4-rc/">here</a> for more information about the above features.</p><h2>All Things Typescript Feb 2024 Issues</h2><p>In February, I consistently released articles every week, except one. In case you missed any, here is a list of the issues that I released in February.</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;be2e2b2c-f2dc-40b3-a2d7-9b24ee5ce6f0&quot;,&quot;caption&quot;:&quot;In Typescript version 4.9, Typescript introduced the satisfies operator. Over the last few months, I have seen examples of it being used out in the world more often, and even I have found myself getting comfortable using it. In this issue, I want to do a deep dive and try and answer the following questions:&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;A Deep Dive into the satisfies operator in Typescript&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-02-24T20:07:07.093Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/150f856f-0aa6-4ab9-9b1b-34d4d178743a_3136x1788.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/a-deep-dive-into-the-typescript-satisfies&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:141990708,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:1,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;086b8909-0744-407e-b435-abcf5ce6fa7b&quot;,&quot;caption&quot;:&quot;Let&#8217;s start with a simple hypothesis. Let&#8217;s say you have a form for your users, for instance, a sign-in form. You need a validation schema and a Typescript type, representing the shape of the form data. Why a validation schema, you may ask? This is because data coming from untrusted sources should always be validated, for instance, form data, ensuring t&#8230;&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Schema First Typescript Types Design with Valibot&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Author of All Things Typescript and Google Developer Expert for Angular&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/623bca1e-4bc1-4d0c-a173-cc5337080e98_1151x1728.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-02-12T06:19:15.080Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7b449bb1-5363-415b-8f8b-01ccd839aa96_1280x853.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/schema-first-typescript-types-design&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:141422661,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:2,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><h2>From other authors</h2><h3>Articles</h3><h4><a href="https://www.totaltypescript.com/as-never">When 'as never' Is The Only Thing That Works by Matt Pocock</a></h4><p><strong>TLDR</strong>: <code>as never</code> is very occasionally needed in TypeScript. Let's look at an example where it's necessary.</p><p><a href="https://www.totaltypescript.com/as-never">Link</a></p><h4><a href="https://claritydev.net/blog/typescript-generics-guide">Using Generics In TypeScript: A Practical Guide by Alex Khomenko</a></h4><p><strong>TLDR</strong>: Among these features, generics stand out as one of the most powerful tools for creating flexible, reusable components without sacrificing type safety. In this blog post, we'll dive into the world of generics in TypeScript, exploring how they can be used to write cleaner, more maintainable code that is robust and easy to understand.</p><p><a href="https://claritydev.net/blog/typescript-generics-guide">Link</a></p><h4><a href="https://claritydev.net/blog/advanced-conditional-types-typescript">TypeScript Advanced Types: Working with Conditional Types by Alex Khomenko</a></h4><p><strong>TLDR</strong>: Conditional types in TypeScript provide logic to type definitions, allowing for type transformations based on certain conditions. They can be used for type inference, filtering types with utility types, building type-safe API handlers, and defensive programming. Best practices include keeping conditions simple, using type aliases and utility types, and testing types. Limitations include operating at compile-time only and not being suitable for all use cases.</p><p><a href="https://claritydev.net/blog/advanced-conditional-types-typescript">Link</a></p><h4><a href="https://dev.to/mochafreddo/understanding-asyncawait-and-promises-in-javascript-and-typescript-d0n">Understanding Async/Await and Promises in JavaScript and TypeScript by Geoffrey Kim</a></h4><p><strong>TLDR</strong>: This blog post aims to demystify the scenarios where you might or might not need to use <code>await</code> when dealing with Promises, through detailed explanations and illustrations.</p><p><a href="https://dev.to/mochafreddo/understanding-asyncawait-and-promises-in-javascript-and-typescript-d0n">Link</a></p><h4><a href="https://www.freecodecamp.org/news/basic-typescript-types/">How Types Work in TypeScript &#8211; Explained with JavaScript + TypeScript Code by Sahil Mahapatra</a></h4><p><strong>TLDR</strong>:  In this comprehensive guide, we'll explore TypeScript's basic types by comparing them with their JavaScript counterparts. I'll also clarify the differences and advantages offered by TypeScript's static typing features.</p><p><a href="https://www.freecodecamp.org/news/basic-typescript-types/">Link</a></p><h4><a href="https://johnnyreilly.com/typescript-eslint-no-unused-vars">ESLint no-unused-vars: _ ignore prefix by John Reilly</a></h4><p><strong>TLDR</strong>: One thing that I missed when switching to the ESLint option is that, with <code>noUnusedLocals</code> and <code>noUnusedParameters</code>, you can simply ignore unused variables by prefixing a variable with the <code>_</code> character. That's right, sometimes I want to declare a variable that I know I'm not going to use, and I want to do that without getting shouted at by the linter.</p><p>It turns out you can get ESLint to respect the TypeScript default of ignoring variables prefixed with <code>_</code>; <a href="https://github.com/typescript-eslint/typescript-eslint/issues/8464#issuecomment-1943325441">it's just not the default configuration for </a><code>no-unused-vars</code>. But with a little configuration, we can have it. This post is a quick guide to how to implement that configuration.</p><p><a href="https://johnnyreilly.com/typescript-eslint-no-unused-vars">Link</a></p><h3>Videos</h3><h4>There&#8217;s a fast new code editor in town by Fireship</h4><div id="youtube2-JGz7Ou0Nwo8" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;JGz7Ou0Nwo8&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/JGz7Ou0Nwo8?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>8 NEW JavaScript 2024 Features by Web Dev Simplified</h4><div id="youtube2-duNEnLUxie8" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;duNEnLUxie8&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/duNEnLUxie8?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><h4>I didn't know snapshot testing was a thing by Joshua Morony</h4><div id="youtube2-jfsgSiMwt3U" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;jfsgSiMwt3U&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/jfsgSiMwt3U?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><div><hr></div><p><strong>If you like my content and want to support my work, please consider supporting me (</strong><em><strong>you can buy me a double latte, the juice that powers my creativity and determination &#9749;&#65039;</strong></em><strong>) through <a href="https://github.com/sponsors/mainawycliffe/">Github Sponsors</a>.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/p/a-deep-dive-into-the-typescript-satisfies?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&amp;token=eyJ1c2VyX2lkIjo0MzIxNDA4NiwicG9zdF9pZCI6MTQxOTkwNzA4LCJpYXQiOjE3MDk0NzQ1NTMsImV4cCI6MTcxMjA2NjU1MywiaXNzIjoicHViLTEwMjU1NjEiLCJzdWIiOiJwb3N0LXJlYWN0aW9uIn0.wYCRh5-ceyKVcvSI58AVTf_9d2o6TBCWNVO-_lVlSiE&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://www.allthingstypescript.dev/p/a-deep-dive-into-the-typescript-satisfies?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&amp;token=eyJ1c2VyX2lkIjo0MzIxNDA4NiwicG9zdF9pZCI6MTQxOTkwNzA4LCJpYXQiOjE3MDk0NzQ1NTMsImV4cCI6MTcxMjA2NjU1MywiaXNzIjoicHViLTEwMjU1NjEiLCJzdWIiOiJwb3N0LXJlYWN0aW9uIn0.wYCRh5-ceyKVcvSI58AVTf_9d2o6TBCWNVO-_lVlSiE"><span>Share</span></a></p><div><hr></div><p>That's all for now. I'll see you again at the end of February for another monthly recap.</p>]]></content:encoded></item><item><title><![CDATA[A Deep Dive into the satisfies operator in Typescript]]></title><description><![CDATA[We take a look at the satisfies Typescript operator and how to use it, when to use it and why you should use it instead of Type annotations and typer assertions.]]></description><link>https://www.allthingstypescript.dev/p/a-deep-dive-into-the-typescript-satisfies</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/a-deep-dive-into-the-typescript-satisfies</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Sat, 24 Feb 2024 20:07:07 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/150f856f-0aa6-4ab9-9b1b-34d4d178743a_3136x1788.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In Typescript <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html">version 4.9</a>, Typescript introduced the satisfies operator. Over the last few months, I have seen examples of it being used out in the world more often, and even I have found myself getting comfortable using it. In this issue, I want to do a deep dive and try and answer the following questions:</p><ul><li><p>Why it exists</p></li><li><p>Why not annotations</p></li><li><p>Why not assertions</p></li><li><p>When to use it</p></li></ul><div><hr></div><h3><strong>&#128226; Sponsored:</strong>  Ready to Move From AI Hype to Real-World Results?</h3><p>At Unstacked Labs, we specialise in building practical AI agents, like the one in this article, that automate complex workflows and solve real business problems.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://unstacked.dev/&quot;,&quot;text&quot;:&quot;Let's discuss your company needs&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://unstacked.dev/"><span>Let's discuss your company needs</span></a></p><div><hr></div><h3>What is the <code>satisfies</code> Typescript operator</h3><p>According to the Typescript <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html">docs</a>, the new <code>satisfies</code> operator lets us validate that the type of an expression matches some type, without changing the resulting type of that expression.</p><p>So, what does this mean? When we add type annotations to a variable, Typescript doesn&#8217;t usually infer the Type to its narrowest possible value. So, if we say a variable&#8217;s type is a string, Typescript takes our word for it and checks the value we assign to it against the type annotation we provided, as shown below:</p><pre><code>const str: string = "hello"</code></pre><p>In the case above, despite us using const, and having all the information at hand to narrow the above type to the <code>hello</code> literal type, Typescript doesn&#8217;t do that and instead takes our annotation as the type of the variable.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!LTzJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdbc50f0-df55-478a-aa0b-2b3f5b8b85ea_337x97.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!LTzJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdbc50f0-df55-478a-aa0b-2b3f5b8b85ea_337x97.png 424w, https://substackcdn.com/image/fetch/$s_!LTzJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdbc50f0-df55-478a-aa0b-2b3f5b8b85ea_337x97.png 848w, https://substackcdn.com/image/fetch/$s_!LTzJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdbc50f0-df55-478a-aa0b-2b3f5b8b85ea_337x97.png 1272w, https://substackcdn.com/image/fetch/$s_!LTzJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdbc50f0-df55-478a-aa0b-2b3f5b8b85ea_337x97.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!LTzJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdbc50f0-df55-478a-aa0b-2b3f5b8b85ea_337x97.png" width="337" height="97" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bdbc50f0-df55-478a-aa0b-2b3f5b8b85ea_337x97.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:97,&quot;width&quot;:337,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:12419,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!LTzJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdbc50f0-df55-478a-aa0b-2b3f5b8b85ea_337x97.png 424w, https://substackcdn.com/image/fetch/$s_!LTzJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdbc50f0-df55-478a-aa0b-2b3f5b8b85ea_337x97.png 848w, https://substackcdn.com/image/fetch/$s_!LTzJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdbc50f0-df55-478a-aa0b-2b3f5b8b85ea_337x97.png 1272w, https://substackcdn.com/image/fetch/$s_!LTzJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdbc50f0-df55-478a-aa0b-2b3f5b8b85ea_337x97.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This is in contrast to when we allow type inference to work for us, instead of explicitly providing Type annotations, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WZiz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ebadb3-c18b-49ca-86bf-3dc167106eda_334x80.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WZiz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ebadb3-c18b-49ca-86bf-3dc167106eda_334x80.png 424w, https://substackcdn.com/image/fetch/$s_!WZiz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ebadb3-c18b-49ca-86bf-3dc167106eda_334x80.png 848w, https://substackcdn.com/image/fetch/$s_!WZiz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ebadb3-c18b-49ca-86bf-3dc167106eda_334x80.png 1272w, https://substackcdn.com/image/fetch/$s_!WZiz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ebadb3-c18b-49ca-86bf-3dc167106eda_334x80.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WZiz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ebadb3-c18b-49ca-86bf-3dc167106eda_334x80.png" width="334" height="80" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a3ebadb3-c18b-49ca-86bf-3dc167106eda_334x80.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:80,&quot;width&quot;:334,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:11842,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!WZiz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ebadb3-c18b-49ca-86bf-3dc167106eda_334x80.png 424w, https://substackcdn.com/image/fetch/$s_!WZiz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ebadb3-c18b-49ca-86bf-3dc167106eda_334x80.png 848w, https://substackcdn.com/image/fetch/$s_!WZiz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ebadb3-c18b-49ca-86bf-3dc167106eda_334x80.png 1272w, https://substackcdn.com/image/fetch/$s_!WZiz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3ebadb3-c18b-49ca-86bf-3dc167106eda_334x80.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>As you can see from the above example, when we use explicit type annotations, we lose the narrower type that we would have gotten if we had let Typescript infer the type for us. </p><p>Why is this important, you may ask? To answer this, let me show you an example, take the following two functions:</p><pre><code>function a(input: string) {
  // do something
}

function b(input: "hello") {
  // do something 
}</code></pre><p>The first function accepts a string and the second accepts a literal type - <code>hello</code>. With the first variable, we annotated the type explicitly, this can be passed to the first function without any issues. </p><p>But in the second function, despite the first variable value being hello, Typescript will make you jump through to accept the variable, simply because it expects a narrower type than the type of string we provided.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!D5Iz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f3dc42-eaf0-4fef-9e3b-6fe89307e4a0_725x141.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!D5Iz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f3dc42-eaf0-4fef-9e3b-6fe89307e4a0_725x141.png 424w, https://substackcdn.com/image/fetch/$s_!D5Iz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f3dc42-eaf0-4fef-9e3b-6fe89307e4a0_725x141.png 848w, https://substackcdn.com/image/fetch/$s_!D5Iz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f3dc42-eaf0-4fef-9e3b-6fe89307e4a0_725x141.png 1272w, https://substackcdn.com/image/fetch/$s_!D5Iz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f3dc42-eaf0-4fef-9e3b-6fe89307e4a0_725x141.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!D5Iz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f3dc42-eaf0-4fef-9e3b-6fe89307e4a0_725x141.png" width="725" height="141" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f3f3dc42-eaf0-4fef-9e3b-6fe89307e4a0_725x141.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:141,&quot;width&quot;:725,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:24167,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!D5Iz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f3dc42-eaf0-4fef-9e3b-6fe89307e4a0_725x141.png 424w, https://substackcdn.com/image/fetch/$s_!D5Iz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f3dc42-eaf0-4fef-9e3b-6fe89307e4a0_725x141.png 848w, https://substackcdn.com/image/fetch/$s_!D5Iz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f3dc42-eaf0-4fef-9e3b-6fe89307e4a0_725x141.png 1272w, https://substackcdn.com/image/fetch/$s_!D5Iz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff3f3dc42-eaf0-4fef-9e3b-6fe89307e4a0_725x141.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>In the second function, you would need to narrow the variable for it to work, as shown below:</p><pre><code>if(str === "hello")  b(str);</code></pre><blockquote><p>In our case above, we are using control flow to narrow the type of str variable to hello, from a broader type of string. You can learn more about type narrowing in this previous <a href="https://www.allthingstypescript.dev/p/narrowing-types-in-typescript">issue</a>.</p></blockquote><h4>How does the <code>satisfies</code> operator help?</h4><p>If you are wondering what this has to do with the satisfies operator, I am getting to the point in a moment.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SHOO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e2df56-0d01-4c28-8808-c65538f3196c_500x574.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SHOO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e2df56-0d01-4c28-8808-c65538f3196c_500x574.jpeg 424w, https://substackcdn.com/image/fetch/$s_!SHOO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e2df56-0d01-4c28-8808-c65538f3196c_500x574.jpeg 848w, https://substackcdn.com/image/fetch/$s_!SHOO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e2df56-0d01-4c28-8808-c65538f3196c_500x574.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!SHOO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e2df56-0d01-4c28-8808-c65538f3196c_500x574.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SHOO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e2df56-0d01-4c28-8808-c65538f3196c_500x574.jpeg" width="500" height="574" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d7e2df56-0d01-4c28-8808-c65538f3196c_500x574.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:574,&quot;width&quot;:500,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!SHOO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e2df56-0d01-4c28-8808-c65538f3196c_500x574.jpeg 424w, https://substackcdn.com/image/fetch/$s_!SHOO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e2df56-0d01-4c28-8808-c65538f3196c_500x574.jpeg 848w, https://substackcdn.com/image/fetch/$s_!SHOO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e2df56-0d01-4c28-8808-c65538f3196c_500x574.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!SHOO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd7e2df56-0d01-4c28-8808-c65538f3196c_500x574.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>What if you wanted to ensure that <code>str</code> is a string, without losing the narrowness of the inferred type? If you remember at the beginning, we said that the satisfies operator allows you to validate (think type-check) a variable type without changing the resulting type of that expression. The <strong>resulting type</strong> being the Type inferred by Typescript in the absence of explicit type annotation. </p><p>So, if we went back to our example, instead of using type annotation for our variable, and instead we used the <code>satisfies</code> operator, as shown below:</p><pre><code>const str = "hello" satisfies string;</code></pre><p>Our variable wouldn&#8217;t carry the type of string as it did previously when we used type annotations, but the resulting inferred type is retained.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!S-Y3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a78b34e-3b93-4bfe-bd3b-f30f703fb755_381x84.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!S-Y3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a78b34e-3b93-4bfe-bd3b-f30f703fb755_381x84.png 424w, https://substackcdn.com/image/fetch/$s_!S-Y3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a78b34e-3b93-4bfe-bd3b-f30f703fb755_381x84.png 848w, https://substackcdn.com/image/fetch/$s_!S-Y3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a78b34e-3b93-4bfe-bd3b-f30f703fb755_381x84.png 1272w, https://substackcdn.com/image/fetch/$s_!S-Y3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a78b34e-3b93-4bfe-bd3b-f30f703fb755_381x84.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!S-Y3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a78b34e-3b93-4bfe-bd3b-f30f703fb755_381x84.png" width="381" height="84" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7a78b34e-3b93-4bfe-bd3b-f30f703fb755_381x84.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:84,&quot;width&quot;:381,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:13193,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!S-Y3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a78b34e-3b93-4bfe-bd3b-f30f703fb755_381x84.png 424w, https://substackcdn.com/image/fetch/$s_!S-Y3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a78b34e-3b93-4bfe-bd3b-f30f703fb755_381x84.png 848w, https://substackcdn.com/image/fetch/$s_!S-Y3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a78b34e-3b93-4bfe-bd3b-f30f703fb755_381x84.png 1272w, https://substackcdn.com/image/fetch/$s_!S-Y3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a78b34e-3b93-4bfe-bd3b-f30f703fb755_381x84.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>And this means, we can now pass the variable to the second function without needing to narrow the type.</p><pre><code>const str = "hello" satisfies string;
//    ^? type = "hello"

// this works now
b(str);</code></pre><h4>&#8220;Real-world&#8221; Example</h4><p>Okay, I know what you are thinking, this isn&#8217;t a practical example. And that&#8217;s true, but I needed a straightforward example, that hopefully was easy to follow and understand, which I hope worked, and if it didn&#8217;t let me know in the comment section below. </p><p>Let&#8217;s see a more realistic example. Let&#8217;s say we want an object, with property names being <code>string</code> and values being either <code>string</code>, <code>number</code>, or <code>boolean</code>, as shown below:</p><pre><code>const user = {
    firstName: "John",
    lastName: "Doe",
    age: 5,
    isMember: true
};</code></pre><p>With the above object, we might want to have Typescript type-check the content of the object. In such a situation we have two options:</p><ul><li><p>providing explicit type annotations </p></li><li><p>or the <code>satisfies</code> operator. </p></li></ul><p>If we use type annotations and try accessing one of the properties, Typescript doesn&#8217;t know the properties that are inside the object or their types, this is despite us explicitly adding them and adding them upfront.</p><pre><code>const user: Record&lt;string, string | number | boolean&gt; = {
    firstName: "John",
    lastName: "Doe",
    age: 5,
    isMember: true
};</code></pre><p>In this case, Typescript goes with the type from our Type annotation for the property values.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!QLEg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff021f734-836d-49d1-8f8c-9174434a759e_525x178.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!QLEg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff021f734-836d-49d1-8f8c-9174434a759e_525x178.png 424w, https://substackcdn.com/image/fetch/$s_!QLEg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff021f734-836d-49d1-8f8c-9174434a759e_525x178.png 848w, https://substackcdn.com/image/fetch/$s_!QLEg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff021f734-836d-49d1-8f8c-9174434a759e_525x178.png 1272w, https://substackcdn.com/image/fetch/$s_!QLEg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff021f734-836d-49d1-8f8c-9174434a759e_525x178.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!QLEg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff021f734-836d-49d1-8f8c-9174434a759e_525x178.png" width="525" height="178" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f021f734-836d-49d1-8f8c-9174434a759e_525x178.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:178,&quot;width&quot;:525,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:25825,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!QLEg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff021f734-836d-49d1-8f8c-9174434a759e_525x178.png 424w, https://substackcdn.com/image/fetch/$s_!QLEg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff021f734-836d-49d1-8f8c-9174434a759e_525x178.png 848w, https://substackcdn.com/image/fetch/$s_!QLEg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff021f734-836d-49d1-8f8c-9174434a759e_525x178.png 1272w, https://substackcdn.com/image/fetch/$s_!QLEg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff021f734-836d-49d1-8f8c-9174434a759e_525x178.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This isn&#8217;t great, for one, we can include properties that don&#8217;t exist inside our user object or typos and which may introduce bugs within our codebase. However, if we went with the <code>satisfies</code> operator, then things work out quite differently and for the better.</p><pre><code>const user = {
    firstName: "John",
    lastName: "Doe",
    age: 5,
} satisfies Record&lt;string, string | number | boolean&gt;</code></pre><p>If we try and access the property firstName, we get both auto-completion &#128076;&#127998; and it has the correct type, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZYeP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb400cea4-2aef-4e8d-8e49-b1a9d7650f11_499x186.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZYeP!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb400cea4-2aef-4e8d-8e49-b1a9d7650f11_499x186.png 424w, https://substackcdn.com/image/fetch/$s_!ZYeP!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb400cea4-2aef-4e8d-8e49-b1a9d7650f11_499x186.png 848w, https://substackcdn.com/image/fetch/$s_!ZYeP!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb400cea4-2aef-4e8d-8e49-b1a9d7650f11_499x186.png 1272w, https://substackcdn.com/image/fetch/$s_!ZYeP!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb400cea4-2aef-4e8d-8e49-b1a9d7650f11_499x186.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZYeP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb400cea4-2aef-4e8d-8e49-b1a9d7650f11_499x186.png" width="499" height="186" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b400cea4-2aef-4e8d-8e49-b1a9d7650f11_499x186.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:186,&quot;width&quot;:499,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:24134,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZYeP!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb400cea4-2aef-4e8d-8e49-b1a9d7650f11_499x186.png 424w, https://substackcdn.com/image/fetch/$s_!ZYeP!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb400cea4-2aef-4e8d-8e49-b1a9d7650f11_499x186.png 848w, https://substackcdn.com/image/fetch/$s_!ZYeP!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb400cea4-2aef-4e8d-8e49-b1a9d7650f11_499x186.png 1272w, https://substackcdn.com/image/fetch/$s_!ZYeP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb400cea4-2aef-4e8d-8e49-b1a9d7650f11_499x186.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>We also can&#8217;t access properties we didn&#8217;t explicitly add to the user object.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CRtJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F319929b7-e7c2-4620-a392-e5c8f0127b16_1001x182.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CRtJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F319929b7-e7c2-4620-a392-e5c8f0127b16_1001x182.png 424w, https://substackcdn.com/image/fetch/$s_!CRtJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F319929b7-e7c2-4620-a392-e5c8f0127b16_1001x182.png 848w, https://substackcdn.com/image/fetch/$s_!CRtJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F319929b7-e7c2-4620-a392-e5c8f0127b16_1001x182.png 1272w, https://substackcdn.com/image/fetch/$s_!CRtJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F319929b7-e7c2-4620-a392-e5c8f0127b16_1001x182.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CRtJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F319929b7-e7c2-4620-a392-e5c8f0127b16_1001x182.png" width="1001" height="182" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/319929b7-e7c2-4620-a392-e5c8f0127b16_1001x182.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:182,&quot;width&quot;:1001,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:32094,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CRtJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F319929b7-e7c2-4620-a392-e5c8f0127b16_1001x182.png 424w, https://substackcdn.com/image/fetch/$s_!CRtJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F319929b7-e7c2-4620-a392-e5c8f0127b16_1001x182.png 848w, https://substackcdn.com/image/fetch/$s_!CRtJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F319929b7-e7c2-4620-a392-e5c8f0127b16_1001x182.png 1272w, https://substackcdn.com/image/fetch/$s_!CRtJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F319929b7-e7c2-4620-a392-e5c8f0127b16_1001x182.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>And we also get the excess property checking we got when we provided explicit type annotations.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9wfk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3c66d63-1983-4855-8809-f0ddd88c4f93_633x179.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9wfk!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3c66d63-1983-4855-8809-f0ddd88c4f93_633x179.png 424w, https://substackcdn.com/image/fetch/$s_!9wfk!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3c66d63-1983-4855-8809-f0ddd88c4f93_633x179.png 848w, https://substackcdn.com/image/fetch/$s_!9wfk!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3c66d63-1983-4855-8809-f0ddd88c4f93_633x179.png 1272w, https://substackcdn.com/image/fetch/$s_!9wfk!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3c66d63-1983-4855-8809-f0ddd88c4f93_633x179.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9wfk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3c66d63-1983-4855-8809-f0ddd88c4f93_633x179.png" width="633" height="179" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d3c66d63-1983-4855-8809-f0ddd88c4f93_633x179.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:179,&quot;width&quot;:633,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:30665,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9wfk!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3c66d63-1983-4855-8809-f0ddd88c4f93_633x179.png 424w, https://substackcdn.com/image/fetch/$s_!9wfk!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3c66d63-1983-4855-8809-f0ddd88c4f93_633x179.png 848w, https://substackcdn.com/image/fetch/$s_!9wfk!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3c66d63-1983-4855-8809-f0ddd88c4f93_633x179.png 1272w, https://substackcdn.com/image/fetch/$s_!9wfk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3c66d63-1983-4855-8809-f0ddd88c4f93_633x179.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>This has a few benefits:</p><ul><li><p>Improved type safety as demonstrated in the last example. This is twofold: </p><ul><li><p>validating the object properties and values are of certain types, and</p></li><li><p>avoiding changing the objects&#8217; inferred type, which means the property value types are narrow instead of wide.</p></li></ul></li><li><p>Reduced the amount of code we write - we don&#8217;t need to engage in type narrowing to use our properties above, they already have a narrow type.</p></li></ul><h4>Use Cases</h4><p>Now that we have seen how we can some of the use cases of the <code>satisfies</code> operator.</p><h5>Constraining the type of variable</h5><p>We can use the satisfies operator to constrain or limit the types that can be used for a variable, without interfering with the type inference. A good example of this is our example above, where we limited our variable <code>str</code> to string, but let it be inferred as a narrower type by typescript, using the available information.</p><pre><code><code>const str = "hello" satisfies string;</code></code></pre><h5>Constraining the Property Names of an Object and Catching Excess Properties</h5><p>We can use the <code>satisfies</code> operator to make sure our object only has the keys and nothing extra.</p><pre><code>type Circle = {
  shape: "circle";
  radius: number;
}

const circle = {
  shape: "circle",
  radius: 4,
  anotherKey: 4
  // this will throw an error as shown bolow
} satisfies Circle;</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Y84D!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F315da09e-0c76-4c4e-a44a-718b0ce3381a_840x194.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Y84D!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F315da09e-0c76-4c4e-a44a-718b0ce3381a_840x194.png 424w, https://substackcdn.com/image/fetch/$s_!Y84D!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F315da09e-0c76-4c4e-a44a-718b0ce3381a_840x194.png 848w, https://substackcdn.com/image/fetch/$s_!Y84D!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F315da09e-0c76-4c4e-a44a-718b0ce3381a_840x194.png 1272w, https://substackcdn.com/image/fetch/$s_!Y84D!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F315da09e-0c76-4c4e-a44a-718b0ce3381a_840x194.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Y84D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F315da09e-0c76-4c4e-a44a-718b0ce3381a_840x194.png" width="840" height="194" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/315da09e-0c76-4c4e-a44a-718b0ce3381a_840x194.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:194,&quot;width&quot;:840,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:36157,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Y84D!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F315da09e-0c76-4c4e-a44a-718b0ce3381a_840x194.png 424w, https://substackcdn.com/image/fetch/$s_!Y84D!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F315da09e-0c76-4c4e-a44a-718b0ce3381a_840x194.png 848w, https://substackcdn.com/image/fetch/$s_!Y84D!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F315da09e-0c76-4c4e-a44a-718b0ce3381a_840x194.png 1272w, https://substackcdn.com/image/fetch/$s_!Y84D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F315da09e-0c76-4c4e-a44a-718b0ce3381a_840x194.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h5>Constraining the values of Properties, the opposite of the above</h5><p>The following will throw an error because the shape property is missing.</p><pre><code>const circle = {
  radius: 4,
} satisfies Record&lt;any, number&gt;;</code></pre><h5>Ensuring an Interface is Implemented</h5><pre><code>interface Person {
  delete: () =&gt; void;
  get: () =&gt; Record&lt;string, unknown&gt;
}

const thisPersion = {
  delete:  () =&gt; {};
  get: () =&gt; ({})
} satisfies Person;</code></pre><p>For more about the <code>satisfies</code> operator, check out the following <a href="https://github.com/microsoft/TypeScript/issues/47920">issue</a>, where the discussion for adding the satisfies operator to Typescript happened.</p><h3>Conclusion</h3><p>In this issue, we took a look at the <code>satisfies</code> Typescript operator, and how it can be used to validate a type, without changing the inferred type of the expression. This is different from how both type assertions and annotations work because Typescript takes that as the type of the object, which is sometimes not desired. </p><p>When using the <code>satisfies</code> operator, Typescript only type-checks our variable against the provided type while inferring the type using the information available (Learn more about Type Widening here). </p><p>The inferred type is usually narrower, meaning it can be used without the need for narrowing, which may not be the case for using assertions and annotations, while also type-checking it against a specific type, which ensures type safety isn&#8217;t compromised.</p><div><hr></div><p><strong>If you loved today&#8217;s content and would be interested in supporting my work, please consider supporting me (</strong><em><strong>you can buy me a double latte, the juice that powers my creativity &#9749;&#65039;</strong></em><strong>) through <a href="https://github.com/sponsors/mainawycliffe/">Github Sponsors</a>.</strong></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/p/a-deep-dive-into-the-typescript-satisfies?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/p/a-deep-dive-into-the-typescript-satisfies?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div><hr></div><p>That&#8217;s it from me and until next week, keep on learning.</p>]]></content:encoded></item><item><title><![CDATA[Schema First Typescript Types Design with Valibot]]></title><description><![CDATA[Learn more about Valibot and how we can use it to design Shema's that we can infer static Types and improve developer experience by reducing code duplication and getting runtime data validation.]]></description><link>https://www.allthingstypescript.dev/p/schema-first-typescript-types-design</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/schema-first-typescript-types-design</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 12 Feb 2024 06:19:15 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/7b449bb1-5363-415b-8f8b-01ccd839aa96_1280x853.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Let&#8217;s start with a simple hypothesis. Let&#8217;s say you have a form for your users, for instance, a sign-in form. You need a validation schema and a Typescript type, representing the shape of the form data. </p><p><em>Why a validation schema, you may ask?</em> This is because data coming from untrusted sources should always be validated, for instance, form data, ensuring that you are working with data that is accurate, complete, and clean. Another example of this is data from APIs among other sources.</p><p>Back to my hypothesis, this means you will need to write about two different pieces of code that basically say the same thing - describe the shape of the data for the form. And as you know about Typescript types, they don&#8217;t exist at runtime, so you will definitely need a validation schema for runtime purposes.</p><p>What if, we could write one and automatically have the other inferred? And this is where tools like <a href="https://zod.dev/">Zod</a> and <a href="https://valibot.dev/">Valibot</a>, among others, come in. With <a href="https://valibot.dev/">Valibot</a>, we can define a Valibot schema for our data and then use Valibot to infer a static type for Typescript.</p><p>Let's look at a simple example. For instance, let&#8217;s say, we are dealing with a simple sign-in form, with 2 fields - <em>an email and a password</em>. </p><p>This is what a Valibot schema looks like:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VKy9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3667e307-950b-46a8-921d-4d02d7831be7_1642x652.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VKy9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3667e307-950b-46a8-921d-4d02d7831be7_1642x652.png 424w, https://substackcdn.com/image/fetch/$s_!VKy9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3667e307-950b-46a8-921d-4d02d7831be7_1642x652.png 848w, https://substackcdn.com/image/fetch/$s_!VKy9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3667e307-950b-46a8-921d-4d02d7831be7_1642x652.png 1272w, https://substackcdn.com/image/fetch/$s_!VKy9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3667e307-950b-46a8-921d-4d02d7831be7_1642x652.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VKy9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3667e307-950b-46a8-921d-4d02d7831be7_1642x652.png" width="1456" height="578" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3667e307-950b-46a8-921d-4d02d7831be7_1642x652.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:578,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:141948,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VKy9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3667e307-950b-46a8-921d-4d02d7831be7_1642x652.png 424w, https://substackcdn.com/image/fetch/$s_!VKy9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3667e307-950b-46a8-921d-4d02d7831be7_1642x652.png 848w, https://substackcdn.com/image/fetch/$s_!VKy9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3667e307-950b-46a8-921d-4d02d7831be7_1642x652.png 1272w, https://substackcdn.com/image/fetch/$s_!VKy9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3667e307-950b-46a8-921d-4d02d7831be7_1642x652.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>With the above schema, we can infer static types for Typescript, which we can use within our application in places like function params Types, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VHmu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3208658-c8db-4cd6-8df6-64943ea3e84e_1758x352.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VHmu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3208658-c8db-4cd6-8df6-64943ea3e84e_1758x352.png 424w, https://substackcdn.com/image/fetch/$s_!VHmu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3208658-c8db-4cd6-8df6-64943ea3e84e_1758x352.png 848w, https://substackcdn.com/image/fetch/$s_!VHmu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3208658-c8db-4cd6-8df6-64943ea3e84e_1758x352.png 1272w, https://substackcdn.com/image/fetch/$s_!VHmu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3208658-c8db-4cd6-8df6-64943ea3e84e_1758x352.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VHmu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3208658-c8db-4cd6-8df6-64943ea3e84e_1758x352.png" width="1456" height="292" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b3208658-c8db-4cd6-8df6-64943ea3e84e_1758x352.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:292,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:83971,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VHmu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3208658-c8db-4cd6-8df6-64943ea3e84e_1758x352.png 424w, https://substackcdn.com/image/fetch/$s_!VHmu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3208658-c8db-4cd6-8df6-64943ea3e84e_1758x352.png 848w, https://substackcdn.com/image/fetch/$s_!VHmu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3208658-c8db-4cd6-8df6-64943ea3e84e_1758x352.png 1272w, https://substackcdn.com/image/fetch/$s_!VHmu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3208658-c8db-4cd6-8df6-64943ea3e84e_1758x352.png 1456w" sizes="100vw"></picture><div></div></div></a></figure></div><p>And here are the results, a Typescript static type:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!RvRG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1696f0-2a34-4e2a-825d-3adcceacaf2f_933x271.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!RvRG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1696f0-2a34-4e2a-825d-3adcceacaf2f_933x271.png 424w, https://substackcdn.com/image/fetch/$s_!RvRG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1696f0-2a34-4e2a-825d-3adcceacaf2f_933x271.png 848w, https://substackcdn.com/image/fetch/$s_!RvRG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1696f0-2a34-4e2a-825d-3adcceacaf2f_933x271.png 1272w, https://substackcdn.com/image/fetch/$s_!RvRG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1696f0-2a34-4e2a-825d-3adcceacaf2f_933x271.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!RvRG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1696f0-2a34-4e2a-825d-3adcceacaf2f_933x271.png" width="933" height="271" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1a1696f0-2a34-4e2a-825d-3adcceacaf2f_933x271.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:271,&quot;width&quot;:933,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:74554,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!RvRG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1696f0-2a34-4e2a-825d-3adcceacaf2f_933x271.png 424w, https://substackcdn.com/image/fetch/$s_!RvRG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1696f0-2a34-4e2a-825d-3adcceacaf2f_933x271.png 848w, https://substackcdn.com/image/fetch/$s_!RvRG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1696f0-2a34-4e2a-825d-3adcceacaf2f_933x271.png 1272w, https://substackcdn.com/image/fetch/$s_!RvRG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1a1696f0-2a34-4e2a-825d-3adcceacaf2f_933x271.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>As you can see from the above example, we just wrote a simple Valibot schema for our sign-in form and we got back static types for Typescript. Isn&#8217;t that cool?</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><h3>So, what&#8217;s Valibot</h3><p>Now that very long intro is out of the way, let's look at what Valibot is.</p><p>According to Valibot (And yes, the docs are written in the first person):</p><blockquote><p>Hello, I am Valibot and I would like to help you validate data easily using a schema. No matter if it is incoming data on a server, a form or even configuration files. I have no dependencies and can run in any JavaScript environment. - <a href="https://valibot.dev/guides/introduction/">Valibot</a>.</p></blockquote><p>In simple terms, it&#8217;s a runtime schema validation tool, which you can use to validate data coming from unknown/untrusted sources, i.e., forms, files, and APIs. Once data is parsed by Valibot, Typescript can infer the static type automatically. </p><p>For instance, continuing by our previous example above, we can parse data, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eYgW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2af4f7ee-685c-4991-8c1f-799ae4139d4c_1406x532.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eYgW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2af4f7ee-685c-4991-8c1f-799ae4139d4c_1406x532.png 424w, https://substackcdn.com/image/fetch/$s_!eYgW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2af4f7ee-685c-4991-8c1f-799ae4139d4c_1406x532.png 848w, https://substackcdn.com/image/fetch/$s_!eYgW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2af4f7ee-685c-4991-8c1f-799ae4139d4c_1406x532.png 1272w, https://substackcdn.com/image/fetch/$s_!eYgW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2af4f7ee-685c-4991-8c1f-799ae4139d4c_1406x532.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eYgW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2af4f7ee-685c-4991-8c1f-799ae4139d4c_1406x532.png" width="1406" height="532" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2af4f7ee-685c-4991-8c1f-799ae4139d4c_1406x532.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:532,&quot;width&quot;:1406,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:107402,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eYgW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2af4f7ee-685c-4991-8c1f-799ae4139d4c_1406x532.png 424w, https://substackcdn.com/image/fetch/$s_!eYgW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2af4f7ee-685c-4991-8c1f-799ae4139d4c_1406x532.png 848w, https://substackcdn.com/image/fetch/$s_!eYgW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2af4f7ee-685c-4991-8c1f-799ae4139d4c_1406x532.png 1272w, https://substackcdn.com/image/fetch/$s_!eYgW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2af4f7ee-685c-4991-8c1f-799ae4139d4c_1406x532.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><blockquote><p><em>Please note, in the case of the above example, since data is an empty object, it would fail validation and throw an error, so we would need to catch the error and process it.</em></p></blockquote><p>And just like magic (not magic, but the powers of Valibot), our <code>result</code> variable now has a correctly inferred type, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MoQS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99392f0e-70d2-40ec-b71f-375e2ad7f9ec_878x266.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MoQS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99392f0e-70d2-40ec-b71f-375e2ad7f9ec_878x266.png 424w, https://substackcdn.com/image/fetch/$s_!MoQS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99392f0e-70d2-40ec-b71f-375e2ad7f9ec_878x266.png 848w, https://substackcdn.com/image/fetch/$s_!MoQS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99392f0e-70d2-40ec-b71f-375e2ad7f9ec_878x266.png 1272w, https://substackcdn.com/image/fetch/$s_!MoQS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99392f0e-70d2-40ec-b71f-375e2ad7f9ec_878x266.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MoQS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99392f0e-70d2-40ec-b71f-375e2ad7f9ec_878x266.png" width="878" height="266" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/99392f0e-70d2-40ec-b71f-375e2ad7f9ec_878x266.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:266,&quot;width&quot;:878,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:55944,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MoQS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99392f0e-70d2-40ec-b71f-375e2ad7f9ec_878x266.png 424w, https://substackcdn.com/image/fetch/$s_!MoQS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99392f0e-70d2-40ec-b71f-375e2ad7f9ec_878x266.png 848w, https://substackcdn.com/image/fetch/$s_!MoQS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99392f0e-70d2-40ec-b71f-375e2ad7f9ec_878x266.png 1272w, https://substackcdn.com/image/fetch/$s_!MoQS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F99392f0e-70d2-40ec-b71f-375e2ad7f9ec_878x266.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>And the best part is, the validation will work during runtime, so, no matter the data source, we can consume it within our application, knowing that it&#8217;s what we want it to be.</p><h4>Benefits of Using Valibot</h4><p>The first and most obvious for those who have read this issue so far (<em>Thank you by the way&nbsp;&#10084;&#65039;</em>), is static type inference from our Valibot schemas. We don&#8217;t need to write duplicate pieces of code to describe the same data - a Typescript type and a validation schema.</p><p>Once we write a Valibot schema, we can get Typescript static types that we can use within our application, and upon parsing the data, Typescript can infer the correct Type.</p><p>This is huge for developer experience, as it reduces the amount of duplication within our application and now we only have to update our schema and our Types will be up to date.</p><p>This is by far for me, the most attractive proposition for Valibot and other tools like it, that do the same.</p><p>On top of the above, you can validate everything from complex objects to simple strings and numbers, etc, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sImj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0aaa621c-4866-4205-b8a5-be77134a4b40_1984x1312.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sImj!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0aaa621c-4866-4205-b8a5-be77134a4b40_1984x1312.png 424w, https://substackcdn.com/image/fetch/$s_!sImj!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0aaa621c-4866-4205-b8a5-be77134a4b40_1984x1312.png 848w, https://substackcdn.com/image/fetch/$s_!sImj!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0aaa621c-4866-4205-b8a5-be77134a4b40_1984x1312.png 1272w, https://substackcdn.com/image/fetch/$s_!sImj!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0aaa621c-4866-4205-b8a5-be77134a4b40_1984x1312.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sImj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0aaa621c-4866-4205-b8a5-be77134a4b40_1984x1312.png" width="1456" height="963" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0aaa621c-4866-4205-b8a5-be77134a4b40_1984x1312.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:963,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:264723,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!sImj!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0aaa621c-4866-4205-b8a5-be77134a4b40_1984x1312.png 424w, https://substackcdn.com/image/fetch/$s_!sImj!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0aaa621c-4866-4205-b8a5-be77134a4b40_1984x1312.png 848w, https://substackcdn.com/image/fetch/$s_!sImj!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0aaa621c-4866-4205-b8a5-be77134a4b40_1984x1312.png 1272w, https://substackcdn.com/image/fetch/$s_!sImj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0aaa621c-4866-4205-b8a5-be77134a4b40_1984x1312.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Impressed yet? For more information, check out the official docs <a href="https://valibot.dev/guides/schemas/">here</a>.</p><p>Lastly, it&#8217;s very lightweight, measuring about <strong>1.4KBs</strong> to validate a simple login form, which means it&#8217;s not expensive to download and consume in browser environments. This is made possible by Valibot modular design, which enables tree-shaking and removal of any unused code during the bundling process.</p><h3>Examples of Using Valibot</h3><p>Now, that we have looked at what Valibot is and why you would want to use it, let&#8217;s take a look at a few examples:</p><h4>Parsing Data</h4><p>Valibot provides several ways to parse data, the default way throws an error if validation fails, which you need to catch and do something about it.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!D_Ja!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2053f781-8589-4a07-9674-956fbfece8da_1664x712.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!D_Ja!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2053f781-8589-4a07-9674-956fbfece8da_1664x712.png 424w, https://substackcdn.com/image/fetch/$s_!D_Ja!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2053f781-8589-4a07-9674-956fbfece8da_1664x712.png 848w, https://substackcdn.com/image/fetch/$s_!D_Ja!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2053f781-8589-4a07-9674-956fbfece8da_1664x712.png 1272w, https://substackcdn.com/image/fetch/$s_!D_Ja!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2053f781-8589-4a07-9674-956fbfece8da_1664x712.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!D_Ja!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2053f781-8589-4a07-9674-956fbfece8da_1664x712.png" width="1456" height="623" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2053f781-8589-4a07-9674-956fbfece8da_1664x712.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:623,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:138308,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!D_Ja!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2053f781-8589-4a07-9674-956fbfece8da_1664x712.png 424w, https://substackcdn.com/image/fetch/$s_!D_Ja!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2053f781-8589-4a07-9674-956fbfece8da_1664x712.png 848w, https://substackcdn.com/image/fetch/$s_!D_Ja!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2053f781-8589-4a07-9674-956fbfece8da_1664x712.png 1272w, https://substackcdn.com/image/fetch/$s_!D_Ja!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2053f781-8589-4a07-9674-956fbfece8da_1664x712.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>On top of the above option, Valibot provides a <code>safeParse</code> method, that instead of throwing an error, will return an object containing output (<em>the validated data</em>) and <a href="https://valibot.dev/guides/issues/">issues</a>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wt5I!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F144d2cc0-3deb-41c2-b89c-9afa8d7b36a2_1828x532.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wt5I!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F144d2cc0-3deb-41c2-b89c-9afa8d7b36a2_1828x532.png 424w, https://substackcdn.com/image/fetch/$s_!wt5I!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F144d2cc0-3deb-41c2-b89c-9afa8d7b36a2_1828x532.png 848w, https://substackcdn.com/image/fetch/$s_!wt5I!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F144d2cc0-3deb-41c2-b89c-9afa8d7b36a2_1828x532.png 1272w, https://substackcdn.com/image/fetch/$s_!wt5I!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F144d2cc0-3deb-41c2-b89c-9afa8d7b36a2_1828x532.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wt5I!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F144d2cc0-3deb-41c2-b89c-9afa8d7b36a2_1828x532.png" width="1456" height="424" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/144d2cc0-3deb-41c2-b89c-9afa8d7b36a2_1828x532.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:424,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:117342,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wt5I!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F144d2cc0-3deb-41c2-b89c-9afa8d7b36a2_1828x532.png 424w, https://substackcdn.com/image/fetch/$s_!wt5I!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F144d2cc0-3deb-41c2-b89c-9afa8d7b36a2_1828x532.png 848w, https://substackcdn.com/image/fetch/$s_!wt5I!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F144d2cc0-3deb-41c2-b89c-9afa8d7b36a2_1828x532.png 1272w, https://substackcdn.com/image/fetch/$s_!wt5I!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F144d2cc0-3deb-41c2-b89c-9afa8d7b36a2_1828x532.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You can learn more about Valibot issues <a href="https://valibot.dev/guides/issues/">here</a>.</p><p>Finally, Valibot also provides a Type Guard option, allowing you to check if data parses a validation check, which returns true or false and can be used for <a href="https://www.allthingstypescript.dev/p/narrowing-types-in-typescript">Type Narrowing</a> as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!pwHi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2128ba66-5836-49f2-9bfc-986ff9ecdf92_1172x712.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!pwHi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2128ba66-5836-49f2-9bfc-986ff9ecdf92_1172x712.png 424w, https://substackcdn.com/image/fetch/$s_!pwHi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2128ba66-5836-49f2-9bfc-986ff9ecdf92_1172x712.png 848w, https://substackcdn.com/image/fetch/$s_!pwHi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2128ba66-5836-49f2-9bfc-986ff9ecdf92_1172x712.png 1272w, https://substackcdn.com/image/fetch/$s_!pwHi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2128ba66-5836-49f2-9bfc-986ff9ecdf92_1172x712.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!pwHi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2128ba66-5836-49f2-9bfc-986ff9ecdf92_1172x712.png" width="1172" height="712" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2128ba66-5836-49f2-9bfc-986ff9ecdf92_1172x712.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:712,&quot;width&quot;:1172,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:132418,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!pwHi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2128ba66-5836-49f2-9bfc-986ff9ecdf92_1172x712.png 424w, https://substackcdn.com/image/fetch/$s_!pwHi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2128ba66-5836-49f2-9bfc-986ff9ecdf92_1172x712.png 848w, https://substackcdn.com/image/fetch/$s_!pwHi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2128ba66-5836-49f2-9bfc-986ff9ecdf92_1172x712.png 1272w, https://substackcdn.com/image/fetch/$s_!pwHi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2128ba66-5836-49f2-9bfc-986ff9ecdf92_1172x712.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Primitives</h4><p>Here are the Javascript Primitives: <code>string</code>, <code>number</code>, <code>bigint</code>, <code>boolean</code>, <code>undefined</code>, <code>symbol</code> and <code>null</code>.</p><p>Valibot supports all the above primitives:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Iyc8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0937bdfe-512f-41dc-8039-73aec995227c_1524x832.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Iyc8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0937bdfe-512f-41dc-8039-73aec995227c_1524x832.png 424w, https://substackcdn.com/image/fetch/$s_!Iyc8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0937bdfe-512f-41dc-8039-73aec995227c_1524x832.png 848w, https://substackcdn.com/image/fetch/$s_!Iyc8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0937bdfe-512f-41dc-8039-73aec995227c_1524x832.png 1272w, https://substackcdn.com/image/fetch/$s_!Iyc8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0937bdfe-512f-41dc-8039-73aec995227c_1524x832.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Iyc8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0937bdfe-512f-41dc-8039-73aec995227c_1524x832.png" width="1456" height="795" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0937bdfe-512f-41dc-8039-73aec995227c_1524x832.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:795,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:216377,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Iyc8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0937bdfe-512f-41dc-8039-73aec995227c_1524x832.png 424w, https://substackcdn.com/image/fetch/$s_!Iyc8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0937bdfe-512f-41dc-8039-73aec995227c_1524x832.png 848w, https://substackcdn.com/image/fetch/$s_!Iyc8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0937bdfe-512f-41dc-8039-73aec995227c_1524x832.png 1272w, https://substackcdn.com/image/fetch/$s_!Iyc8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0937bdfe-512f-41dc-8039-73aec995227c_1524x832.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Optionals: <code>Undefined</code> and <code>Null</code></h4><p>Sometimes, <code>undefined</code>, <code>null</code> or both are valid values, when a value isn&#8217;t required. So, Valibot provides you with functions to make this possible.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rTfc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be9d025-0ba2-4d32-b282-760877a382de_1608x952.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rTfc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be9d025-0ba2-4d32-b282-760877a382de_1608x952.png 424w, https://substackcdn.com/image/fetch/$s_!rTfc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be9d025-0ba2-4d32-b282-760877a382de_1608x952.png 848w, https://substackcdn.com/image/fetch/$s_!rTfc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be9d025-0ba2-4d32-b282-760877a382de_1608x952.png 1272w, https://substackcdn.com/image/fetch/$s_!rTfc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be9d025-0ba2-4d32-b282-760877a382de_1608x952.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rTfc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be9d025-0ba2-4d32-b282-760877a382de_1608x952.png" width="1456" height="862" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7be9d025-0ba2-4d32-b282-760877a382de_1608x952.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:862,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:194163,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rTfc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be9d025-0ba2-4d32-b282-760877a382de_1608x952.png 424w, https://substackcdn.com/image/fetch/$s_!rTfc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be9d025-0ba2-4d32-b282-760877a382de_1608x952.png 848w, https://substackcdn.com/image/fetch/$s_!rTfc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be9d025-0ba2-4d32-b282-760877a382de_1608x952.png 1272w, https://substackcdn.com/image/fetch/$s_!rTfc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7be9d025-0ba2-4d32-b282-760877a382de_1608x952.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Error Messages </h4><p>And finally, you can customize error messages, so that when validation fails, you can provide nice messages for your users, so that it&#8217;s easy to know and understand what went wrong.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!W1Ia!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F627dd592-450e-4397-92e1-495c307a37c7_1922x712.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!W1Ia!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F627dd592-450e-4397-92e1-495c307a37c7_1922x712.png 424w, https://substackcdn.com/image/fetch/$s_!W1Ia!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F627dd592-450e-4397-92e1-495c307a37c7_1922x712.png 848w, https://substackcdn.com/image/fetch/$s_!W1Ia!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F627dd592-450e-4397-92e1-495c307a37c7_1922x712.png 1272w, https://substackcdn.com/image/fetch/$s_!W1Ia!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F627dd592-450e-4397-92e1-495c307a37c7_1922x712.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!W1Ia!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F627dd592-450e-4397-92e1-495c307a37c7_1922x712.png" width="1456" height="539" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/627dd592-450e-4397-92e1-495c307a37c7_1922x712.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:539,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:167569,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!W1Ia!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F627dd592-450e-4397-92e1-495c307a37c7_1922x712.png 424w, https://substackcdn.com/image/fetch/$s_!W1Ia!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F627dd592-450e-4397-92e1-495c307a37c7_1922x712.png 848w, https://substackcdn.com/image/fetch/$s_!W1Ia!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F627dd592-450e-4397-92e1-495c307a37c7_1922x712.png 1272w, https://substackcdn.com/image/fetch/$s_!W1Ia!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F627dd592-450e-4397-92e1-495c307a37c7_1922x712.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Conclusion</h3><p>In this All Things Typescript issue, we looked at Valibot -  a runtime schema validation library that can be used to validate data at runtime and ensure the integrity of data flowing within our application.</p><p>On top of that, we looked at how we can utilize it in Typescript so that we can avoid code duplication, and get static types from the Valibot schemas we create. This has the overall effect of improving the developer experience we enjoy without sacrificing much, apart from maybe learning some newer syntax -  the Valibot Way of doing things.</p><p>If you would like to learn more about Valibot, check out the official documentation <a href="https://valibot.dev/guides/introduction/">here</a>.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/p/schema-first-typescript-types-design?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/p/schema-first-typescript-types-design?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div><hr></div><p>I hope you learned something from today&#8217;s issues and I am open to suggestions for the next topics to cover for All Things Typescript. If you have any suggestions, feel free to drop them in the comment section below.</p><p>Otherwise, until next time, keep on learning.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share All Things Typescript&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share All Things Typescript</span></a></p>]]></content:encoded></item><item><title><![CDATA[Jan 2024 Top Articles and Resources]]></title><description><![CDATA[Articles and Learning Resources that I found useful in January 2024.]]></description><link>https://www.allthingstypescript.dev/p/jan-2024-top-articles-and-resources</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/jan-2024-top-articles-and-resources</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 05 Feb 2024 08:25:18 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/94cff8ed-4394-45e7-88ef-ea5c94a47d4a_1280x853.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello everyone,</p><p>I am very excited to let you know that starting in January 2024, I will be sending out a monthly curated list of top articles and resources related to Typescript and other relevant technologies. This list will be sent out on the first Monday following the end of each month.</p><p>Although I share these articles on my social media, I wanted to create a central place where all of my subscribers could find all the useful resources related to Typescript I come across regularly. This is a return to the roots of the contents of this Newsletter, which was mainly focused on curating Typescript resources. </p><p>For those who are still interested in my regular content, don't worry, as it will continue to be sent out every week.</p><p>I appreciate your support.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading All Things Typescript! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h3>Announcement</h3><h4><a href="https://devblogs.microsoft.com/typescript/announcing-typescript-5-4-beta/">TypeScript 5.4 Beta Announced</a></h4><p>In late January, the beta version of Typescript 5.4 was released. Some of its features include:</p><ul><li><p>Assignment Narrowing is Preserved in Closures</p></li><li><p>New <strong>NoInfer</strong> Utility Type</p></li><li><p>Type Declarations for <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy">Object.groupBy</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/groupBy">Map.groupBy</a></p></li><li><p>Support for require() calls in <code>--moduleResolution</code> bundler and <code>--module preserve</code></p></li><li><p>Checked Import Attributes and Assertions</p></li><li><p>Quick Fix for Adding Missing Parameters</p></li></ul><p>For more information, check out the official announcement <a href="https://devblogs.microsoft.com/typescript/announcing-typescript-5-4-beta/">here</a>.</p><div><hr></div><h3>My Articles</h3><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;fbabaded-e153-4e0c-ac1e-d1005e614885&quot;,&quot;caption&quot;:&quot;Bun is a JS/TS runtime environment, a package manager, a test runner, and bundler for your TS/JS Project. Should you consider it?&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;lg&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Bun - The One Tool for All Your JavaScript/Typescript Project's Needs?&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Software Engineer, Google Developer Expert and Typescript Nerd.&quot;,&quot;photo_url&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/8765c349-75ba-43ee-9976-e3ae5631b064_904x1124.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2024-01-29T07:05:28.656Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a3d8e88d-87c6-4c6f-bda2-7ed3300f1a7d_1280x675.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/bun-one-tool-for-all-your-javascripttypescript&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:139489394,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:2,&quot;comment_count&quot;:2,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div><hr></div><h3>Articles</h3><h4><a href="https://thenewstack.io/daniel-rosenwasser-on-typescript-and-whats-ahead-in-2024/">Daniel Rosenwasser on TypeScript and What&#8217;s Ahead in 2024 by Loraine Lawson</a></h4><p>In this Q&amp;A with The New Stack, Rosenwasser shared TypeScript&#8217;s most significant developments in 2023 and provided a preview of what developers can expect to see in the coming year.</p><p><a href="https://thenewstack.io/daniel-rosenwasser-on-typescript-and-whats-ahead-in-2024/">Link</a></p><h4><a href="https://www.totaltypescript.com/tutorials/solving-typescript-errors">Solving TypeScript Errors by Matt Pocock</a></h4><p>In this tutorial, you'll find challenges centered around reading error messages and practical solutions to solving the issues they describe.</p><p><a href="https://www.totaltypescript.com/tutorials/solving-typescript-errors">Link</a></p><h4><strong><a href="https://bun.sh/blog/the-bun-shell">The Bun Shell by Jarred Sumner</a></strong></h4><p>The Bun Shell is a new experimental embedded language and interpreter in Bun that allows you to run cross-platform shell scripts in JavaScript &amp; TypeScript.</p><p><a href="https://bun.sh/blog/the-bun-shell">Link</a>.</p><h4><strong><a href="https://timdeschryver.dev/blog/using-zods-schema-to-render-a-reusable-and-dynamic-angular-table-component">Using Zod's schema to render a reusable and dynamic Angular table component by Tim Deschryver</a></strong></h4><p>In this blog post, we'll see how to use Zod to dynamically build a (minimal) table as a reusable and typesafe Angular component. For this, the following <code>Person</code> schema will be used within the next snippets.</p><p><a href="https://timdeschryver.dev/blog/using-zods-schema-to-render-a-reusable-and-dynamic-angular-table-component">Link</a></p><h4><a href="https://blog.jetbrains.com/webstorm/2024/01/everything-you-ve-always-wanted-to-know-about-typescript-s-integration-in-webstorm/?ref=dailydev">Everything You&#8217;ve Always Wanted to Know About TypeScript&#8217;s Integration in WebStorm by Jan-Niklas Wortmann</a></h4><p>Ever tense up when a new TypeScript version drops? You cross your fingers and hope it plays nice with your IDE. You&#8217;re not alone. Relax into this fireside chat with WebStorm lead developer and Team Lead, Andrey Starovoyt, as we pull back the curtain on TypeScript&#8217;s integration in WebStorm.</p><p><a href="https://blog.jetbrains.com/webstorm/2024/01/everything-you-ve-always-wanted-to-know-about-typescript-s-integration-in-webstorm/?ref=dailydev">Link</a></p><h4><a href="https://advancedweb.hu/modern-javascript-library-starter/">Modern JavaScript library starter by Tam&#225;s Sallai</a></h4><p>How to publish a package with TypeScript, testing, GitHub Actions, and auto-publish to NPM</p><p><a href="https://advancedweb.hu/modern-javascript-library-starter/?ref=dailydev">Link</a></p><h4><a href="https://medium.com/javascript-scene/tdd-writing-testable-code-30ac7a3bf49c">TDD: Writing Testable Code by JavaScript Scene by Eric Elliott</a></h4><p>Writing testable code is a vital skill in software engineering. Let&#8217;s explore practical advice, strategies, and tactics for writing more testable code, unlocking the benefits of modularity, reusability, and high quality software in your projects.</p><p><a href="https://medium.com/javascript-scene/tdd-writing-testable-code-30ac7a3bf49c">Link</a></p><h4><a href="https://dev.to/nazeelashraf/writing-type-safe-api-clients-in-typescript-1j92">Writing type safe API clients in TypeScript by Nazeel</a></h4><p>Writing API integrations can be a daunting task. But it does not need to be. How many times have you had to deal with these problems?</p><p><a href="https://dev.to/nazeelashraf/writing-type-safe-api-clients-in-typescript-1j92">Link</a></p><h4><a href="https://blog.logrocket.com/typescript-domain-driven-design/">Leveraging TypeScript for domain-driven design by Matteo Di Pirro</a></h4><p>Domain-driven design (DDD) is a software development approach that aims to simplify the creation of applications that involve complex business logic. In this article, we&#8217;ll explore how to leverage TypeScript for DDD. TypeScript&#8217;s sophisticated type system enables fine-grained domain modeling and is highly adaptable, lending itself to complex app development.</p><p><a href="https://blog.logrocket.com/typescript-domain-driven-design/">Link</a></p><h4><a href="https://spin.atomicobject.com/discriminated-unions-typescript/">The Discriminated Union: Writing Easy-to-Use Types in TypeScript by Peter Van Drunen</a></h4><p>TypeScript&#8217;s flexibility is one of the reasons I love using it. Unfortunately, that flexibility also provides opportunities to write confusing code. The main area where I see developers struggling is defining types that make writing code painful or verbose. Often, this comes in the form of using <a href="https://spin.atomicobject.com/optional-undefined-typescript/">optional</a> fields to extend types so they represent too many ideas within the same type.</p><p><a href="https://spin.atomicobject.com/discriminated-unions-typescript/?ref=dailydev">Link</a></p><div><hr></div><p>That's all for now. I'll see you again at the end of February for another monthly recap.</p>]]></content:encoded></item><item><title><![CDATA[Bun - The One Tool for All Your JavaScript/Typescript Project's Needs?]]></title><description><![CDATA[Bun is a JS/TS runtime environment, a package manager, a test runner and bundler for your TS/JS Project. Should you consider it?]]></description><link>https://www.allthingstypescript.dev/p/bun-one-tool-for-all-your-javascripttypescript</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/bun-one-tool-for-all-your-javascripttypescript</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 29 Jan 2024 07:05:28 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/59df4cca-7a3d-49ab-8a87-5a1da1efa693_3840x2160.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In most issues for this newsletter, I have focussed on Typescript and its type system, which I will continue doing. This has been great for my readers (from the feedback I have received) but I have wanted to cover broader Typescript-related topics, such as today&#8217;s topic, after all this is All Things Typescript. I hope you will enjoy this topic and let me know if you want more like this.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><h3>What is Bun?</h3><p>NodeJS is the dominant Javascript server runtime environment for Javascript and Typescript (sort of) projects. But over the years, we have seen several attempts to build alternative runtime environments such as <a href="https://deno.com/">Deno</a> and Bun, today&#8217;s subject, among others. </p><p>These newer alternatives focus on providing much better runtime performance, much better support for Web APIs on the server, and generally better security among other features, we will look at in later sections. The NodeJS team also continues to make improvements to NodeJS and we have seen tremendous improvements in here as well.</p><div class="pullquote"><p><strong>&#8220;Develop, test, run, and bundle JavaScript &amp; TypeScript projects&#8212;all with Bun. Bun is an all-in-one JavaScript runtime &amp; toolkit designed for speed, complete with a bundler, <a href="https://bun.sh/docs/cli/test">test runner</a>, and Node.js-compatible <a href="https://bun.sh/package-manager">package manager</a>.&#8221;</strong></p></div><p>Compared to Deno, what I think makes Bun special, is that it&#8217;s a drop-in replacement for node (you don&#8217;t have to change much if anything at all). Unlike Deno which required you to make significant changes to your application at launch to adopt it, Bun doesn&#8217;t require you to make any changes to your codebase. </p><p>With Bun, you can go into any node-js-based project, like let&#8217;s a Next JS or React Application, and just run <code>bun install</code> (y<em>es, but install you read that correctly not npm install</em>) and then run the <code>bun run dev</code> (<em>or whatever <a href="https://docs.npmjs.com/cli/v9/using-npm/scripts">npm script</a> you use to launch your project</em>) and it should run your application, just like Node JS. </p><p>From my experience, this ran flawlessly all the time I tried it.</p><h3>So, why use Bun?</h3><p>So, now that I have briefly introduced you to Bun and why it&#8217;s a very interesting project, let's see some of the advantages of using Bun, over Node (where most of us are probably coming from), and the others.</p><h4>Speed, it&#8217;s Fast, Really Fast</h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!tKu4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13d50cd4-e8cc-4cbe-9cc2-3fb651bf4cbd_631x396.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!tKu4!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13d50cd4-e8cc-4cbe-9cc2-3fb651bf4cbd_631x396.jpeg 424w, https://substackcdn.com/image/fetch/$s_!tKu4!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13d50cd4-e8cc-4cbe-9cc2-3fb651bf4cbd_631x396.jpeg 848w, https://substackcdn.com/image/fetch/$s_!tKu4!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13d50cd4-e8cc-4cbe-9cc2-3fb651bf4cbd_631x396.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!tKu4!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13d50cd4-e8cc-4cbe-9cc2-3fb651bf4cbd_631x396.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!tKu4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13d50cd4-e8cc-4cbe-9cc2-3fb651bf4cbd_631x396.jpeg" width="631" height="396" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/13d50cd4-e8cc-4cbe-9cc2-3fb651bf4cbd_631x396.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:396,&quot;width&quot;:631,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!tKu4!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13d50cd4-e8cc-4cbe-9cc2-3fb651bf4cbd_631x396.jpeg 424w, https://substackcdn.com/image/fetch/$s_!tKu4!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13d50cd4-e8cc-4cbe-9cc2-3fb651bf4cbd_631x396.jpeg 848w, https://substackcdn.com/image/fetch/$s_!tKu4!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13d50cd4-e8cc-4cbe-9cc2-3fb651bf4cbd_631x396.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!tKu4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F13d50cd4-e8cc-4cbe-9cc2-3fb651bf4cbd_631x396.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>NodeJS is by no means a slow runtime, it wouldn&#8217;t be so popular if it was. But compared to Bun, it&#8217;s slow. Bun was built from the ground up with speed in mind, using both <a href="https://developer.apple.com/documentation/javascriptcore">JavascriptCore</a> and <a href="https://ziglang.org/">Zig</a>. The Bun team spent an enormous amount of time and energy trying to make Bun fast, including lots of profiling, benchmarking, and optimizations. </p><p>Even the choice of the programming language to use Zig was done with performance in mind. The team chose Zig programming language as it allows them to have low-level control of memory management and lacks hidden control flow, making it easy to write fast software.</p><p>The same can be said about the team&#8217;s decision to go with JavascriptCore. Both NodeJS and Deno are based on Chrome V8 Engine, while Bun is based on Apple's JavaScriptCore. While not mind-blowingly faster than the V8 Engine, it&#8217;s a little bit faster. And these optimizations have paid off for Bun, as it&#8217;s faster than both Deno and NodeJS.</p><p>To demonstrate this I put my novice benchmarking skills to the test. To run the benchmarks, I used the latest versions of NodeJS, Deno, and Bun, as of the time of writing this issue.</p><p>For benchmarking, I used the following simple JS code:</p><pre><code>for (let i = 0; i &lt; 10000; i++) {
  console.log(`Count: ${i + 1}`);
}</code></pre><p>And then I used <a href="https://github.com/sharkdp/hyperfine">hyperfine</a> to run the benchmarks on my MacBook Pro 14 M2 Max, and here are the results:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZdFL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3aae920c-34d1-40ee-9c42-a17157cce109_2048x544.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZdFL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3aae920c-34d1-40ee-9c42-a17157cce109_2048x544.png 424w, https://substackcdn.com/image/fetch/$s_!ZdFL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3aae920c-34d1-40ee-9c42-a17157cce109_2048x544.png 848w, https://substackcdn.com/image/fetch/$s_!ZdFL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3aae920c-34d1-40ee-9c42-a17157cce109_2048x544.png 1272w, https://substackcdn.com/image/fetch/$s_!ZdFL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3aae920c-34d1-40ee-9c42-a17157cce109_2048x544.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZdFL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3aae920c-34d1-40ee-9c42-a17157cce109_2048x544.png" width="1456" height="387" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3aae920c-34d1-40ee-9c42-a17157cce109_2048x544.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:387,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:220940,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZdFL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3aae920c-34d1-40ee-9c42-a17157cce109_2048x544.png 424w, https://substackcdn.com/image/fetch/$s_!ZdFL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3aae920c-34d1-40ee-9c42-a17157cce109_2048x544.png 848w, https://substackcdn.com/image/fetch/$s_!ZdFL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3aae920c-34d1-40ee-9c42-a17157cce109_2048x544.png 1272w, https://substackcdn.com/image/fetch/$s_!ZdFL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3aae920c-34d1-40ee-9c42-a17157cce109_2048x544.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>As you can see, Bun is 2.20 faster than Deno and 2.88 faster than NodeJS. I also ran the same benchmarks on Windows 11 using WSL and Bun managed an even more impressive feat, being 3.06 times faster than node and 3.26 times faster than Deno, as shown below:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6zXv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa257aeb-7442-43a9-a346-c2f66c061aba_1414x597.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6zXv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa257aeb-7442-43a9-a346-c2f66c061aba_1414x597.png 424w, https://substackcdn.com/image/fetch/$s_!6zXv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa257aeb-7442-43a9-a346-c2f66c061aba_1414x597.png 848w, https://substackcdn.com/image/fetch/$s_!6zXv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa257aeb-7442-43a9-a346-c2f66c061aba_1414x597.png 1272w, https://substackcdn.com/image/fetch/$s_!6zXv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa257aeb-7442-43a9-a346-c2f66c061aba_1414x597.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6zXv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa257aeb-7442-43a9-a346-c2f66c061aba_1414x597.png" width="1414" height="597" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fa257aeb-7442-43a9-a346-c2f66c061aba_1414x597.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:597,&quot;width&quot;:1414,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:82197,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!6zXv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa257aeb-7442-43a9-a346-c2f66c061aba_1414x597.png 424w, https://substackcdn.com/image/fetch/$s_!6zXv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa257aeb-7442-43a9-a346-c2f66c061aba_1414x597.png 848w, https://substackcdn.com/image/fetch/$s_!6zXv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa257aeb-7442-43a9-a346-c2f66c061aba_1414x597.png 1272w, https://substackcdn.com/image/fetch/$s_!6zXv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa257aeb-7442-43a9-a346-c2f66c061aba_1414x597.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">This benchmark was run on an Intel Core i9 11900H process and 40 GB of RAM </figcaption></figure></div><blockquote><p>&#129300;<em> <strong>Interestingly</strong>, the Gap between NodeJS and Deno <strong>seems</strong> to be closing, let me know in the comment section below if you want me to do a deep in dive comparing the three on different workloads and different OSs.</em></p></blockquote><p>If you want to dig into the performance comparison and a very good explanation of the internal workings (in relationship to Javascript Engines) of NodeJS, check out this amazing video by <a href="https://www.youtube.com/watch?v=8wTulvlllGQ">Chris Hay</a>.</p><h4>Compatibility with NodeJS and NPM</h4><p>I touched on this earlier, if you are already using NodeJS and would love to switch to Bun, you can do so with minimal effort. You can visit any NodeJS-based project, run <code>bun install</code> and then run the server using Bun instead of NodeJS, no code changes are needed right out of the box.</p><div class="pullquote"><p>&#8220;Bun is designed as a drop-in replacement for Node.js. It natively implements hundreds of Node.js and Web APIs, including <code>fs</code>, <code>path</code>, <code>Buffer</code> and more.&#8221;</p></div><p>For more information on NodeJS APIs implemented by Bun <a href="https://bun.sh/docs/runtime/nodejs-apis">here</a>.</p><p>And if all you just need is a much faster package manager, you can use Bun to replace NPM and use NodeJS for your server, no fuzz.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3-yg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a40787a-aa87-40c8-921d-0d2fef789bb8_1122x445.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3-yg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a40787a-aa87-40c8-921d-0d2fef789bb8_1122x445.png 424w, https://substackcdn.com/image/fetch/$s_!3-yg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a40787a-aa87-40c8-921d-0d2fef789bb8_1122x445.png 848w, https://substackcdn.com/image/fetch/$s_!3-yg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a40787a-aa87-40c8-921d-0d2fef789bb8_1122x445.png 1272w, https://substackcdn.com/image/fetch/$s_!3-yg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a40787a-aa87-40c8-921d-0d2fef789bb8_1122x445.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3-yg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a40787a-aa87-40c8-921d-0d2fef789bb8_1122x445.png" width="1122" height="445" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2a40787a-aa87-40c8-921d-0d2fef789bb8_1122x445.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:445,&quot;width&quot;:1122,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:59082,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!3-yg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a40787a-aa87-40c8-921d-0d2fef789bb8_1122x445.png 424w, https://substackcdn.com/image/fetch/$s_!3-yg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a40787a-aa87-40c8-921d-0d2fef789bb8_1122x445.png 848w, https://substackcdn.com/image/fetch/$s_!3-yg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a40787a-aa87-40c8-921d-0d2fef789bb8_1122x445.png 1272w, https://substackcdn.com/image/fetch/$s_!3-yg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a40787a-aa87-40c8-921d-0d2fef789bb8_1122x445.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You can learn more about bun install <a href="https://bun.sh/docs/install/lockfile">here</a>.</p><h4>Built-in Tooling</h4><p>One of the most frustrating or overwhelming things especially for novices about modern web development is the amount of tools that you need to learn. For starters, you will need node/deno/bun and npm, and if you decide to use Typescript, you will need a transpiler and maybe (by maybe I mean, most certainly) a bundler and the story goes on.</p><p>We, as web development veterans, sometimes forget how intimidating this can be for beginners, I guess you can call this Stockholm syndrome.</p><p>With Bun, you only need Bun. It&#8217;s a drop-in replacement for all of the above tools above. Want to install packages, bun has got you, with Bun package manager, it&#8217;s a runtime and bundler and will transpile and run typescript code, without you having to install a host of tools to achieve the same.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rxok!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c569d67-cb93-4fad-87e5-c1ccac631b24_500x572.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rxok!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c569d67-cb93-4fad-87e5-c1ccac631b24_500x572.jpeg 424w, https://substackcdn.com/image/fetch/$s_!rxok!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c569d67-cb93-4fad-87e5-c1ccac631b24_500x572.jpeg 848w, https://substackcdn.com/image/fetch/$s_!rxok!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c569d67-cb93-4fad-87e5-c1ccac631b24_500x572.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!rxok!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c569d67-cb93-4fad-87e5-c1ccac631b24_500x572.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rxok!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c569d67-cb93-4fad-87e5-c1ccac631b24_500x572.jpeg" width="500" height="572" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4c569d67-cb93-4fad-87e5-c1ccac631b24_500x572.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:572,&quot;width&quot;:500,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!rxok!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c569d67-cb93-4fad-87e5-c1ccac631b24_500x572.jpeg 424w, https://substackcdn.com/image/fetch/$s_!rxok!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c569d67-cb93-4fad-87e5-c1ccac631b24_500x572.jpeg 848w, https://substackcdn.com/image/fetch/$s_!rxok!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c569d67-cb93-4fad-87e5-c1ccac631b24_500x572.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!rxok!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c569d67-cb93-4fad-87e5-c1ccac631b24_500x572.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>What about testing? Yeah, Bun still got you with an inbuilt test-runner, goodbye Jest?&#129335;&#127998;. Bun is fully compatible with jest syntax and you can use it as a drop-in replacement for jest, and you get all the benefits of Bun discussed in this article.</p><p>You can learn more about Bun testing <a href="https://bun.sh/docs/cli/test">here</a>.</p><h4>Web Standard API</h4><p>One thing that serves as a divide depending on the runtime you are targeting is how some APIs are available on the browser, yet are missing on the server (NodeJS). This means that even for tasks you can perform in both the server and the browser, it&#8217;s possible to have two different APIs for achieving the same on both, despite the fact you using the same programming language. </p><p>A good example of this, until recently, was fetch, a common task like fetching data over HTTP,  which you can perform on both the browser and the server. It was natively supported in the browser environment but not supported natively by NodeJS. This can be frustrating because despite the fact you are writing Javascript, the tooling changes based on your target environment.</p><p>Bun aims to address this by providing and implementing a list of standard web APIs that are similar to the ones available in the browser environment. Of course not all APIs available in the browser are required in the server environment, think of the DOM and History APIs as good examples of this. </p><p>But common tasks in both environments such as making HTTP calls don&#8217;t need to have unique Web APIs for each environment. Bun implements the Web Standard APIs for some of this, instead of re-inventing the wheel, which makes our Job (especially full-stack developers) a tad bit easier.</p><p>You can find a comprehensive list of Web Standard APIs implemented by Bun <a href="https://bun.sh/docs/runtime/web-apis">here</a>.</p><h4>Supports Typescript and JSX/TSX out of the Box</h4><p>With Bun, you don&#8217;t need to transpile your Typescript code to Javascript to execute it (or result in using something ts-node). You can execute your Typescript directly using Bun, and it&#8217;s going to do all the heavy lifting for you - the process of transpilation, and executing the resulting JS code, without you having to think about it. </p><p><strong>One very important caveat</strong>, that I missed before and I was corrected in the comment section below, is that Bun doesn&#8217;t perform type-checking, unlike other build tools, just does transpilation. You will still need the Typescript compiler to Typecheck your code.</p><div class="pullquote"><p>Bun treats TypeScript as a first-class citizen.</p></div><p>On top of that, it does support JSX (TSX by extension) out of the box, compiling it internally to JS code ready for execution. In both cases, Bun will respect your configurations (<code>tsconfigs</code>/<code>jsconfig</code>) during the transpilation/conversion process.</p><p>You can learn more about Typescript support <a href="https://bun.sh/docs/runtime/typescript">here</a> and JSX <a href="https://bun.sh/docs/runtime/jsx">here</a>.</p><h4>Single File Executables</h4><p>Ever wanted to run compile your javascript into binary executables that you can run directly? Bun got your back here as well, you can compile Typescript and Javascript projects, including all dependencies into a single executable binary that you can distribute and run, without needing to install dependencies or even Bun itself. </p><p>On top of that, you can embed files and even databases such as SQS Lite, if you want to. Please note, that if you embed SQL Lite, it runs in memory and the changes will be lost when the binary exists. This does give you several choices when it comes to distributing your projects, which doesn&#8217;t require the OS to have all the dependencies installed to run.</p><h3>Bun Resources</h3><p>Let&#8217;s say you are interested in learning more about Bun and probably give it a try. Bun has a website, where you can learn more about Bun and its features (including all the benchmark data captured in this issue), and here is the <a href="https://bun.sh/">link</a>.</p><ol><li><p><a href="https://bun.sh/docs">Bun Documentation</a> </p></li><li><p><a href="https://bun.sh/docs/installation">Installation Instructions</a></p></li><li><p><a href="https://blog.logrocket.com/getting-started-bun-react/">Getting started with Bun and React</a></p></li><li><p><a href="https://www.freecodecamp.org/news/learn-bun-a-faster-node-js-alternative/">Learn Bun, a faster Node.js alternative</a></p></li></ol><h3>Conclusion</h3><p>In this issue, I have done something different from my usual content. We have gone over Bun, a Javascript server runtime environment which is a drop-in replacement for NodeJS. We took a look at some of the features that set Bun apart from NodeJS and which might tempt you to switch to Bun.</p><p>That&#8217;s it from me and until next time, keep on learning.</p>]]></content:encoded></item><item><title><![CDATA[Why avoid the Any Type in Typescript]]></title><description><![CDATA[We do a deep dive on any in Typescript, why it exists, why you should avoid using it and remedies and alternatives to using any within your codebase]]></description><link>https://www.allthingstypescript.dev/p/why-avoid-the-any-type-in-typescript</link><guid isPermaLink="false">https://www.allthingstypescript.dev/p/why-avoid-the-any-type-in-typescript</guid><dc:creator><![CDATA[Maina Wycliffe]]></dc:creator><pubDate>Mon, 20 Nov 2023 06:45:25 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c2e6820-51d1-4873-8329-dbc6646cc5e4_500x624.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>So, in the last few issues, we have covered <a href="https://www.allthingstypescript.dev/p/avoid-using-type-assertions-in-typescript">avoiding type assertions</a> and why you should <a href="https://www.allthingstypescript.dev/p/always-prefer-type-with-a-narrower">prefer narrower types in Typescript</a>. In this issue, I wanted to take a closer look at the <code>any</code> type and why you should avoid it, alternatives to the <code>any</code> type and when it&#8217;s okay to use the <code>any</code> type (spoiler &#8252;&#65039; - not many situations). </p><h3>The <code>any</code> Type in Typescript</h3><p>One of the most controversial types in Typescript is any, this is because it could be anything, and can be assigned to all variable types and assigned to all variable types, and as such Typescript doesn&#8217;t do any type checking when the <code>any </code>type is involved. Yes, that&#8217;s how powerful the <code>any</code> type is in Typescript.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fe2g!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2127ce84-6239-4d31-8e7b-ee9858ce48a9_360x146.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fe2g!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2127ce84-6239-4d31-8e7b-ee9858ce48a9_360x146.gif 424w, https://substackcdn.com/image/fetch/$s_!fe2g!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2127ce84-6239-4d31-8e7b-ee9858ce48a9_360x146.gif 848w, https://substackcdn.com/image/fetch/$s_!fe2g!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2127ce84-6239-4d31-8e7b-ee9858ce48a9_360x146.gif 1272w, https://substackcdn.com/image/fetch/$s_!fe2g!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2127ce84-6239-4d31-8e7b-ee9858ce48a9_360x146.gif 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fe2g!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2127ce84-6239-4d31-8e7b-ee9858ce48a9_360x146.gif" width="360" height="146" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2127ce84-6239-4d31-8e7b-ee9858ce48a9_360x146.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:146,&quot;width&quot;:360,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1099240,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fe2g!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2127ce84-6239-4d31-8e7b-ee9858ce48a9_360x146.gif 424w, https://substackcdn.com/image/fetch/$s_!fe2g!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2127ce84-6239-4d31-8e7b-ee9858ce48a9_360x146.gif 848w, https://substackcdn.com/image/fetch/$s_!fe2g!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2127ce84-6239-4d31-8e7b-ee9858ce48a9_360x146.gif 1272w, https://substackcdn.com/image/fetch/$s_!fe2g!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2127ce84-6239-4d31-8e7b-ee9858ce48a9_360x146.gif 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a></figure></div><p>As you can imagine, for a statically typed language that aims to provide type safety, this is a misnomer, an anti-pattern and any (pun intended&#9786;) type nerds worth their salt in the Typescript will frown at you for using any. </p><p>So I guess the question now is, why does it even exist? In order to understand why any exists, you need to understand the Typescript type system. If you are trying to master Typescript, one of the very first mental models you need to form is that a Type represents a possible set of values. What do I mean, if the type is a number, then that represents only numbers, and boolean, just true and false. </p><p>This means that when a variable has a type, it can only be assigned values within the set, and errors if it's assigned variables outside the set. When it comes to the <code>any</code> type, this is an open set, meaning any possible value from string, numbers, boolean, etc. forms the set of the <code>any</code> type. Basically, it means all possible values in existence can be assigned to a variable of the <code>any</code> type.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!U1jR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f82b671-b0f5-4917-bef8-08815e0411e7_2000x1500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!U1jR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f82b671-b0f5-4917-bef8-08815e0411e7_2000x1500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!U1jR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f82b671-b0f5-4917-bef8-08815e0411e7_2000x1500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!U1jR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f82b671-b0f5-4917-bef8-08815e0411e7_2000x1500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!U1jR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f82b671-b0f5-4917-bef8-08815e0411e7_2000x1500.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!U1jR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f82b671-b0f5-4917-bef8-08815e0411e7_2000x1500.jpeg" width="1456" height="1092" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8f82b671-b0f5-4917-bef8-08815e0411e7_2000x1500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1092,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:175722,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!U1jR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f82b671-b0f5-4917-bef8-08815e0411e7_2000x1500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!U1jR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f82b671-b0f5-4917-bef8-08815e0411e7_2000x1500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!U1jR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f82b671-b0f5-4917-bef8-08815e0411e7_2000x1500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!U1jR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8f82b671-b0f5-4917-bef8-08815e0411e7_2000x1500.jpeg 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This made sense for Typescript originally, if Typescript was going to experience widespread adoption from Javascript codebases, it needed to support a way of doing gradual migrations. Hence, you could use the <code>any</code> type, both implicitly and explicitly, for any parts of our codebase that we weren't ready to adopt Typescript fully yet. We still do this, even today, when migrating to Typescript from a Javascript codebase.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/subscribe?"><span>Subscribe now</span></a></p><h3>So, why avoid <code>any</code> then?</h3><p>If you have learned anything so far, the <code>any</code> type is very powerful. It has a few superpowers that can easily be abused.</p><p>First, the <code>any</code> type can be assigned to any variable of all other types &#8594; string, number, object, etc, it doesn&#8217;t matter. And the vice-versa is also true, any value can be assigned to a variable with <code>any</code> type. </p><p>This can cause unintended bugs within our system because any could be anything, the Typescript type checker won&#8217;t type check the code when any is involved. You could end up in a situation where you expected a number for customer balance calculation, and instead got something completely different, at the very least providing an unreliable experience to users of the system or could be worse. </p><p>This is something we Javascript/Typescript developers need to be keenly aware of, as the <code>any</code> type also has another superpower, disabling the Type checker. Typescript has no way of Type checking a variable of any type and developers mostly use the <code>any</code> type to disable Type checking in Typescript applications.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7duf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c2e6820-51d1-4873-8329-dbc6646cc5e4_500x624.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7duf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c2e6820-51d1-4873-8329-dbc6646cc5e4_500x624.jpeg 424w, https://substackcdn.com/image/fetch/$s_!7duf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c2e6820-51d1-4873-8329-dbc6646cc5e4_500x624.jpeg 848w, https://substackcdn.com/image/fetch/$s_!7duf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c2e6820-51d1-4873-8329-dbc6646cc5e4_500x624.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!7duf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c2e6820-51d1-4873-8329-dbc6646cc5e4_500x624.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7duf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c2e6820-51d1-4873-8329-dbc6646cc5e4_500x624.jpeg" width="500" height="624" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5c2e6820-51d1-4873-8329-dbc6646cc5e4_500x624.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:624,&quot;width&quot;:500,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7duf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c2e6820-51d1-4873-8329-dbc6646cc5e4_500x624.jpeg 424w, https://substackcdn.com/image/fetch/$s_!7duf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c2e6820-51d1-4873-8329-dbc6646cc5e4_500x624.jpeg 848w, https://substackcdn.com/image/fetch/$s_!7duf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c2e6820-51d1-4873-8329-dbc6646cc5e4_500x624.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!7duf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5c2e6820-51d1-4873-8329-dbc6646cc5e4_500x624.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Unlike all other types in Typescript and by extension Javascript (yes, Javascript has types too, just not static types &#8594; I have an article in the draft that I should finish about this), <code>any</code> is very contagious, and if you assign a variable to the <code>any</code> in place of any other variable or vice versa, Typescript will not warn you, while in case you pass a string in place of a number, Typescript will not let you.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9HTD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff80f9686-8c8b-4361-869a-44ff7f0353dc_800x450.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9HTD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff80f9686-8c8b-4361-869a-44ff7f0353dc_800x450.jpeg 424w, https://substackcdn.com/image/fetch/$s_!9HTD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff80f9686-8c8b-4361-869a-44ff7f0353dc_800x450.jpeg 848w, https://substackcdn.com/image/fetch/$s_!9HTD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff80f9686-8c8b-4361-869a-44ff7f0353dc_800x450.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!9HTD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff80f9686-8c8b-4361-869a-44ff7f0353dc_800x450.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9HTD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff80f9686-8c8b-4361-869a-44ff7f0353dc_800x450.jpeg" width="800" height="450" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f80f9686-8c8b-4361-869a-44ff7f0353dc_800x450.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:450,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9HTD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff80f9686-8c8b-4361-869a-44ff7f0353dc_800x450.jpeg 424w, https://substackcdn.com/image/fetch/$s_!9HTD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff80f9686-8c8b-4361-869a-44ff7f0353dc_800x450.jpeg 848w, https://substackcdn.com/image/fetch/$s_!9HTD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff80f9686-8c8b-4361-869a-44ff7f0353dc_800x450.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!9HTD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff80f9686-8c8b-4361-869a-44ff7f0353dc_800x450.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>My point is to avoid using <code>any</code> unless it&#8217;s absolutely necessary or type checking is not really necessary, which in most cases is. As developers, we are paid big bucks to make these decisions, but just like tests, types are not only there to prevent regressions within our codebase, but also help improve the developer experience, as we can refactor and make changes within our codebase with a lot more confidence, as compared to when types don&#8217;t exist.</p><h3>What should I do now?</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!POzy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63f341c1-fcfb-420e-8ae1-4e03f2c79b15_1176x500.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!POzy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63f341c1-fcfb-420e-8ae1-4e03f2c79b15_1176x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!POzy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63f341c1-fcfb-420e-8ae1-4e03f2c79b15_1176x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!POzy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63f341c1-fcfb-420e-8ae1-4e03f2c79b15_1176x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!POzy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63f341c1-fcfb-420e-8ae1-4e03f2c79b15_1176x500.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!POzy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63f341c1-fcfb-420e-8ae1-4e03f2c79b15_1176x500.jpeg" width="1176" height="500" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/63f341c1-fcfb-420e-8ae1-4e03f2c79b15_1176x500.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:500,&quot;width&quot;:1176,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!POzy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63f341c1-fcfb-420e-8ae1-4e03f2c79b15_1176x500.jpeg 424w, https://substackcdn.com/image/fetch/$s_!POzy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63f341c1-fcfb-420e-8ae1-4e03f2c79b15_1176x500.jpeg 848w, https://substackcdn.com/image/fetch/$s_!POzy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63f341c1-fcfb-420e-8ae1-4e03f2c79b15_1176x500.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!POzy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63f341c1-fcfb-420e-8ae1-4e03f2c79b15_1176x500.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Let&#8217;s say you buy into my argument (hooray &#129395;), what should you do? There are two main types of any within your codebase, the explicit any and the implicit any. I don&#8217;t want to dive too much into the explicit any because those are any types that we as developers (or our teams) have introduced knowingly within our codebase. The solution is to provide proper types, check out the next section if you can&#8217;t, you might find better solutions than just using any. If you are struggling with this in your codebase, consider tracking <a href="https://www.allthingstypescript.dev/p/tracking-type-coverage-in-typescript-why-all-things-typescript-newsletter-issue-18-1271268">Type coverage</a>.</p><p>The second one is implicit, this could come from us not providing types and Typescript doesn&#8217;t have enough information to infer the type of a variable and hence implicit any, for instance, the following function:</p><pre><code>functions doSomething(var) {
                    // ^ implicit any 
}</code></pre><p>You can remedy the above issue by setting typescript to throw an error instead of implicitly inferring a variable type as any. This can be achieved by updating the <code>tsconfig</code> settings and setting <code>noImplicitAny</code> to <code>true</code>. You can also update the <code>useUnknownInCatchVariable</code> property to true while at it so that errors are inferred as any instead of any in try-catch blocks.</p><p>The second possible source of this is third-party libraries that ship without Type declarations or ship incomplete or inaccurate Type declarations. NPM packages ship as Javascript, not Typescript, they have to be usable in the whole ecosystem, including Javascript-only projects. To support Typescript, libraries can ship with Typescript declaration files, which provide Type information about a library that Typescript can use for type checking. </p><p>You need to be aware of this and can remedy this by installing types from Definitely Typed if they exist or you provide the <a href="https://www.typescriptlang.org/docs/handbook/2/type-declarations.html">type declaration</a> yourself. For instance, if you using React, which doesn&#8217;t ship with type declaration, you can do the following:</p><pre><code>npm i --save-dev @types/react</code></pre><p>And just like that, you will Typescript and react to play nice. I will cover providing your own type declarations in a future issue, let me know if you are interested below.</p><p>Also, as I previously touched on at the beginning of this section, please consider tracking Type coverage to aid in improving type safety in your codebase and be aware of where you are trending. You can learn more <a href="https://www.allthingstypescript.dev/p/tracking-type-coverage-in-typescript-why-all-things-typescript-newsletter-issue-18-1271268">here</a> in one of my previous issues. </p><h3>What if I can&#8217;t avoid any?</h3><p>There are situations where you can&#8217;t avoid the <code>any</code> type, maybe because you don&#8217;t have enough information to define the type or it&#8217;s not important because you are not consuming the data within your application, think of a log dump application or a proxy server. In both cases, you don&#8217;t care much about the content shape, as long as save the data being sent to you properly or redirect the requests to the correct server and responses back to the client. </p><p>In this case, I would advise you to consider preferring the <code>unknown</code> type. The unknown type is another powerful type in Typescript, and just like any, all other types can be assigned to the <code>unknown</code> type. However, this is how it differs from the <code>any</code> type, it can only be assigned to another <code>unknown</code> type or <code>any</code> type. </p><p>This means that before using unknown, you will need to resolve the underlying type either using assertions (<a href="https://www.allthingstypescript.dev/p/avoid-using-type-assertions-in-typescript">please avoid</a>) or <a href="https://www.allthingstypescript.dev/p/narrowing-types-in-typescript">Type narrowing</a>. This makes the <code>unknown</code> type is not contagious as it can&#8217;t spread within the codebase just like the <code>any</code> type.</p><p>If you can&#8217;t avoid the <code>any</code> type, I would encourage you to use a more narrower version of the <code>any</code> type. For instance, <code>any[] </code>or <code>Record&lt;string, any&gt;</code> is a lot better than just any. In both cases, we are containing the superpowers of the <code>any</code> type but limiting what types can be assigned to the two types, in the first case, we are saying it has to be an array, the content could be anything, and in the second case we are indicating it has to be an object, with the string keys, but the values could be of any type. </p><p>Is this the best way? Definitely not, but in my eyes and in some special circumstances it&#8217;s better than than just using any.</p><h3>Conclusion</h3><p>That&#8217;s it, we have reached the conclusion of this article, where we discussed about the <code>any</code> type, why it exists, and why you should avoid it. We learned that any exists to make it easy to turn off Typescript hence aiding in the gradual adoption of Typescript, which played a hand in helping in the adoption of Typescript. We also learned that any can be contagious and spread within our codebase, which can lead to bugs. </p><p>We also learned about the <code>unknown</code> type which is a good alternative to any when we can&#8217;t avoid it and how we can instead prefer a narrower version of the <code>any</code> Type instead of just using any when we don&#8217;t have a choice.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.allthingstypescript.dev/p/why-avoid-the-any-type-in-typescript?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.allthingstypescript.dev/p/why-avoid-the-any-type-in-typescript?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><h4>Previous issues you might be interested in</h4><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;f5e29f23-512f-423d-a313-23acbd4b77ed&quot;,&quot;caption&quot;:&quot;We all love Typescript because it brings some sanity when working with Javascript. We define our intention (type annotation), and Typescript checks the code we write against our intentions. This works great for static data within our control, but what about data from sources outside our control, i.e., HTTP requests, files, etc.?&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Typescript: How do you provide types for data from external sources? &quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Software Engineer, Google Developer Expert and Typescript Nerd.&quot;,&quot;photo_url&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/8765c349-75ba-43ee-9976-e3ae5631b064_904x1124.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2022-09-19T06:01:03.814Z&quot;,&quot;cover_image&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/032ff249-19ea-4e03-8479-52d4a669e5ab_1280x720.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/typescript-how-do-you-provide-types&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:73354059,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:2,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;3fa4ede4-aab8-4c77-8fad-c8652d673a5f&quot;,&quot;caption&quot;:&quot;Hey, Wycliffe here, and welcome to this week&#8217;s mid-week scoop. At the beginning of every week, I usually send out an article covering various Techniques and Lessons in Typescript to help you learn and build more Type-safe code in Typescript, such as&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Using Zod Schemas as Source of Truth for Typescript Types&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Software Engineer, Google Developer Expert and Typescript Nerd.&quot;,&quot;photo_url&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/8765c349-75ba-43ee-9976-e3ae5631b064_904x1124.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2023-01-23T18:30:32.487Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f4dc44a2-dccf-44c6-9bdb-19ce7bdab683_1280x853.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/using-zod-schemas-as-source-of-truth&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:97587944,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:2,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;8c88dc58-310d-4291-83f5-9f663fbca996&quot;,&quot;caption&quot;:&quot;Hey, Wycliffe here, and welcome to this week&#8217;s mid-week scoop. At the beginning of every week, I usually send out an article covering various Techniques and Lessons in Typescript to help you learn and build more Type safe code in Typescript, such as&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Narrowing Types in Typescript&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:43214086,&quot;name&quot;:&quot;Maina Wycliffe&quot;,&quot;bio&quot;:&quot;Software Engineer, Google Developer Expert and Typescript Nerd.&quot;,&quot;photo_url&quot;:&quot;https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/8765c349-75ba-43ee-9976-e3ae5631b064_904x1124.jpeg&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2023-01-17T04:42:54.650Z&quot;,&quot;cover_image&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a63afa41-ae6d-4d26-9d7d-4703aa0a2125_1280x720.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.allthingstypescript.dev/p/narrowing-types-in-typescript&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:95731310,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:1,&quot;comment_count&quot;:0,&quot;publication_id&quot;:null,&quot;publication_name&quot;:&quot;All Things Typescript&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7922083f-3e29-485a-ab34-3b07ac80034c_1084x1084.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p></p>]]></content:encoded></item></channel></rss>