A match should narrow based on the implications of the thing being matched against.
declare const value: { x: 1 | 2 | "foo" } | { y: false }
match (value) {
// 'value' should be narrowed to '{ x: 1 | 2 | "foo" }'
// Accessing 'value.x' should give a narrowed '1 | 2'
when ({ x: Number }): value;
}
Pattern Matching
[[Overview of current pattern matching proposal.]]
Looks roughlylike
All RHSs are expressions, not statements. If you need statements, you need a
doexpression which doesn't exist yet.What patterns are there?
andPatternorPattern${Expression}InfinityNaNundefined0,"some string",null,RegularExpressionLiteral, andTemplateLiteral.match ([1]) { when ([1, undefined]) ...- does this match?const [x, y] = [1]One can use a value with a custom matcher within an
${interpolation pattern}.Symbol.matcherto return the things that you want to allow a user to bind/destructure after the pattern match.withkeyword that allows you to bind against that value.withdestructuring.Control-flow analysis implications for TypeScript
A match should narrow based on the implications of the thing being matched against.
One counter-proposal around "epics"
Break down functionality into smaller pieces, make things more teachable.
Matching becomes a single primitive that can be brought into several constructs, including maybe a new
matchexpression.let when, etc.{}.Integration with control flow expressions
Then
matchexpressions would invert the difference between matching and binding.Extractor Objects Addition
apply/unapplyif you're familiar, but not crucial.Epics + Extractor Objects
isexpressionisMatchPatternisintegration with patterns if you're familiar with it, but not important.Allows you to match and bind.
Allows you to create a local from expression position.
Like the proposal direction.
let ... insort of syntax.Wait, what does
${and}from the original proposal mean again?${Foo}would say "the thing inside of the curlies (Foo) is a matcher" because otherwise you'd create a variable namedFoo