tc39 / proposal-pattern-matching

Pattern matching syntax for ECMAScript
https://tc39.es/proposal-pattern-matching/
MIT License
5.5k stars 89 forks source link

Normative: add `expr is pattern` expression #284

Closed Jack-Works closed 2 years ago

Jack-Works commented 2 years ago

is expression

A new expression is expression cherry picked from the recent discussion about pattern matching.

Syntax

RelationalExpression `is` MatchPattern

This syntax has the same priority as instanceof and in expression.

Semantics

Check if the LHS expression match the RHS pattern, returns a boolean.

It does not introduce new bindings in the current scope.

Note

The MatchPattern and its semantics is unmodified, therefore it have some unintuitive behaviors like:

if (x is y) {
    // always runs, because y is a always success match.
}

if (z is { type }) {
    type // ReferenceError. is expression does not introduce new bindings.
}

Syntax bikeshedding

Maybe expr matches pattern?