ZodZ
Zod9mo ago
MattIPv4

MattIPv4 - :WaveDoggo: I think this might be a ...

:Wave_Doggo: I think this might be a Zod Q at this point rather than TypeScript itself -- I have a custom type that relies on import() types. I've figured out how to partially preserve them when generating declaration files, but I'm still getting some unknowns included instead of references to the import() types.
Solution
Ahah!

type ImagePng = (typeof import("*.png"))["default"];
type ImageJpg = (typeof import("*.jpg"))["default"];
type ImageJpeg = (typeof import("*.jpeg"))["default"];
type ImageImport = ImagePng | ImageJpg | ImageJpeg;

// Manually set the type of the schema to avoid TS inferring `ImageImport` and `Position`
// We want `ImageImport` to retain its original type using `import` calls
type ZodImageObject = z.ZodObject<{
  src: z.ZodType<ImageImport>;
  alt: z.ZodString;
}>;

export const ambassadorImageSchema: ZodImageObject = z.object({
  src: z.custom<ImageImport>(),
  alt: z.string(),
});


Results in:


import * as __jpeg from '*.jpeg';
import * as __jpg from '*.jpg';
import * as __png from '*.png';

type ImagePng = (typeof __png)["default"];
type ImageJpg = (typeof __jpg)["default"];
type ImageJpeg = (typeof __jpeg)["default"];
type ImageImport = ImagePng | ImageJpg | ImageJpeg;
type ZodImageObject = z.ZodObject<{
    src: z.ZodType<ImageImport>;
    alt: z.ZodString;
    position: z.ZodOptional<z.ZodEffects<z.ZodString, Position>>;
}>;
declare const ambassadorImageSchema: ZodImageObject;
Was this page helpful?