From 1e753df087f2fe521099d63f8fc6c802ebefb067 Mon Sep 17 00:00:00 2001 From: Muhammad Saqib Date: Fri, 19 Jul 2019 12:21:42 +0500 Subject: [PATCH] Add another example Property renaming and default values are demonstrated separately while destructuring, but they are not demoed together, this newly added example does the job. --- pages/Variable Declarations.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pages/Variable Declarations.md b/pages/Variable Declarations.md index 1f26f9542..cd7b6433f 100644 --- a/pages/Variable Declarations.md +++ b/pages/Variable Declarations.md @@ -597,6 +597,18 @@ function keepWholeObject(wholeObject: { a: string, b?: number }) { In this example the `b?` indicates that `b` is optional, so it may be `undefined`. `keepWholeObject` now has a variable for `wholeObject` as well as the properties `a` and `b`, even if `b` is undefined. +### Default values with renamed destructured properties + +This is how we can use property renaming and default values together: + +```ts +function keepWholeObject(wholeObject: { a: string, b?: number }) { + let { a: foo, b: bar = 1001 } = wholeObject; +} +``` + +`keepWholeObject` now has a variable for `wholeObject` as well as properties `foo` and `bar`, accessing `a` and `b` will result in an error. + ## Function declarations Destructuring also works in function declarations.