Arrays and tuples
Arrays
This can also be done a second way using the generic array type, Array<elemType>
:
...but you can mostly ignore this for now, because: (a) Generics are an advanced topic that weβll cover later, and (b) this is not the Shopify-preferred way for indicating array types anyway.
Tuples
A tuple is a general way of grouping together some number of other values with a variety of types into one compound type. Each position in the tuple has a type, and the types of the different values in the tuple donβt have to be the same.
For example:
When accessing an element with a known index, the correct type is retrieved:
When accessing an element outside the set of known indices, a union type is used instead. Union types are a topic that weβll cover in depth later, but if we use a simplified definiton, they can be said to apply an "OR" condition to each type in the set of types.
In this example, the union type string | number
can have assigned to it any value of type string
or number
:
Last updated