Extractors S2
- Stage: Stage 2
- Status: Active
- ECMAScript edition: —
- Synchronized: Aug 28, 2026
- 中文译文 · Source repository
This proposal introduces Extractors to ECMAScript, extending destructuring syntax with custom user-defined logic for validation and transformation. It leverages the Symbol.customMatcher symbol from the Pattern Matching proposal, allowing patterns like const Foo(y) = x to invoke user code.
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
Extractors for ECMAScript
A proposal to introduce Extractors (a.k.a. "Extractor Objects") to ECMAScript.
Extractors would augment the syntax for BindingPattern and AssignmentPattern to allow for new destructuring forms, as in the following example:
In addition, this would leverage the new Symbol.customMatcher built-in symbol added by the Pattern Matching proposal. When
destructuring using the new form, the Symbol.customMatcher method would be called and its result would be destructured instead.
Status
Stage: 1
Champion: Ron Buckton (@rbuckton)
For more information see the TC39 proposal process.
Authors
- Ron Buckton (@rbuckton)
Motivations
ECMAScript currently has no mechanism for executing user-defined logic during destructuring, which means that operations related to data validation and transformation may require multiple statements:
With Extractors, such validation and transformation logic can be encapsulated and reused inside of the binding pattern:
This would also be extremely useful when paired with a forthcoming enum proposal with support for Algebraic Data Types (ADT):
Proposed Solution
Extractors are loosely based on Scala's Extractor Objects and Rust's Pattern Matching. Extractors extend the syntax for BindingPattern and AssignmentPattern to allow for the evaluation of user-defined logic for validation and transformation.
Extractors perform array destructuring on a successful match result, starting with a reference to a value in scope
using an ExtractorMemberExpression, which is essentially an IdentifierReference (i.e., Point, InstantExtractor,
etc.) or a dotted-name (i.e., Option.Some, Message.Move, etc.).
When an Extractor is evaluated during destructuring, its ExtractorMemberExpression is evaluated, and that evaluated
result's [Symbol.customMatcher]() method is invoked with the current value to be destructured, returning an iterable
object that indicates the match succeeded, and the extracted elements to use for further destructuring. For the purpose
of destructuring, any other value will produce a TypeError. In the case of pattern matching, true and false are
also valid return values.
An Extractor consists of an ExtractorMemberExpression followed by a parenthesized list of additional destructuring patterns:
Parentheses (()) are used instead of square brackets ([]) for several reasons:
- Avoids collisions with a ElementAccessExpression when the extractor is part of an assignment pattern:
- Ensures a consistent syntax between binding and assignment patterns:
- Allows destructuring (and pattern matching) to mirror construction/application:
Object Destructuring using Extractors
Extractors do not introduce a novel syntax for object extraction. Instead, an Extractor can return a single element match result containing the object to be further destructured:
Iterable Wrapper Overhead
It's important to note that this has the overhead of allocating an iterable wrapper object to perform further destructuring. If necessary, this overhead could be removed if we consider an alternative representation for a match result that indicates result is the sole extracted value, i.e.:
Future Object Extractor Syntax
It is possible that a future property-literal construction syntax that might be used by Algebraic Data Types or other constructors, i.e.:
However, such a syntax is currently out of scope for this proposal. In addition, introducing an Identifier { syntax
for extractors has the high likelihood of carving off too much syntax space that could be used by other proposals. As
a result, an "Object Extractor" like syntax like const Point{ x, y } = p is not being considered part of this proposal
at this time.
Prior Art
Related Proposals
- Collection Literals (Withdrawn)
- Pattern Matching (Stage 1)
- Enums and Algebraic Data Types (Stage 0, not yet accepted)
Examples
The examples in this section use a desugaring to explain the underlying semantics, given the following helper:
const C(x) = subject
Given,
The statement
is approximately the same as the transposed representation
such that x results in the value "data".
const C(x, y) = subject
Given,
The statement
is approximately the same as the transposed representation
such that x and y result in the values 1 and 2, respectively.
const C(x, ...y) = subject
Given,
The statement
is approximately the same as the transposed representation
such that x and y result in the values 1 and [2, 3], respectively.
const C(x = -1, y) = subject
Given,
The statement
is approximately the same as the transposed representation
such that x and y result in the values -1 and 2, respectively.
const C({ x }) = subject
Given,
The statement
is approximately the same as the transposed representation
such that x and y have the values 1 and 2, respectively.
const C(D(x)) = subject
Given,
The statement
is approximately the same as the transposed representation
such that x results in the value "data".
Custom Logic During Destructuring
Given,
The statement
is approximately the same as the transposed representation
such that a and b result in the values 1 and 2, respectively.
Regular Expressions
Receivers: const obj.extractor(x) = subject
When the ExtractorMemberExpression results in a Reference, the receiver is preserved and passed on to the custom matcher:
Given,
The statement
is approximately the same as the transposed representation
such that x results in the value "DATA".
Potential Grammar
For the proposed grammar, see the specification text.
Potential Semantics
For the proposed semantics, see the specification text.
API
This proposal would adopt (and continue to align with) the behavior of Custom Matchers from the Pattern Matching proposal:
- A Custom Matcher is a regular ECMAScript Object value with a
[Symbol.customMatcher]method that accepts a three arguments:subject(the value to match against),hint(either"boolean"or"list"), andreceiver, and returns either a Boolean or an Iterable, depending on the value ofhint. - When
hintis"boolean", the return value will be coerced to a Boolean via the ToBoolean() abstract operation. Whenhintis"list", the return value value must either be an Iterable object or a falsy value (i.e.,false,0,null,undefined, etc.).- In Pattern Matching, a
hintof"boolean"can be used to avoid the costly allocation of an Iterable object when the return value won't be used for further matching (such as withx is C), while ahintof"list"indicates further matching will be performed on the result (such as withx is C(1, 2)). - For destructuring and binding patterns (e.g., this proposal),
hintwill always be"list".
- In Pattern Matching, a
Relation to Pattern Matching
We believe that Extractors would also be extremely valuable as part of the Pattern Matching Proposal, and intend to discuss adoption with the champions should this proposal be adopted.
Extractors could easily be added to MatchPattern using the same syntax as proposed for destructuring, which would allow for more concise and potentially more readily understood code:
This is even more evident with respect to complex, nested patterns:
Relation to Enums and Algebraic Data Types
We strongly believe that ECMAScript will eventually adopt some form of the current enum proposal, given the particular value
that Algebraic Data Types could provide. The Enum proposal would strongly favor consistent and coherent syntax between
declaration, construction, destructuring, and pattern matching, as in the following example:
Here, declaration, construction, destructuring, and pattern matching are consistent for ADT enum members and values:
As noted before, it is possible that a future property-literal construction syntax that might be used by Algebraic Data Types or other constructors. Adding a matching syntax for object extraction would be the responsibility of that proposal and is out of scope for this proposal.
TODO
The following is a high-level list of tasks to progress through each stage of the TC39 proposal process:
Stage 1 Entrance Criteria
- Identified a "champion" who will advance the addition.
- Prose outlining the problem or need and the general shape of a solution.
- Illustrative examples of usage.
- High-level API.
Stage 2 Entrance Criteria
- Initial specification text.
- Transpiler support (Optional).
Stage 3 Entrance Criteria
- Complete specification text.
- Designated reviewers have signed off on the current spec text.
- The ECMAScript editor has signed off on the current spec text.
Stage 4 Entrance Criteria
- Test262 acceptance tests have been written for mainline usage scenarios and merged.
- Two compatible implementations which pass the acceptance tests: [1], [2].
- A pull request has been sent to tc39/ecma262 with the integrated spec text.
- The ECMAScript editor has signed off on the pull request.