Hack Frontend Community

never Type in TypeScript

The never type in TypeScript is used to denote values that never occur. It's a type that represents values that cannot be returned from a function or assigned to a variable. Usually, the never type is used in the following cases:

  1. When a function doesn't return a value and doesn't complete execution (e.g., throws an exception).
  2. When a function executes infinitely (e.g., with an infinite loop).

The never type helps TypeScript understand that a function or expression won't complete correctly and cannot return a value.

Recommendation:

Use never to explicitly indicate situations where a function shouldn't terminate normally, for example, when throwing errors or in infinite loops. This will help you maintain type safety in your code.