Generic Zod Type without defining generics?

I'm trying to type this correctyl in that paramSchema and querySchema are basically inferred by the given schema when defined, but I want to also provide the correct type for everything else in the object. Not sure how to achieve that without just foregoing defining the type. Are there any other ways to do that? Posting my example in the comments
1 Reply
Pertinate
PertinateOP14mo ago
type Methods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
type RouteType = 'client' | 'server';

interface Route<T extends RouteType> {
type: T;
path: T extends 'client' ? ClientPath : ServerPath;
method: T extends 'client' ? 'GET' : Methods;
}

export type ClientPath = '/page' | '/:user/:productId';

type ClientRoute = Route<'client'> & {
schema?: {
paramSchema?: z.Schema;
querySchema?: z.Schema;
};
};
export const clientRoutes: readonly ClientRoute[] = [
{
type: 'client',
path: '/:user/:productId',
method: 'GET',
schema: {
paramSchema: z.record(z.union([z.literal('user'), z.literal('productId')]), z.string())
}
},
{
type: 'client',
path: '/page',
method: 'GET',
},
] as const;

const te = clientRoutes[0].schema?.paramSchema?.parse({})
type Methods = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
type RouteType = 'client' | 'server';

interface Route<T extends RouteType> {
type: T;
path: T extends 'client' ? ClientPath : ServerPath;
method: T extends 'client' ? 'GET' : Methods;
}

export type ClientPath = '/page' | '/:user/:productId';

type ClientRoute = Route<'client'> & {
schema?: {
paramSchema?: z.Schema;
querySchema?: z.Schema;
};
};
export const clientRoutes: readonly ClientRoute[] = [
{
type: 'client',
path: '/:user/:productId',
method: 'GET',
schema: {
paramSchema: z.record(z.union([z.literal('user'), z.literal('productId')]), z.string())
}
},
{
type: 'client',
path: '/page',
method: 'GET',
},
] as const;

const te = clientRoutes[0].schema?.paramSchema?.parse({})
at the bottom, te doesn't know the given return for parse
Want results from more Discord servers?
Add your server