Using an ArkType Generic within TS types.

How can I convert an ArkType generic to a TS generic? Something like:
export const SuccessResponse = type("<T>", { data: "T" });
export type SuccessResponse<T> = ??? // Goal: { data: T }
export const SuccessResponse = type("<T>", { data: "T" });
export type SuccessResponse<T> = ??? // Goal: { data: T }
In Zod I would do something like this:
export const SuccessResponse = <T extends AnyZodType>(data: T) => z.object({ data })
export type SuccessResponse<T> = z.infer<ReturnType<typeof SuccessResponse<T>>>
export const SuccessResponse = <T extends AnyZodType>(data: T) => z.object({ data })
export type SuccessResponse<T> = z.infer<ReturnType<typeof SuccessResponse<T>>>
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
TizzySaurus
TizzySaurus4w ago
Does (typeof t)["infer"] not work?
HamishWHC
HamishWHCOP4w ago
No, infer doesn't exist on generics :/
No description
TizzySaurus
TizzySaurus4w ago
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.ts
GitHub
arktype/ark/type/tests/generic.test.ts at main · arktypeio/ark...
TypeScript's 1:1 validator, optimized from editor to runtime - arktypeio/arktype
ssalbdivad
ssalbdivad3w ago
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
ArkType
ArkType Docs
TypeScript's 1:1 validator, optimized from editor to runtime
HamishWHC
HamishWHCOP3w ago
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.

Did you find this page helpful?