You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now [subpattern] matches arrays with 1 element in them. This is more consistent with native language features, like destructuring assignement and is overall more intuitive. This will resolve #69, #62 and #46.
NaN
The __.NaN pattern has been replaced by simply using the NaN value in the pattern:
match<number>(NaN)
- .with(__.NaN, () => "this is not a number")+ .with(NaN, () => "this is not a number")
.otherwise((n) => n);
⭐️ New features ⭐️
Here is the list of all new features which have been added in TS-Pattern v4.
Arrays and unary tuples
P.array(pattern)
To match an array of elements, you can now use P.array:
P.union(...patterns) and P.intersection(...patterns) combine several patterns into a single one, either by checking that one of them match the input (p.union) or all of them match it (P.intersection).
P.select() now can take a subpattern and match only what the subpattern matches:
typeImg={type: 'img';src: string};typeText={type: 'text';content: string;length: number};typeUser={type: 'user';username: string};typeOrg={type: 'org';orgId: number};constpost=(input: {author: User|Org;content: Text|Img})=>match(input).with({author: P.select({type: 'user'})},// user: User(user)=>{}).with({// This also works with named selectionsauthor: P.select('org',{type: 'org'}),content: P.select('text',{type: 'text'}),},// org: Org, text: Text({ org, text })=>{}).otherwise(()=>{// ...});
Infer the matching types from a pattern
P.infer<typeof pattern>
TS-Pattern is pretty handy for parsing unknown payloads like HTTP responses. You can write a pattern for the shape you are expecting, and then use isMatching(pattern, response) to make sure the response has the correct shape.
One limitation TS-Pattern had in its previous version was that it did not provide a way to get the TypeScript type of the value a given pattern matches. This is what P.infer<typeof pattern> does :)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
✨ TS-Pattern v4 ✨
Imports
type-specific wildcard patterns have moved from
__.<pattern>
to a newPattern
qualified module, also exported asP
by ts-pattern.or
__
The top level
__
export was moved toP._
andP.any
:select()
,not()
,when()
Function to create patterns have been moved to the
P
module.Pattern
typethe
Pattern
type which used to be exported at the toplevel is now accessible atP.Pattern
.list patterns
The syntax for matching on a list of elements with an unknown length has changed from
[subpattern]
toP.array(subpattern)
.Example:
Now
[subpattern]
matches arrays with 1 element in them. This is more consistent with native language features, like destructuring assignement and is overall more intuitive. This will resolve #69, #62 and #46.NaN
The
__.NaN
pattern has been replaced by simply using the NaN value in the pattern:⭐️ New features ⭐️
Here is the list of all new features which have been added in TS-Pattern v4.
Arrays and unary tuples
P.array(pattern)
To match an array of elements, you can now use
P.array
:Optional object properties
P.optional(pattern)
If you want one of the keys of your pattern to be optional, you can now use
P.optional(subpattern)
.If you
P.select()
something in an optional pattern, it's type will be infered asT | undefined
.Union & intersection patterns
P.union(...patterns)
andP.intersection(...patterns)
combine several patterns into a single one, either by checking that one of them match the input (p.union
) or all of them match it (P.intersection
).P.union(...patterns)
P.intersection(...patterns)
Select with sub pattern
P.select()
now can take a subpattern and match only what the subpattern matches:Infer the matching types from a pattern
P.infer<typeof pattern>
TS-Pattern is pretty handy for parsing unknown payloads like HTTP responses. You can write a pattern for the shape you are expecting, and then use
isMatching(pattern, response)
to make sure the response has the correct shape.One limitation TS-Pattern had in its previous version was that it did not provide a way to get the TypeScript type of the value a given pattern matches. This is what
P.infer<typeof pattern>
does :)New type specific wildcards
P.symbol
P.symbol
is a wildcard pattern matching any symbol.P.bigint
P.bigint
is a wildcard pattern matching any bigint.This discussion was created from the release v4.0.1.
Beta Was this translation helpful? Give feedback.
All reactions