6

Typescript playground

I'm having some trouble while trying to wrap my head around the fact that the type never is meaningless and can be discarded when it's inside a union type.

I mean, I get that inside an intersection type, never will immediately make everything result in never, since no type can by some type and never at the same type. It makes sense to me.

enter image description here

But in an union type, my instinct initially tells me that the never type would be a valid option. My question is why isn't it? Why does never can be discarded in a union type?

enter image description here

Could anybody give a reasonable explanation about this so I can understand it better?

2
  • 6
    If you were using sets instead of types, then string & never = never is like A ∩ ∅ = ∅ (where is the empty set) and string | never = string is like A ∪ ∅ = A. Or if you are using logical propositions, string & never = never is like A ∧ ⊥ = ⊥ (where is "false") and string | never = string is like A ∨ ⊥ = A. Or if you really squint and use multiplication and addition, then string & never = never is like a × 0 = 0 and string | never = string is like a + 0 = a. Do you want the analogy spelled out as an answer? Or are you happy with the answer from VLRoyrenn below?
    – jcalz
    Oct 6, 2020 at 20:44
  • @jcalz: Should be an answer. Was about to explain it using sets as well.
    – H.B.
    Oct 6, 2020 at 22:00

2 Answers 2

13

One way to think of a type is as a set of all the values that are assignable to it. So boolean can be thought of as {true, false}, the set containing just those two values. And string can be thought of as the (essentially) infinite set containing every possible string value.

In TypeScript, never is the bottom type. It has no values. If you have a JavaScript value and you ask "is this a value of type never?" then the answer is "no". In terms of sets, never can be thought of as ∅, the empty set.

In the mapping from types to sets-of-values, the intersection operation in TypeScript (&) can be thought of as the set intersection operation (∩). If you have sets A and B, then A∩B is the set of exactly the objects which are members of both A and B. For any set A, the intersection A∩∅ with the empty set is just the empty set ∅. There are no elements in both A and the empty set, since there are no elements in the empty set at all. Back in TypeScript types, this means A & never becomes never for any type A. It would be valid if the TypeScript compiler just left string & never as string & never, but in fact it goes ahead and reduces it to never automatically, since the latter representation is simpler.

On the flip side: in the mapping from types to sets-of-values, the union operation in TypeScript (|) can be thought of as the set union operation (∪). If you have sets A and B, then A∪B is the set of exactly the objects which are members of either A or B (this is an inclusive or). For any set A, the union A∪∅ with the empty set is just A. The union contains all the elements of A and all the elements of the empty set. Since there are no elements of the empty set, that's just "all the elements of A". Back in TypeScript types, this means A | never becomes A for any type A. It would be valid if the TypeScript compiler just left string | never as string | never, but in fact it goes ahead and reduces it to string automatically, since the latter representation is simpler.

So that's the basic explanation. There are other analogies, such as boolean logic propositions like "this element is a member of this type" which is always FALSE for the never type, leading to things like A ∧ FALSE = FALSE and A ∨ FALSE = A. Or like arithmetic, where the analogy isn't exact, but intersection looks like multiplication and union looks like addition (this analogy becomes exact for pairs instead of intersection and discriminated unions instead of regular unions) and the never type is 0. But hopefully this gives enough intuition about why the compiler behaves this way.

Note that there's also a top type in TypeScript called unknown which behaves exactly as the dual to never in that A & unknown = A and A | unknown = unknown and has the dual analog in set theory (the universal set/class). But you didn't ask about that and this answer is already long enough as it is.

1
  • Thank you for this detailed explanation. Now it makes total sense to me. I think that what I was getting wrong was the fact that I was thinking of string as being "an actual string type" instead of a representation of all possible string types. Oct 7, 2020 at 14:28
1

Explained concretely, a variable of type UNION_A = string | boolean | never can take any valid value for a boolean, any valid value for a string and any valid value for never (of which no values can exist by definition), so a union with the never type adds nothing to the domain of values this variable may end up receiving.

EDIT: The point of the never type is that it's the type of something that cannot happen, usually the return type of function that can't possibly return.

function fail() {
    throw new Error(":(");
    // What is the *return* type of that function?
}

let foo = fail(); // foo can only receive the type never
console.log(foo * 2); // Doesn't matter what foo is, this is guaranteed to be dead code

There is no possible way for fail() to return a value, not even null. It can never return, so its return type is never, and any code that operates using the return value of that function is basically just dead code.

A union with never usually means you have this

function do_thing(x) {
    let foo;
    if (x == 0) {
        fail(); // foo is of type never
    } else {
        foo = 1/x; // foo is of type number
    }

    // Foo is technically of type number | never,
    // but you can discard never since it never runs.
    let bar = foo * 2;
    return bar;
}
5
  • This is a great answer but there's one issue. A return type of never is not inferred from a throw statement. Oct 6, 2020 at 17:47
  • @AluanHaddad: Isn't it in Typescript? I've seen never be inferred in a number of other cases, and I know diverging functions are a common use case for that type in general, but that's actually surprising to hear. Serves me right for not double-checking.
    – VLRoyrenn
    Oct 6, 2020 at 18:25
  • This case is an odd one out. Inference of return types is usually more precise if anything than an explicit annotation and I take advantage of it in most cases. Oct 6, 2020 at 18:32
  • What is fail()? What is Exception()? It helps when your code compiles in an IDE... but this has errors. Do you mind editing the code here so that it works the way you're describing?
    – jcalz
    Oct 6, 2020 at 20:38
  • @jcalz: My bad, I keep forgetting that JS's default exception class is Error. Fixed the code so it's at least valid JS/TS.
    – VLRoyrenn
    Oct 8, 2020 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.