Why is never assignable to every type?

后端 未结 3 1617
天涯浪人
天涯浪人 2021-01-13 16:02

The TypeScript documentation says that

The never type is a subtype of, and assignable to, every type

but doesn\'t m

3条回答
  •  深忆病人
    2021-01-13 16:44

    The type that TypeScript calls never is what's known in type theory as a bottom type, sometimes referred to with the symbol "⊥". The idea is that it is the (unique) type for which there are no values of that type. You should never find yourself holding a value of that type because it has no values. If you think of types as sets of possible values, then it is the empty set (symbol "∅").

    This probably all makes sense to you.

    TypeScript also has the notion of subtyping. Just like sets, types can overlap by containing some of the same values. If every value of type A is also a value of type B, then A is a subtype of B. You can also say that A extends B, or symbolically, A <: B. In TypeScript, {a: string} is a subtype of object, because every value of type {a: string} (for example, the value {a: "hello"}) is also a value of type object.

    TypeScript's assignability rules are basically related to substitutability. If a variable is of type B, and A <: B, then you can assign a value of type A to that variable, because every value of type A is also a value of type B. You can't necessarily do the reverse, assigning a value of type B to a variable of type A. Unless B <: A, there are some values of type B which are not values of type A.

    From the types-as-sets-of-values point of view, A <: B is like saying the set of values of type A is a subset of the set of values of type B, (symbols A ⊆ B).

    This probably (I hope) all makes sense to you too.

    One more thing we need: the logical principle of explosion. If you start with a statement that is false, then you can prove anything at all from it. So, assuming "the moon is made of cheese" is false, then "If the moon is made of cheese, then today is Wednesday" is true. Also, "if the moon is made of cheese, then today is not Wednesday" is true. There are dire consequences for taking something false to be true: everything explodes.

提交回复
热议问题