Comments (4)
you should use narrowed instances to benefit from pattern matching.
match(object)
.with({type:"variant1"}, (object) => {
console.log(object.data.someField);
console.log(object.flavor);
})
.with({type:"variant2"}, (object) => {
console.log(object.data.length);
console.log(object.kind);
})
.exhaustive();
};
from ts-pattern.
Thanks guys! I had no idea I was supposed to be matching on the whole object. It's interesting that the native TS switch
will do this exactly as I hoped 🤔
Anyway, I am unblocked on this 👍🏻
Really interested in the TS 5.5 predicate inference. Seems like that would allow match()
to behave a lot more like the native switch
that I gave in my sandbox.
from ts-pattern.
Yes, as @JUSTIVE showed, you need to match on the whole object and use the narrowed reference that's provided as a parameter to handler functions.
I wish there was a way to narrow an object that's in scope based on the pattern given to .with, but it isn't possible today.
from ts-pattern.
maybe there's some point where we could benefit from typescript 5.5's type predicate inference. not sure how we'll benefit from it, but since it automatically narrows type, I'm pretty sure there's some way.
from ts-pattern.
Related Issues (20)
- Support point-free style HOT 3
- Select sub-type for exhaustive check. E.g. `.exhaustive(([state, event]) => event.type)`
- Handling of undefined typings in tuples HOT 1
- a way to split long match blocks
- Predicate factory HOT 3
- .safeExhaustive HOT 1
- Deconstruct string matched with `.startsWith`, `.endsWith`, `regex` HOT 2
- How to check if a key of unknown type exists on an object? HOT 3
- Enter multiple pipeline for objects type match HOT 1
- Template literal matching support HOT 2
- There are cases where unwanted patterns match. HOT 8
- Support P.object.empty HOT 1
- `with(P.not([])` with readonly array
- Example playground doesn't open HOT 1
- P.nonNullable behaves differently than P.not(P.nullish) HOT 2
- Feature: return the value directly HOT 2
- `Pattern.nonNullable` does not behave as expected HOT 2
- P.string.length
- Object's unions with unions in keys require us to check for patterns that won't exist
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-pattern.