Never Say Never, The never Keyword In TypeScript And Exhaustive Matching
Tags: typescript, javascript, webdev, tutorial
Published: 2023-04-24 13:35:03

>  Ever heard of the **never** keyword in **TypeScript** and know when to use it?

![The never keyword in TypeScript](https://cdn-images-1.medium.com/max/2000/1*7BtXXIgGc_gT8zXEoujiVA.png)

## Exhaustive Matching

**Exhaustive Matching** is when you try to match a **discriminant property** to its corresponding **value/function.** This is better explained through an example.

## Example

![An example of Exhaustive Matching](https://cdn-images-1.medium.com/max/2000/1*-ktrWgctvU4hpv8ewmcGIw.png "An example of Exhaustive Matching")

The above **colorToString** function is an example of **Exhaustive Matching.** Where we try to map all **enums** of **COLORS** to a **corresponding value.**

_The main idea of **Exhaustive Matching** is we try to match a set of values to another set of values (can also be functions, DOM elements, etc.)._

## The Problem?

The problem arises when we try to add another value to our **COLORS enum.**

When we update our **enums,** the **compiler** does not **warn** us that we have forgotten to update our **colorToString** function as well. Thus, we might forget to handle this newly added color.

![No warning from the compiler. Even though we have added a new color.](https://cdn-images-1.medium.com/max/2000/1*ZQIKPtXwzsqyzc6e-hL48w.png "No warning from the compiler. Even though we have added a new color.")

## The Solution?

Use the **never** keyword in **TypeScript!**

We can assign the **color** to a type of **never** in **TypeScript.** This tells the compiler to warn us if we forget to update a function that **Exhaustive Matches.**

Like so:

![Compiler warning when we forget to update our Exhaustive Matching function](https://cdn-images-1.medium.com/max/2000/1*m7fMCSm0HMvYlyRclRSmOw.png "Compiler warning when we forget to update our Exhaustive Matching function")

Contrast this to when we handle our function appropriately.

![No warnings from the compiler when we handled all our enums](https://cdn-images-1.medium.com/max/2000/1*ILw8QxLjCqG3JSiHiH95tQ.png "No warnings from the compiler when we handled all our enums")

## Conclusion

I hope that from these simple examples, you have learned a new tool for you! This is an especially important skill to use if you are in a large project and there are multiple such functions scattered around in the project!

---

## A Step Further

- You can abstract out the **assert never** into a **function** for **reusability** throughout your codebase.

![Using a function to assert never for reusability](https://cdn-images-1.medium.com/max/2000/1*ZqWmlUHGfFfipeQ1FCii0g.png "Using a function to assert never for reusability")

- Besides coming in as a form of a **switch/case statement,** **Exhaustive Matching** functions can be in a form of **if/else statements** or **if/return statements.**

![Other forms of Exhaustive Matching functions](https://cdn-images-1.medium.com/max/2000/1*RRuVg1tuEapoyVDpp8GP1Q.png "Other forms of Exhaustive Matching functions")

- You can play with the examples shown, in this [TypeScript Playground.](https://www.typescriptlang.org/play?#code/KYOwrgtgBAwg8gGTgJQMpQN4Cgq6sgUQBEAaKAIQQFUCyBxQggOTIE0CEkB1LAXyywBjAPYgAzgBcoIgDbCATgBVhqCfICWIAOZQAvFAAUshQC5YiFKgCUegHyYceMQHd1EwQAtDx+Tex4A6QBDMWBzJDQAOkIiE0dAwPlgCTB5ECgAIiSAEwyAbnjAwRCw+AjUSMoaOISEpJS0zIAjGTBgfMKA4tDwy0iGAmYa2oD61PSMrSTQDpHs4AAzILAZCWGR3AB6TelRSSgQ0PkJJmAAN2B5MxBzy71duXkCjbxDy5Pb+SNhR6tnl6gY0aGRAwgeClmeH4-AWYBAggk6lEBzERw+Fy+AA8TDcMVYMPwBCJxFIfMoAGLyMBue7fR5mMqWGy6ez+PDqBbeH4KPS6fSMqIxPydPBAiZBAAOEpk7X+uF4UGAMh6HK5j15-IsUSqBGFIzFzVawCal3kAE9IfLFcqwqq6Ty+Zryv1GEw9bUDRkJcAgvJLVAFUqemyEm9jqcMfbfHLEslxplQVAFlS3GJ-dChHtSdylMIABLATG0nwMrXWOwOAJ2nwa3qC4juupx4EAYnJ5IADF2O+nCtWc7WBRUdY3Yw0Ji3ux3272q5yo4Oyy7Bm7Kx7mxOu+2u7PXqj3hHLlG-oVPZOpz2CoTM+IfsBInItFHlKoNNoDEPog2-lBtlBH2IZg5EAA)
{% embed https://www.typescriptlang.org/play?#code/KYOwrgtgBAwg8gGTgJQMpQN4Cgq6sgUQBEAaKAIQQFUCyBxQggOTIE0CEkB1LAXyywBjAPYgAzgBcoIgDbCATgBVhqCfICWIAOZQAvFAAUshQC5YiFKgCUegHyYceMQHd1EwQAtDx+Tex4A6QBDMWBzJDQAOkIiE0dAwPlgCTB5ECgAIiSAEwyAbnjAwRCw+AjUSMoaOISEpJS0zIAjGTBgfMKA4tDwy0iGAmYa2oD61PSMrSTQDpHs4AAzILAZCWGR3AB6TelRSSgQ0PkJJmAAN2B5MxBzy71duXkCjbxDy5Pb+SNhR6tnl6gY0aGRAwgeClmeH4-AWYBAggk6lEBzERw+Fy+AA8TDcMVYMPwBCJxFIfMoAGLyMBue7fR5mMqWGy6ez+PDqBbeH4KPS6fSMqIxPydPBAiZBAAOEpk7X+uF4UGAMh6HK5j15-IsUSqBGFIzFzVawCal3kAE9IfLFcqwqq6Ty+Zryv1GEw9bUDRkJcAgvJLVAFUqemyEm9jqcMfbfHLEslxplQVAFlS3GJ-dChHtSdylMIABLATG0nwMrXWOwOAJ2nwa3qC4juupx4EAYnJ5IADF2O+nCtWc7WBRUdY3Yw0Ji3ux3272q5yo4Oyy7Bm7Kx7mxOu+2u7PXqj3hHLlG-oVPZOpz2CoTM+IfsBInItFHlKoNNoDEPog2-lBtlBH2IZg5EAA %}

---

If you felt that you have learned something new, do feel free to **follow** 🥰, drop a **react 💖** or write a **comment** ✍️!
It helps makes me create and share more valuable content for **you** to become a better **Web Developer!** 🤗