<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Uduak 'Eren</title>
    <description>The latest articles on DEV Community by Uduak 'Eren (@meekg33k).</description>
    <link>https://dev.to/meekg33k</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F356470%2F4a3f9cd6-9265-46ff-b63a-c6f629ff0f72.jpeg</url>
      <title>DEV Community: Uduak 'Eren</title>
      <link>https://dev.to/meekg33k</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/meekg33k"/>
    <language>en</language>
    <item>
      <title>TypeScript Generics and Type Guards - Explained by Example</title>
      <dc:creator>Uduak 'Eren</dc:creator>
      <pubDate>Wed, 01 Apr 2020 04:24:18 +0000</pubDate>
      <link>https://dev.to/meekg33k/typescript-generics-and-type-guards-explained-by-example-20kp</link>
      <guid>https://dev.to/meekg33k/typescript-generics-and-type-guards-explained-by-example-20kp</guid>
      <description>&lt;p&gt;TypeScript has some powerful features that enable easy building of scalable JavaScript applications. This article describes how we leveraged two TypeScript features: generics and type guards in writing a robust utility function.&lt;/p&gt;

&lt;h2&gt;
  
  
  How a utility function was born
&lt;/h2&gt;

&lt;p&gt;If you had to write logic to check if an object of a certain type is defined and if the object actually has any keys, a simple way to do it in TypeScript would be:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;If you had to use this logic repeatedly, an ideal thing to do would be to create a function that encapsulates this logic. Written in TypeScript, this function would look something like: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;Good stuff right? Let's consume this utility function and see what happens. The code snippet below shows what we see in a VS code editor when we use the utility function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pg7H_p0b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fnr8lkkh1yfsv2dblemt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pg7H_p0b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fnr8lkkh1yfsv2dblemt.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
Typescript compiler, huh?



&lt;p&gt;Okay, what just happened? Why are we having the error above? &lt;/p&gt;

&lt;p&gt;If you look closely at the code snippet above, the logic inside the function is the same logic we used before we wrote the function but for some reason, TypeScript is complaining. What are we doing wrong?&lt;/p&gt;
&lt;h3&gt;
  
  
  Issue I: TypeScript doesn't "recognize" the logic in the utility function
&lt;/h3&gt;

&lt;p&gt;How do we solve this issue with TypeScript not recognizing the correct logic contained within the &lt;code&gt;isPresentSomeObject&lt;/code&gt; utility function?&lt;/p&gt;

&lt;p&gt;One way to fix this would be to use the &lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator"&gt;non-null assertion operator&lt;/a&gt; (!) in TypeScript which is basically you telling the compiler, you know what you're doing.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;While this works, I'm not a huge fan of this approach maybe because I'm lazy and don't want to type the non-null assertion operator (!) every time I have to use this function.&lt;/p&gt;

&lt;p&gt;But seriously is there a better, more scalable way we could rewrite this? Hold that thought while we look at what the second issue with this function is.&lt;/p&gt;
&lt;h3&gt;
  
  
  Issue II: The utility function does not currently support other types
&lt;/h3&gt;

&lt;p&gt;Our utility function works with arguments of &lt;code&gt;SomeObjectType&lt;/code&gt; type, what if we had to use the same function with another object of a different type, &lt;code&gt;AnotherObjectType&lt;/code&gt;? We could refactor the utility function to support the new type by creating a union of the existing type and the new type, as shown below: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;Okay, say we have to support a third type and a fourth type? Add the types to the union? Seriously? What if there was a fifth type?&lt;/p&gt;

&lt;p&gt;You'll agree that creating a union of more types is not scalable and at this point, you may catch yourself thinking "&lt;em&gt;Why not just make the argument of type &lt;code&gt;any&lt;/code&gt; so it can accept 'all' types&lt;/em&gt;?"&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;Err, hang on! The challenge with using type any is that we lose the type safety that comes with TypeScript. So how can we rewrite this function without having to use type &lt;code&gt;any&lt;/code&gt;?&lt;/p&gt;


&lt;h2&gt;
  
  
  Hello Generics!
&lt;/h2&gt;

&lt;p&gt;According to Wikipedia, Generics are a facility of generic programming that allows you extend a &lt;a href="https://en.wikipedia.org/wiki/Type_system"&gt;type system&lt;/a&gt; to allow "a type or method to operate on objects of various types while providing compile-time type safety".&lt;/p&gt;

&lt;p&gt;Imagine you had an identity function that takes an argument of a certain type and returns the argument. If you had to create one function each for a string, Boolean and number types, your code could possibly look like the code below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;Hopefully not because the code snippet above violates the &lt;a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself"&gt;DRY&lt;/a&gt; principle. What we need instead is a way to reuse this logic regardless of argument type. Already seeing the similarity between this and our utility function? &lt;/p&gt;



&lt;p&gt;Generics provides that abstraction that allows you define parameter types (to be specified later) making it possible to reuse code with different types. With generics, the identity function above becomes:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;By doing this, we are telling the TypeScript compiler "&lt;em&gt;hey, the argument type is of a parameter type, &lt;strong&gt;T&lt;/strong&gt;, and you'll find out what &lt;strong&gt;T&lt;/strong&gt; is at compile time&lt;/em&gt;". This allows us to pass any type without losing type-checking.&lt;/p&gt;

&lt;p&gt;Rewriting our utility function to use generics, the function becomes:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;With this, you get both the dynamism that comes with using any type and the safety that comes with type checking: double win, if you ask me. &lt;/p&gt;

&lt;p&gt;Challenge 1 solved! Next stop, how do we get the compiler to "recognize" our utility function?&lt;/p&gt;
&lt;h2&gt;
  
  
  Introducing User-Defined Type Guards
&lt;/h2&gt;

&lt;p&gt;If you've used TypeScript for a while but haven't heard of type guards, you're in good company. Until we ran into this challenge, I hadn't heard of them either.&lt;br&gt;
Let's imagine you have the following defined interfaces:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;If you had to implement logic that would check for what type an animal is, how would you do it? One way would be to use a &lt;a href="https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions"&gt;type assertion&lt;/a&gt;: &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;Great! What happens if more types are added to the &lt;code&gt;Animal&lt;/code&gt; type union?&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;type Animal = Cat | Dog | Goat | Horse | Kangaroo | ...&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Writing a function for each new type isn't scalable. How then can we create a utility function that allows the TypeScript compiler infer the type of a variable without having to explicitly use type assertion? This is where type guards come to play.&lt;/p&gt;

&lt;p&gt;From the TypeScript &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards"&gt;documentation&lt;/a&gt;, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;a type guard is some expression that performs a runtime check that guarantees the type in some scope&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What does that even mean?&lt;/p&gt;

&lt;p&gt;The gist is that with type guards, you can instruct the TypeScript compiler to infer a specific type for a variable in a certain context. &lt;/p&gt;

&lt;p&gt;By using a feature called &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates"&gt;type predicates&lt;/a&gt;, type guards allow you to tell the TypeScript compiler that the type of an argument is &lt;u&gt;what you say it is&lt;/u&gt;. This process of refining types to more specific types is called &lt;em&gt;narrowing&lt;/em&gt; and it ensures that the variable type is the exact type you expect at runtime.&lt;/p&gt;

&lt;p&gt;Predicates are functions that return Boolean values. Type predicates are of the form &lt;code&gt;parameterName is Type&lt;/code&gt;,  &lt;code&gt;parameterName&lt;/code&gt; being the name of the function argument and &lt;code&gt;Type&lt;/code&gt; the argument type, and they also return Boolean values.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you define a type guard?
&lt;/h3&gt;

&lt;p&gt;To define a type guard, simply define a function whose return type is a &lt;em&gt;type predicate&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;So an &lt;code&gt;isCat&lt;/code&gt; type guard is a function that has the &lt;strong&gt;&lt;code&gt;animal is Cat&lt;/code&gt;&lt;/strong&gt; predicate as its return type as shown below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;Note that I didn't even have to implement any 'serious' logic in the function, all I had to do was return &lt;code&gt;true&lt;/code&gt; because remember, predicates are functions that return Boolean values.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;isCat&lt;/code&gt; type guard tells the type system that we've confirmed that the &lt;code&gt;animal&lt;/code&gt; argument passed is a &lt;code&gt;Cat&lt;/code&gt;, so when the compiler does its control flow analysis and sees our type guard, it says "&lt;em&gt;Oh! Whatever is passed into this function has to be a Cat if it returns true&lt;/em&gt;". &lt;/p&gt;

&lt;p&gt;It then narrows the argument type from &lt;code&gt;Animal&lt;/code&gt; to &lt;code&gt;Cat&lt;/code&gt;, that way, we are guaranteed that what the type we get at runtime will be a structure that is consistent the &lt;code&gt;Cat&lt;/code&gt; interface definition.&lt;/p&gt;

&lt;p&gt;To further appreciate type guards, let's use it in an if-else block:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3HtjRfUa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/864x2xyq4mwl3f909353.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3HtjRfUa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/864x2xyq4mwl3f909353.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
Typeguards in if-else blocks



&lt;p&gt;Remember again that we didn't implement any 'serious' logic in the &lt;code&gt;isCat&lt;/code&gt; type guard, but the compiler sees the return type as a &lt;code&gt;Cat&lt;/code&gt; because of the &lt;code&gt;animal is Cat&lt;/code&gt; predicate; so it doesn't throw any error in the &lt;code&gt;if&lt;/code&gt; block of the code snippet above. &lt;/p&gt;

&lt;p&gt;What happens in the else block however is more interesting.&lt;/p&gt;

&lt;p&gt;Because the compiler expects a &lt;code&gt;Cat&lt;/code&gt; type in the &lt;code&gt;if&lt;/code&gt; block, it says whatever is in the &lt;code&gt;else&lt;/code&gt; block has got to be a different type - that type is the &lt;a href="https://www.typescriptlang.org/docs/handbook/basic-types.html#never"&gt;never&lt;/a&gt; type which is a type used for values that never occur, hence the error message you see in the &lt;code&gt;else&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;Hopefully that was clear, now let us apply type guards to our utility function and see how that solves the problem we described at the start of this writing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Converting our utility function to a type guard
&lt;/h3&gt;

&lt;p&gt;Since a type guard is just a function whose return type is a type predicate, all we need to do is to modify our utility function to return a type predicate.&lt;/p&gt;

&lt;p&gt;What type predicate however, do we use for this utility function?&lt;/p&gt;

&lt;p&gt;Recall that generics allow you to define parameter types which then get assigned at compile time. To convert our utility function to a type guard, we would take advantage of that and make our return type a type predicate of the form &lt;code&gt;arg is T&lt;/code&gt; where &lt;code&gt;T&lt;/code&gt; is the argument type.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
 

&lt;p&gt;And now it works!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rsfRaxar--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w0a5spfsilb1pon4mo70.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rsfRaxar--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w0a5spfsilb1pon4mo70.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Thank you for reading. I hope reading this has raised your knowledge level about TypeScript generics and user type guards but more importantly, I hope it has stoked your interest in understanding how the TypeScript compiler works.&lt;/p&gt;

&lt;p&gt;If you want to learn more about type guards, the type system or the TypeScript compiler generally, you may find the links in the Further Reading section helpful.&lt;/p&gt;

&lt;p&gt;If you'd like to chat about this or anything at all, I'll be happy to connect via &lt;a href="https://www.twitter.com/meekg33k"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/uduak-obong-eren"&gt;LinkedIn&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Stay safe 😊.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If you catch yourself using type any as a way of making a piece of code more robust, think generics.&lt;/li&gt;
&lt;li&gt;Type guards are powerful for narrowing types, satisfying the TypeScript compiler's control flow process and guaranteeing runtime type-safety.&lt;/li&gt;
&lt;li&gt;Think type guards when the type you want to use is not as specific as you'd want or when you want to verify that external data is of a specific type.&lt;/li&gt;
&lt;li&gt;There are built-in type guards that come with TypeScript like &lt;code&gt;typeof&lt;/code&gt; and &lt;code&gt;instanceof&lt;/code&gt; but user-defined type guards can be more powerful when used right.&lt;/li&gt;
&lt;li&gt;Like all things powerful, user-defined type guards shouldn't be abused; they shouldn't be thought of as a way to hack the TypeScript compiler.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://emmanueltouzery.github.io/blog/posts/2018-04-07-prelude-type-guards.html"&gt;Type guards and conditional types in typescript &amp;amp; preclude-ts&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://rangle.io/blog/how-to-use-typescript-type-guards/"&gt;How to get the types you want with TypeScript type guards | Rangle.io&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://microsoft.github.io/TypeScript-New-Handbook/chapters/narrowing/"&gt;Narrowing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://mariusschulz.com/blog/control-flow-based-type-analysis-in-typescript"&gt;Control Flow Based Type Analysis in TypeScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@jackhmwilliams/type-narrowing-in-typescript-44a6757c5a64"&gt;Type Narrowing in TypeScript&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>codequality</category>
      <category>refactorit</category>
      <category>typescript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
