articles
2 min read
7 days ago

TypeScript never: Catch the Impossible

The never keyword in TypeScript ensures exhaustive checks in logic, signaling that a code path should be unreachable. It’s useful in business logic to catch unhandled cases at compile time, improving safety and reducing bugs—especially in critical flows like payment statuses or user roles.

TypeScript never: Catch the Impossible
Share this content:

In TypeScript, the never keyword is used to represent values that should never occur, making it especially useful in business logic for ensuring exhaustive handling of all possible cases in conditional statements. By applying never in situations like switch statements or type guards, developers can catch logic errors at compile time—such as unhandled conditions—which helps maintain robustness and consistency in critical decision-making flows like payment status handling or user role permissions. This leads to safer, more predictable business logic in TypeScript applications.

Let's see the program:

 

enum Position {
    Programmer = 'Programmer',
    HR = 'HR',
    CEO = 'CEO'
}

type Employee = {
    name: string
    salary: number
    position: Position
}

function payAnnualBonus(empl: Employee) {
    let bonusPercent: number = 0;
    const position = empl.position
    switch (position) {
        case Position.Programmer:
            bonusPercent = 0.2
            break;
        case Position.HR:
            bonusPercent = 0.8
            break;
        case Position.CEO:
            bonusPercent = 200
            break;
        default:
            // exhaustive enum:
            const remainingValues: never = position;
            break;
    }
    console.log(`Paying ${empl.salary * bonusPercent} as bonus to ${empl.name}`)
}

 

Imagine if you create a new Position, what do you think would happen ?

You can copy this code, and put a new entry at Position: Manager = "Manager"

So, in short, what happens is, the remaining values will be throw a compilation error first, because it will return a new Position inside a never type, giving you the hint to deal also with that use case, since Manager was not dealt inside the switch case.
Below you can check the errors.

 

Captura De Pantalla 2025 05 20 a Las 15.40.28

remaining values is catching the new created Position

Captura De Pantalla 2025 05 20 a Las 15.40.52

awareness of the returning type



Hope this clarifies a use case, enjoy using TypeScript in your projects.
That's all, thanks for reading till here.

CTA Card Image
#exploringtech

Explore the world with me...

I'm navigating the worlds of Frontend, Backend, and DevOps—steering through projects while blending innovation with fundamentals. Join me on this journey to uncover fresh insights and level up your skills every step of the way.

me-icon-footer

Tech Explorer

A minimal portofolio and blog website, showing my expertise and spreading the rich content of programming.

#exploringtech
Currently At
Lisbon, PT
All rights reserverd | Copyright 2025