# Exhaustive branch checks with TypeScript

DevFeed: [Exhaustive branch checks with TypeScript](<https://devfeed.tech/articles/exhaustive-branch-checks-with-typescript-52577.md>)

Original publisher: [Read original article](<http://www.jackfranklin.co.uk/blog/typescript-exhaustive-branches/>)

Published: 2024-03-04T00:00:00Z

Content type: tutorial

Language: en

Sources: [Jack Franklin](<https://devfeed.tech/sources/jack-franklin.md>)

Topics: [TypeScript](<https://devfeed.tech/topics/typescript.md>), [exhaustive matching](<https://devfeed.tech/topics/exhaustive-matching.md>), [union types](<https://devfeed.tech/topics/union-types.md>), [enum](<https://devfeed.tech/topics/enum.md>)

Tags: [argument](<https://devfeed.tech/tags/argument.md>), [branch](<https://devfeed.tech/tags/branch.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [enum](<https://devfeed.tech/tags/enum.md>), [error](<https://devfeed.tech/tags/error.md>), [exhaustive](<https://devfeed.tech/tags/exhaustive.md>), [function](<https://devfeed.tech/tags/function.md>), [parameter](<https://devfeed.tech/tags/parameter.md>), [switch](<https://devfeed.tech/tags/switch.md>), [type](<https://devfeed.tech/tags/type.md>), [typescript](<https://devfeed.tech/tags/typescript.md>), [union](<https://devfeed.tech/tags/union.md>)

## AI overview

When branching on a TypeScript union or enum, add a `default` branch that calls `ensureExhaustive(value)`, a helper accepting `never`. If the type later gains a value that the branches do not handle, TypeScript reports that the value is not assignable to `never` and identifies the missing variant. This can catch unhandled cases even in `void` functions, where a missing branch may otherwise produce no error. The excerpt says the same approach works with narrowed `if`/`else` branches. The helper throws at runtime if reached.

## Source excerpt

It's very common when working with TypeScript that you will have a type that declares a list of values, such as an enum or union type: enum SupportedColour1 { RED, YELLOW, BLUE, } type SupportedColour2 = 'RED' | 'YELLOW' | 'BLUE' And then you will often have functions that need to run differently or return different values based on what variant is passed in: function codeForColour1(colour: SupportedColour1): string { switch (colour) { case SupportedColour1.BLUE: return '#0000ff' case SupportedColour1.RED: return '#ff0000' case SupportedColour1.YELLOW: return '#ffff00' } } function codeForColour2(colour: SupportedColour2): string { switch (colour) { case 'BLUE': return '#0000ff' case 'RED': return '#ff0000' case 'YELLOW': return '#ffff00' } } Updating code when types change One thing that can catch you out is if the list of valid values in the type changes. Let's say we want to add GREEN as a value to our union type (I will drop the enum example for brevity, but the functionality for the sake of this post is equivalent): type SupportedColour2 = 'RED' | 'YELLOW' | 'BLUE' | 'GREEN' In this case we will now get an error in this function. TypeScript will realise that the function does not return anything in the case that GREEN is passed in, but the error is not very obvious: function codeForColour2(colour: SupportedColour2): string { switch (colour) { case 'BLUE': return '#0000ff' case 'RED': return '#ff0000' case 'YELLOW': return '#ffff00' } } Function lacks ending return statement and return type does not include 'undefined'. If the function did not return a value, TypeScript will not error and it would be easy to miss that we now have a branch (for GREEN) that is not dealt with: // This will not error in TypeScript, even though we are not // dealing with the GREEN case. function logForColour(colour: SupportedColour2): void { switch (colour) { case 'BLUE': console.log('User picked blue') case 'RED': console.log('User picked red') case 'YELLOW': console.log('User picked