How to change the output type?

24 views Asked by At

e.g. we use a Flavor type UserId

interface Flavoring<FlavorT> {
  _type?: FlavorT;
}
type Flavor<T, FlavorT> = T & Flavoring<FlavorT>;

type UserId = Flavor<string, 'UserId'>;

and want the zod-type to use be UserId instead of string.

Using refine seems to work, but I hope, that there is a better way that can avoid the function call:

const MySchema = z.object({
  id: z.string().refine((v): v is UserId => true),
});

type MySchemaType = z.infer<typeof MySchema>;

// type MySchemaType = {
//    id: string & Flavoring<"UserId">;
// }

Stackblitz example

0

There are 0 answers