Tuple

A tuple is an alternative to an object to hold multiple values within a single variable. Often used to move information around by function, it leverages an array to carry different types. The assignation with a tuple is done by setting the desired value in a specific index of an array, which the consumer must know to retrieve the pertinent information. In JavaScript, the usage of an array is sufficient. However, doing so in TypeScript leads to a weak type. The following code shows that TypeScript will infer the type to be an array of number or string, which can make sense but not when the code wants to be strongly typed. The reason is that an array can be a number or type at any position of the array, while in a tuple situation we want to have a specific type for each position in the array:

let tuple1 = ["First name", "Last Name", 1980]; // Infered as (string | number)[]
tuple1[2] = "Not a number";
let tuple2: [string, string, number] = ["First name", "Last Name", 1980]; // Tuple
tuple2[2] = "Not a number"; // Does not compile

To overcome the array inference, we need to specify the type for each position. However, even if a tuple is more specific than an array with multiple types because of the specification by the index of the type, this one still is not as safe as using an object:

let tuple3: [string, string, number]
tuple3 = ["Only One"]; // Does not compile
tuple3 = ["One", "Two", 3, "four"]; // Does not compile
tuple3[5] = "12"; // Compile

The previous code illustrates that during instantiation, there is a validation of type as well as a validation of size. It means that when assigning values, you must respect the type and the number of expected values. However, the last line of the code shows the nature of the array surface, and regardless of the fact that the tuple declaration specifies three positions, it is possible to add any type at any position after the ones defined. In the code example, the three first positions (index 0, 1, and 2) are strongly typed but position four and above can be anything. Nevertheless, changing a value, with the square brackets, will validate the type:

tuple3[5] = "12"; // Compile, do not mind the type
tuple3[1]= 2; // Does not compile, require to be a string

Tuple supports spread operator to deconstruct a function parameter into several variables. The following code example shows that a single tuple argument can be spread. The function restFunction is the equivalent of the resultFunction. The code example shows that it is possible to pass a tuple but not an array:

function restFunction(...args: [number, string, boolean]): void{
const p1:number = args[0];
const p2:string = args[1];
const p3:boolean = args[2];
// Code
}

function resultFunction(p1: number, p2: string, p3: boolean): void{
// Code
}

let tuple4: [number, string, boolean] = [0, "one", false];
let array4: (number | string | boolean )[] = [0, "one", false];

restFunction(...tuple4);
restFunction(...array4); // Doesn't compile

restFunction(tuple4[0],tuple4[1],tuple4[2]);
restFunction(array4[0],array4[1],array4[2]); // Does not compile

Tuple supports optional. The syntax is similar to a function with optional parameters or a type with optional members. Positions without value are automatically set to undefined

let tuple5: [number, string?, boolean?];
tuple5 = [1];
tuple5 = [1, "two"];
tuple5 = [1, "two", false];

The previous declaration is similar to the following, where optional position can be set to undefined as well:

let tuple5: [number, (string | undefined)?, (boolean | undefined)?]

Setting the tuple definition in a type can be advantageous when a tuple is reused in several places. The syntax is identical to when defining a type with the keyword type:

type MyTuple = [number, string];
let tuple6:MyTuple = [1, "Test"];

In conclusion, the tuple is a convenient way to pass information in a function and also to quickly return more than one value. However, a better alternative is to define a quick interface with the member desired. Not only does it not rely on position, but it can be reused in many situations easily by allowing extension and intersection. Furthermore, an object is easier to read because the assignation and the readability rely on a name instead of a number.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.70.66