Understanding the TypeScript never Type > dev

Skip to content
Entire search within the site

dev

TypeScript Understanding the TypeScript never Type

page info

profile_image
Author Goposu
comment 0co View 24hit Creation date 25-10-10 17:36

๋ณธ๋ฌธ

Understanding the TypeScript never Type

Understanding the TypeScript never Type

Published on October 10, 2025

What is never in TypeScript?

The never type represents values that never occur. It is typically used to indicate that a function never returns, such as when it throws an error or enters an infinite loop.

Basic Example

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

In this example, the function always throws an error, so it never returns a value. TypeScript infers the return type as never.

Use Cases

  • Functions that always throw errors
  • Infinite loops
  • Exhaustive type checking in switch statements

Comparison with Other Types

Type Description
any Allows any value and disables type checking
void Represents functions that return nothing
never Represents functions that never return

Exhaustive Type Checking Example

type Shape = "circle" | "square";

function handleShape(shape: Shape) {
  switch (shape) {
    case "circle":
      // handle circle
      break;
    case "square":
      // handle square
      break;
    default:
      const exhaustiveCheck: never = shape;
      throw new Error(`Unhandled shape: ${shape}`);
  }
}

This pattern ensures that all possible cases are handled. If a new shape is added and not handled, TypeScript will show an error.

Author: Your Name

Tags: TypeScript, never, types, programming

์ถ”์ฒœ0 ๋น„์ถ”์ฒœ0

comment list

There are no registered comments.

Total 19๊ฑด 1 page

search

memberlogin

join

Copyright © https://goposu.com All rights reserved.