From 0ddbda2c78769b9e2cdea8be1d2ba2286c55e0b4 Mon Sep 17 00:00:00 2001 From: Anna Tyrrell <16312226+githesp@users.noreply.github.com> Date: Fri, 15 Feb 2019 05:09:20 +1300 Subject: [PATCH] Add updating and destructuring examples. (#27628) --- guide/english/typescript/tuple-type/index.md | 61 +++++++++++++------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/guide/english/typescript/tuple-type/index.md b/guide/english/typescript/tuple-type/index.md index 5156c0b0d7..6911a3c259 100644 --- a/guide/english/typescript/tuple-type/index.md +++ b/guide/english/typescript/tuple-type/index.md @@ -4,7 +4,7 @@ title: Tuple Type # Tuple Type -Express an array in which a fixed number of elements of types is known, but not the same. +Expresses an array with a fixed number of elements of known (but possibly different) types. For example: ```typescript let arr: [string, number]; @@ -12,54 +12,75 @@ let arr: [string, number]; // This is correct arr = ['Hello', 7]; -//This is incorrect +// This is incorrect arr = [7, 'Hello']; ``` When accessing an element outside the known indices, it will use a union type: ```typescript -arr[3] = 'World!' +arr[3] = 'World!'; // OK, 'string' can be assigned to 'string | number' -// Error, 'boolean' is not a 'string | number' arr[5] = false; // Error, 'boolean' is not a 'string | number' ``` ## Properties -In Typescript's type you can have some buit-in properties. Forexample length or some other unique each types. +Tuples have some built-in properties, such as length. ### length -This property said, how many element has it's element. +The number of elements: ```typescript -let tuple = []; //you can initialize it after the declaration too, not just the method above -tuple[0] = 10; -tuple[1] = 'Mike'; +let tuple = [10, 'Mike']; let number = tuple.length; -//number = 2; +// number = 2 ``` ## Built-in methods -In Typescript's type you can use some built-in functions. Each type has common and unique methods. -Below you can read about the most used ones of the tuple type's methods. +Tuples have some built-in functions, such as adding a new element and removing an element: ### pop() -Removes the last element from a tuple. +Removes the last element from a tuple and returns that element. ```typescript -var tuple = [10,'Emma',11,'Lily',12,'Mike Ross']; -tuple.pop(); -//tuple = [10,'Emma',11,'Lily',12,] -//We popped 'Mike Ross' from the tuple +let tuple = [10,'Emma',11,'Lily',12,'Mike Ross']; +let popped = tuple.pop(); +// tuple = [10,'Emma',11,'Lily', 12] +// popped = 'Mike Ross' ``` ### push() -Adds element to the end of the tuple. +Adds an element to the end of the tuple. ```typescript -var tuple = [10,'Emma',11,'Lily',12,'Mike Ross']; +let tuple = [10,'Emma',11,'Lily',12,'Mike Ross']; tuple.push('Rachel Zane'); -//tuple = [10,'Emma',11,'Lily',12,'Mike Ross','Rachel Zane'] +// tuple = [10,'Emma',11,'Lily',12,'Mike Ross','Rachel Zane'] +``` + +## Destructuring +The following is an easy way to break apart a tuple in TypeScript: +```typescript +let tuple = ['sheep', 4]; +[a, b] = tuple; +// a = 'sheep' +// b = 4 +``` + +## Updating +You can initialize the tuple after the declaration: +```typescript +let tuple = []; +tuple[0] = 'cow'; +tuple[1] = 'Mike'; +// tuple = ['cow', 'Mike'] +``` + +You can update an existing tuple in the same way you update an array: +```typescript +let tuple = ['sheep', 4]; +tuple[0] = 'cow'; +// tuple = ['cow', 4] ``` [More info about tuples on TutorialsPoint](https://www.tutorialspoint.com/typescript/typescript_tuples.htm)