Best common type
When a type inference is made from several expressions, the types of those expressions are used to calculate a “best common type”. For example,
let x = [0, 1, null];
To infer the type of x
in the example above, we must consider the type of each array element. Here we are given two choices for the type of the array: number
and null
. The best common type algorithm considers each candidate type, and picks the type that is compatible with all the other candidates.
Because the best common type has to be chosen from the provided candidate types, there are some cases where types share a common structure, but no one type is the super type of all candidate types. For example:
let zoo = [new Rhino(), new Elephant(), new Snake()];
Ideally, we may want zoo
to be inferred as an Animal[]
, but because there is no object that is strictly of type Animal
in the array, we make no inference about the array element type. To correct this, instead explicitly provide the type when no one type is a super type of all other candidates:
let zoo: Animal[] = [new Rhino(), new Elephant(), new Snake()];
When no best common type is found, the resulting inference is the empty object type, {}
. Because this type has no members, attempting to use any properties of it will cause an error. This result allows you to still use the object in a type-agnostic manner, while providing type safety in cases where the type of the object can’t be implicitly determined.
Please login to continue.