• TypeScript’s never Type
  • Originally by Dornhoth
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: JohnieXu
  • Proofreader

As the Chinese interpretation of never suggests, the never type in TypeScript represents a type where a value never occurs. Such as:

type A = 'A';
type B = 'B';
type C = A & B;
Copy the code

The union type C is the intersection of types A and B, which means that its type values are equal to both A and B. This type definition is called associative type and is supported in TypeScript, but the example is written to indicate that the value of type C is equal to both A and B, which is obviously impossible to achieve, so type C is never.

Type information can be thought of as A collection of elements. Type A corresponds to the set {‘A’}, type B to the set {‘B’}, type C to the set {}, and type never to an empty set.

The never type can be used to declare the return value type of a function, indicating that the function will not have any return value.

function a() :never {
  throw new Error('error');
}
Copy the code

Of course, void can also be used to declare the return type of a function to indicate that it does not have any return value, but the meaning of void is less clear. Void allows functions to return undefined, whereas never does not allow functions to return undefined.

function a() :void {
  return undefined; // Everything is normal
}

function b() :never {
  return undefined; // Type 'undefined' cannot be assigned to type 'never'
}
Copy the code

When a function created with a function expression that does not return a value is not specified, TypeScript automatically assumes that its return value type is never.

However, in the above case, if a function is created using a function declaration, its return value type will be inferred to be void.

This is not a TypeScript bug; it is designed for backward compatibility. Many existing libraries have a large number of abstract function definitions whose return value type information is not explicitly specified as void. Since these functions are abstract (abstract functions have only type information, but no function implementation), the implementation of the function is left to the library user, who is free to decide whether the function has a return value during implementation. If the function type in this case is inferred by TypeScript to be never, then the function implementation cannot return a value (the return keyword is not allowed in the function implementation), which obviously leads to a conflict that prevents the function implementation from being rewritten properly.

class AbstractClass {
    methodToBeOverriden() { // TypeScript assumes that its return type is void
        throw new Error('Not implemented! '); }}Copy the code

Therefore, in actual project development, we cannot rely entirely on TypeScript’s type inference system to help us generate the correct type information. To explicitly indicate that a function returns no value, specify its return value type as never.

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.