-
-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Investigate typed match expression #802
Comments
Messed around with this a bit. Got completions working but only in expressions, and wasn't able to actually get the function inputs to infer. Likely will require similar workarounds to morph/narrow. type validateCases<cases, $> = {
-readonly [k in keyof cases as validateDefinition<k, $, {}>]: (
In: inferTypeRoot<k, $>
) => unknown
}
export type MatchParser<$> = {
<const cases>(def: conform<cases, validateCases<cases, $>>): Type<cases, $>
}
declare const match: MatchParser<Ark>
const sizeOf = match({
number: (n) => n,
"string|unknown[]": (data) => data.length
}) |
Updated the above example to provide the best completions possible while allowing inference (completions with an expression doesn't seem possible in keys right now): type validateCases<cases, $> = {
// adding keyof $ explicitly provides key completions for aliases
[k in keyof cases | keyof $]?: k extends validateTypeRoot<k, $>
? (In: inferTypeRoot<k, $>) => unknown
: never
}
export type MatchParser<$> = {
<cases>(
def: conform<cases, validateCases<cases, $>>
): Type<
(In: inferTypeRoot<keyof cases, $>) => Out<returnOf<cases[keyof cases]>>,
$
>
}
export declare const match: MatchParser<Ark> |
25 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I often hear about devs using other languages missing match expressions. I'd need to do some investigation around common syntax and use cases, but I could imagine a thin layer around AT providing type-safe and configurable matching. Something like:
Here's an example of how it could be used with a scope:
Could use entry syntax for non string serializable types like objects and custom validators, and combine with transformations. It wouldn't really involve any new logic, the types would be very efficient and fully autocompleted. You could even define your own validation keywords and easily use them in your matcher keys.
It could also reuse all the type reduction + auto-discrimination functionality to error on impossible to hit cases and determine if any branches are a match in O(1) time for most unions.
The text was updated successfully, but these errors were encountered: