Repository navigation
feat(2.1.0): match, keyword configs, discrimination improvements - #1314
Merged
Merged
Conversation
|
Fantastic work. I've been waiting for arktype to get pattern matching! |
This was
linked to
issues
Feb 23, 2025
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
2.1.0
matchThe
matchfunction provides a powerful way to handle different types of input and return corresponding outputs based on the input type, like a type-safeswitchstatement.Case Record API
The simplest way to define a matcher is with ArkType definition strings as keys with corresponding handlers as values:
In this example,
sizeOfis a matcher that takes a string, array, number, or bigint as input. It returns the length of strings and arrays, and the value of numbers and bigints.defaultaccepts one of 4 values:"assert": acceptunknown, throw if none of the cases match"never": accept an input based on inferred cases, throw if none match"reject": acceptunknown, returnArkErrorsif none of the cases match(data: In) => unknown: handle data not matching other cases directlyCases will be checked in the order they are specified, either as object literal keys or via chained methods.
Fluent API
The
matchfunction also provides a fluent API. This can be convenient for non-string-embeddable definitions:Narrowing input with
in, property matching withatBuiltin keywords can now be globally configured
This can be very helpful for customizing error messages without needing to create your own aliases or wrappers.
The options you can provide here are identical to those used to configure a Type directly, and can also be extended at a type-level to include custom metadata.
Tuple and args expressions for
.toIf a morph returns an
ArkErrorsinstance, validation will fail with that result instead of it being treated as a value. This is especially useful for using other Types as morphs to validate output or chain transformations.To make this easier, there's a special
tooperator that can pipe to a parsed definition without having to wrap it intypeto make it a function.This was added before 2.0, but now it comes with a corresponding operator (
|>) so that it can be expressed via a tuple or args like most other expressions:Error configurations now accept a string directly
Keep in mind, as mentioned in the docs, error configs like
messagecan clobber more granular config options likeexpectedandactualand cannot be included in composite errors e.g. for a union.Though generally, returning a string based on context is the best option, in situations where you always want the same static message, it's now easier to get that!
Type.toString() now wraps its syntactic representation in
Type<..>Previously,
Type.toString()just returnedType.expression. However, in contexts where the source of a message isn't always aType, it could be confusing:Hopefully if you interpolate a Type, you'll be less confused by the result from now on!
Improve how Type instances are inferred when wrapped in external generics
Previously, we used
NoInferin some Type method returns. After migrating those to inlined conditionals, we get the same benefit and external inference for cases like this is more reliable:Fix an issue causing some discriminated unions to incorrectly reject default cases