Tuples ([A, B, ...]
)
A Tuple is a fixed-length set of items of the given types. Example:
type Point = [number, number]
interface Math extends HybridObject {
distance(a: Point, b: Point): number
}
Tuples can also have different types per value:
Bad ❌
type Values = (number | string | Person)[]
interface Math extends HybridObject {
calculate(values: Values): void
}
The type in the Bad ❌ example generates an array of variants, where it's size is unknown and each value could be a number
, a string
or a Person
. It is less efficient than a tuple because of the variant allocation.
Good ✅
type Values = [number, string, Person]
interface Math extends HybridObject {
calculate(values: Values): void
}
The type in the Good ✅ example generates a tuple, where it's size is guaranteed to be 3 and each value is known at compile-time: values[0]: number
, values[1]: string
, values[2]: Person
.