filmov
tv
TypeScript Tutorials #18 - Tuples

Показать описание
#TypeScript #Tuple
For programs to be useful, we need to be able to work with some of the simplest units of data: numbers, strings, structures, boolean values, and the like. In TypeScript, we support the same types as you would expect in JavaScript, with an extra enumeration type thrown in to help things along.
Tuple types allow you to express an array with a fixed number of elements whose types are known, but need not be the same. We can access tuple elements using the index, the same way as an array. An index starts from zero. You can add new elements to a tuple using the push() method. For Example:-
// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10]; // OK
// Initialize it incorrectly
x = [10, "hello"]; // Error
Error Message:-
Type 'number' is not assignable to type 'string'.
Type 'string' is not assignable to type 'number'.
The tuple is like an array. So, we can use array methods on tuple such as pop(), concat() etc.
✅ Let's connect:
Twitter - @AnshulLaddha3
Instagram - /anshul_laddha
For programs to be useful, we need to be able to work with some of the simplest units of data: numbers, strings, structures, boolean values, and the like. In TypeScript, we support the same types as you would expect in JavaScript, with an extra enumeration type thrown in to help things along.
Tuple types allow you to express an array with a fixed number of elements whose types are known, but need not be the same. We can access tuple elements using the index, the same way as an array. An index starts from zero. You can add new elements to a tuple using the push() method. For Example:-
// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10]; // OK
// Initialize it incorrectly
x = [10, "hello"]; // Error
Error Message:-
Type 'number' is not assignable to type 'string'.
Type 'string' is not assignable to type 'number'.
The tuple is like an array. So, we can use array methods on tuple such as pop(), concat() etc.
✅ Let's connect:
Twitter - @AnshulLaddha3
Instagram - /anshul_laddha