1.

Explain the tuple types in TypeScript.

Answer»

Tuples are a special TYPE in TypeScript. They are SIMILAR to arrays with a fixed number of elements with a known type. However, the types need not be the same.

// Declare a tuple type and initialize itlet values: [string, number] = ["Foo", 15];// Type 'BOOLEAN' is not assignable to type 'string'.(2322)// Type 'string' is not assignable to type 'number'.(2322)LET wrongValues: [string, number] = [true, "hello"]; // Error

Since TypeScript 3.0, a tuple can specify one or more optional types using the ? as shown below.

let values: [string, number, boolean?] = ["Foo", 15];


Discussion

No Comment Found