TIL — Typescript type guards and predicates

<p>n TypeScript, type guards are a way to provide additional type information at runtime, making your code more type-safe and reducing the possibility of runtime errors. They help ensure that you are working with the correct type of value, enabling you to perform operations specific to that type with confidence.</p> <p>Let&rsquo;s examine a clear example to understand how type guards work. In this example, we have a custom type&nbsp;<code><strong>Foo</strong></code>&nbsp;and a function&nbsp;<code>isValidItem</code>&nbsp;that serves as a type guard for the&nbsp;<code><strong>Foo</strong></code>&nbsp;type:</p> <pre> type Foo = &#39;bar&#39; | &#39;baz&#39;; const isValidItem = (x: any): x is Foo =&gt; { return typeof x === &#39;string&#39; &amp;&amp; (x === &#39;bar&#39; || x === &#39;baz&#39;); }; const item: any = &#39;bar&#39;; if (isValidItem(item)) { item; }</pre> <p>Let&rsquo;s break down the example step-by-step:</p> <ol> <li><strong>Defining a custom type:</strong>&nbsp;We define a custom type&nbsp;<code><strong>Foo</strong></code>&nbsp;as a union of two string literal types, &#39;bar&#39; and &#39;baz&#39;:&nbsp;<code><strong>type Foo = &#39;bar&#39; | &#39;baz&#39;;</strong></code>.</li> <li><strong>Creating a type guard function:</strong>&nbsp;We declare the&nbsp;<code>isValidItem</code>&nbsp;function with a single parameter&nbsp;<code><strong>x</strong></code>&nbsp;of type&nbsp;<code><strong>any</strong></code>. The function uses a type predicate&nbsp;<code><strong>x is Foo</strong></code>&nbsp;to indicate that if it returns&nbsp;<code><strong>true</strong></code>,&nbsp;<code><strong>x</strong></code>&nbsp;is of type.</li> <li><strong>Checking the type:</strong>&nbsp;The function body checks if&nbsp;<code><strong>x</strong></code>&nbsp;is a string and if it&#39;s either &#39;bar&#39; or &#39;baz&#39;. If either condition is met, it returns&nbsp;<code><strong>true</strong></code>, meaning that&nbsp;<code><strong>x</strong></code>&nbsp;can be considered of type&nbsp;<code><strong>Foo</strong></code>:</li> <li><strong>Using the type guard:</strong>&nbsp;We use the&nbsp;<code>isValidItem</code>&nbsp;function as a type guard in the&nbsp;<code><strong>if</strong></code>&nbsp;statement:&nbsp;<code><strong>if (isValidItem(item)) { ... }</strong></code>. If the condition is true (i.e.,&nbsp;<code><strong>isValidItem(item)</strong></code>&nbsp;returns&nbsp;<code><strong>true</strong></code>), TypeScript knows that within this block, the&nbsp;<code><strong>item</strong></code>&nbsp;variable is of type&nbsp;<code><strong>Foo</strong></code>.</li> <li><strong>Safely using the variable:</strong>&nbsp;Inside the&nbsp;<code><strong>if</strong></code>&nbsp;block, we can safely use the&nbsp;<code><strong>item</strong></code>&nbsp;variable as a value of type&nbsp;<code><strong>Foo</strong></code>&nbsp;without any type errors.</li> </ol> <p><a href="https://medium.com/@AbbasPlusPlus/til-typescript-type-guards-and-predicates-1ed69cb1ed21">Website</a>&nbsp;</p>