Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Demonstrate property renaming and default values together while destructuring #1074

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pages/Variable Declarations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down