Menu
Coddy logo textTech

The 'never' Type

Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 70 of 73.

The never type represents values that never occur. This might sound confusing at first, but it's actually quite logical when you think about certain scenarios in your code.

The never type is most commonly used for functions that never return normally.

This includes functions that always throw an error or contain infinite loops:

function throwError(message: string): never {
  throw new Error(message);
}

function infiniteLoop(): never {
  while (true) {
    // This loop never ends
  }
}

Unlike void, which indicates a function doesn't return a meaningful value, never indicates that the function never reaches its end point at all. The execution either stops due to an error being thrown or continues indefinitely.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create a function named throwError that demonstrates the never return type by always throwing an error and never returning normally.

The function should:

  • Take a parameter message of type string
  • Throw a new Error with the provided message
  • Have an explicit return type of never

Create a second function named handleOperation that:

  • Takes two parameters: operation of type string and value of type number
  • Returns value * 2 if operation is "double"
  • Returns value / 2 if operation is "half"
  • Calls throwError with the message "Invalid operation: [operation]" for any other operation
  • Has an explicit return type of number

Test your functions with the following operations:

  • Call handleOperation("double", 5) and print the result
  • Call handleOperation("half", 8) and print the result
  • Call handleOperation("triple", 3) and print the result (this will throw an error)

Create additional test cases:

  • Call handleOperation("double", 15) and print the result
  • Call handleOperation("half", 20) and print the result

Cheat sheet

The never type represents values that never occur. It's used for functions that never return normally.

Functions that return never include those that always throw errors or contain infinite loops:

function throwError(message: string): never {
  throw new Error(message);
}

function infiniteLoop(): never {
  while (true) {
    // This loop never ends
  }
}

Unlike void (which indicates a function doesn't return a meaningful value), never indicates that the function never reaches its end point at all.

Try it yourself

// TODO: Write your code here
// Create the throwError function with never return type
// Create the handleOperation function with number return type

// Test the functions with try-catch blocks
try {
    console.log(handleOperation("double", 5));
} catch (error) {
    console.log(`Error: ${(error as Error).message}`);
}

try {
    console.log(handleOperation("half", 8));
} catch (error) {
    console.log(`Error: ${(error as Error).message}`);
}

try {
    console.log(handleOperation("triple", 3));
} catch (error) {
    console.log(`Error: ${(error as Error).message}`);
}

try {
    console.log(handleOperation("double", 15));
} catch (error) {
    console.log(`Error: ${(error as Error).message}`);
}

try {
    console.log(handleOperation("half", 20));
} catch (error) {
    console.log(`Error: ${(error as Error).message}`);
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To TypeScript