fix(deps): update dependency ts-pattern to v4 #233
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR contains the following updates:
4.2.1
->4.3.0
Release Notes
gvergnaud/ts-pattern (ts-pattern)
v4.3.0
Compare Source
TS-Pattern and node16
TS-Pattern now fully supports
moduleResolution: node16
, with both ES and CommonJS modules. This resolves the long standing issue number #110. Special thanks to @Andarist and @frankie303 for helping me understand and fix this issue ❤️What's Changed
Full Changelog: gvergnaud/ts-pattern@v4.2.2...v4.3.0
v4.2.3
Compare Source
v4.2.2
Compare Source
Bug fixes:
v4.2.1
Compare Source
Bug fixes
This release fixes inference of
P.array
when the input is a readonly array (issue #148)v4.2.0
Compare Source
Features
Better inference for
match
and.with
match
When using match with an inline array, it will now infer its type as tuple automatically, even when not using
as const
. This means that exhaustiveness checking will also improve in this case:.with(...)
Thanks to the help of @Andarist, this release fixes a long-standing issue of
.with
.Until now, patterns like
P.array
,P.union
orP.when
didn't have proper type inference when used in.with()
directly. Here are a few behaviors that use to be incorrect and work now:This also fixes the following issue: https://github.com/gvergnaud/ts-pattern/issues/140
v4.1.4
Compare Source
Bug fixes
Issue https://github.com/gvergnaud/ts-pattern/issues/138 — inference issues with
P.not
When using
P.not
with a literal value likeP.not(2)
, Exhaustive checking was mistakenly considering that all numbers had been handled, even though2
isn't. This was causing this code to erroneously type-check:This new patch version fixes this bug.
Caveats
Exhaustive pattern-matching expressions where the input is a primitive (like
number
) and the pattern is a negation of a literal number (likeP.not(2)
) are no longer considered exhaustive:Technically, this expression is exhaustive but there is no easy way to type-check it is without negated types (https://github.com/microsoft/TypeScript/pull/29317), so this is an expected false-positive for now.
Exhaustive checking works as expected when the pattern and the input are primitive types:
And when the pattern and the input are literal types:
v4.1.3
Compare Source
v4.1.2
Compare Source
Make subsequent
.with
clause inherit narrowing from previous clausesProblem
With the current version of ts-pattern, nothing prevents you from writing
.with
clauses that will never match any input because the case has already been handled in a previous close:Approach
Initially, I was reluctant to narrow the input type on every call of
.with
because of type checking performance. TS-Pattern's exhaustive checking is pretty expensive because it not only narrows top-level union types, but also nested ones. In order to make that work, TS-Pattern needs to distribute nested union types when they are matched by a pattern, which can sometimes generate large unions which are more expensive to match.I ended up settling on a more modest approach, which turns out to have great performance: Only narrowing top level union types. This should cover 80% of cases, including the aforementioned one:
Examples of invalid cases that no longer type check:
Narrowing will work on unions of literals, but also discriminated unions of objects:
It also works with tuples, and any other union of data structures:
It works with any patterns, including wildcards:
Examples of invalid cases that still type check:
This won't prevent you from writing duplicated clauses in case the union you're matching is nested:
.otherwise
's input also inherit narrowingThe nice effect of refining the input value on every
.with
clause is that.otherwise
also get a narrowed input type:Perf
Type-checking performance is generally better, with a 29% reduction of type instantiation and a 17% check time improvement on my benchmark:
3810512
2670937
Other changes
package.json
exports have been updated to provide a default export for build systems that read neitherimport
norrequire
.v4.1.1
Compare Source
v4.1.0
Compare Source
v4.0.6
Compare Source
Bug fixes
P.instanceOf
to accept not only classes but also abstract classes. Related issue, gvergnaud/ts-pattern@000927c gvergnaud/ts-pattern@ebeb39bv4.0.5
Compare Source
This release adds the ./package.json file to exported files (PR by @zoontek).
v4.0.4
Compare Source
Fixes:
P.array()
andP.select()
, the handler function used to receiveundefined
instead of an empty array when the input array was empty. Now it received an empty array as expected:[]
) when matching on a value of typeunknown
. This has been fixed.Commits:
v4.0.3
Compare Source
v4.0.2
Compare Source
Patch release containing a few runtime performance improvements:
Builder
class internally in match expression to rely on prototypal inheritance instead of defining method every time.with
is called..with(pattern, handler)
case separately from.with(...pattern, handler)
to avoid iterating on params and make it faster.v4.0.1
: ✨ v4.0.1 ✨Compare Source
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.v3.3.5
Compare Source
Bug fixes
This fixes a type inference bug impacting handler functions with explicit type annotations.
It used to be possible to annotate the handler function with an invalid type annotation. Thanks to this commit, it no longer type-checks gvergnaud/ts-pattern@2d75074.
See the related issue for more details: #73
v3.3.4
Compare Source
Bug fixes
This release fixes a type inference bug specific to Error sub classes. See the related issue for more details: https://github.com/gvergnaud/ts-pattern/issues/63
v3.3.3
Compare Source
v3.3.2
Compare Source
This patch contains some compile time perf improvements.
@ahejlsberg recently implemented tail call elimination for recursive conditional types (https://github.com/microsoft/TypeScript/pull/45711). This release is preparation work to take advantage of this new feature by making most type helper functions tail recursive. From the non scientific tests I made on my machine, this also improves the compilation time of the
tests/
folder quite significantly on our current TS version (4.4). Compilation is ~ 20% faster.v3.3.1
Compare Source
Features
Add a
__.NaN
pattern, matching onlyNaN
s values. Thanks @mhintz for adding thisBugfix
Update the
__.number
pattern to also match onNaN
values.Since
NaN
has typenumber
in TypeScript, there is no way to distinguish aNaN
from a regular number at the type level. This was causing an issue where.exhaustive()
considered all numbers handled by the__.number
pattern even thoughNaN
wasn't matched by it, resulting in possible runtime errors.v3.2.5
Compare Source
Bugfixes
Configuration
📅 Schedule: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).
🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.
♻ Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.
🔕 Ignore: Close this PR and you won't be reminded about this update again.
This PR was generated by Mend Renovate. View the repository job log.