Manually declare output type for a validation
Hello - I'm working on a library that emits types inferred from arktype validations.
I want the inferred output type to use a named type (which i also export), instead of the literal inferred value of that type.
So:
Given the following code
I get the following output (built with tsdown):
Which makes complete sense, but wondering if theres something like Drizzle ORM's.
.$type<Fruit>
to get this behavior.9 Replies
There's
type(...).as<Type>()
.
E.g.🤯
Hmm, wish I didn't have to do it on the containing object since I still want inference for everything else on the object.
Doing just this doesn't work:
What exactly doesn't work about that?
yields
You shouldn't need to repeat the
.as
in the FoodSchema
Try that and see what happensYeah I tried that too, no luck
Yeah, not sure then, sorry
All good!
In general I've found it's a lot easier to get TS to avoid expanding objects than primitive unions.
You could also try something like this:
This avoids having to cast (this will enforce the expected inference).
Locally though, I still see the literal union when hovering the type (not sure what happens with your build tooling in .d.ts).
If you have a way to reliably stop TS from expanding a string literal union like this though would definitely be interested