└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # ECMAScript As-Patterns for Matching and Destructuring 2 | 3 | ## [Status](https://tc39.github.io/process-document/) 4 | 5 | **Stage**: 0 6 | 7 | **Author**: Kat Marchán (npm, [@maybekatz](https://twitter.com/maybekatz)) 8 | 9 | **Champions**: Kat Marchán (npm, [@maybekatz](https://twitter.com/maybekatz)) 10 | 11 | ## Introduction 12 | 13 | When matching non-Identifier values, it's often the case that users might want 14 | to also bind that value to an Identifier while doing the matching. For this 15 | reason, it's proposed that destructuring be extended with the ability to do 16 | this sort of binding. Furthermore, the separate [pattern matching 17 | proposal](https://github.com/tc39/proposal-pattern-matching) will benefit from 18 | this change by allowing matching operations against values that are also put 19 | into identifiers, since identifiers are irrefutable patterns. 20 | 21 | The syntax uses an `as` keyword, and looks as follows: 22 | 23 | ```js 24 | const {x: {y} as x} = {x: {y: 1}} 25 | // x is {y: 1} 26 | // y is 1 27 | ``` 28 | 29 | Or: 30 | ```js 31 | function foo ([{y} as x, [z] as zed = [1]]) { 32 | // x is {y: ...} 33 | // y is x.y 34 | // z is runs an initializer if arguments[0][1] is undefined 35 | } 36 | ``` 37 | 38 | This applies similarly to `match`: 39 | ```js 40 | match (x) { 41 | when {x: {y: 1} as x} ~> console.log(x.y === 1) 42 | } 43 | ``` 44 | 45 | Note: This syntax is [used by 46 | F#](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/pattern-matching). 47 | It's also reminiscent of `as` syntax in `import` statements, so there's some 48 | precedent in the language for this sort of binding (`import * as bar from 49 | './x.js'`) 50 | 51 | ## The Big Picture 52 | 53 | ### Related Active Proposals 54 | 55 | * [Pattern matching](https://github.com/tc39/proposal-pattern-matching) 56 | * [Collection literals](https://github.com/zkat/proposal-collection-literals) 57 | 58 | ## As-Patterns 59 | 60 | ### Syntax 61 | 62 | **12.15.5 Destructuring Assignment Changes**: 63 | ``` 64 | AssignmentRebinding : 65 | `as` IdentifierReference 66 | 67 | AssignmentElement : 68 | DestructuringAssignmentTarget 69 | DestructuringAssignmentTarget AssignmentRebinding 70 | DestructuringAssignmentTarget Initializer 71 | DestructuringAssignmentTarget AssignmentRebinding Initializer 72 | ``` 73 | 74 | **13.3.3 Destructuring Binding Patterns Changes**: 75 | ``` 76 | BindingRebinding : 77 | `as` IdentifierReference 78 | 79 | BindingElement : 80 | SingleNameBinding 81 | BindingPattern 82 | BindingPattern BindingRebinding 83 | BindingPattern Initializer 84 | BindingPattern BindingRebinding Initializer 85 | ``` 86 | 87 | **[Match Operator](https://github.com/tc39/proposal-pattern-matching) Syntax Changes**: 88 | ``` 89 | MatchRebinding : 90 | `as` IdentifierReference 91 | 92 | MatchElement : 93 | SingleNameBinding 94 | MatchPattern 95 | MatchPattern MatchRebinding 96 | MatchPattern Initializer 97 | MatchPattern MatchRebinding Initializer 98 | ``` 99 | --------------------------------------------------------------------------------