Using an ArkType Generic within TS types.
How can I convert an ArkType generic to a TS generic? Something like:
In Zod I would do something like this:
I think I've missed a
ZodType
or two somewhere in there, but thats the general idea. I have dont it before.
What would be the ArkType equivalent? Thanks!5 Replies
Does
(typeof t)["infer"]
not work?No,
infer
doesn't exist on generics :/
Yeah, not sure I'm afraid....
Looking through the unit tests there's
(typeof t).t
, but I don't think that's what you want
https://github.com/arktypeio/arktype/blob/main/ark/type/__tests__/generic.test.tsGitHub
arktype/ark/type/tests/generic.test.ts at main · arktypeio/ark...
TypeScript's 1:1 validator, optimized from editor to runtime - arktypeio/arktype
This is inherently impossible to do directly with native arktype generics.
You can also use generics in ArkType the same way you'd use them in Zod by just wrapping a standard TS generic. There are some examples of that here:
https://arktype.io/docs/generics#external
Another option would be to use an HKT. Though this is a bit more advanced and depends on the details of your use case, these are introspectable via both ArkType and TS:
https://arktype.io/docs/generics#hkt
thanks for the info @ArkDavid ! i think replicating the zod approach will suffice, but good to know theres an option for fully introspectable ones as well.