MattIPv4
MattIPv4
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
So I wasn't far off with what I had before manually setting the type for just the src, just needed to set the type manually for the entire schema
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
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(),
});
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;
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;
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
But really, it could be anything, hence wanting to have this use those import()s correctly so that it'll just use whatever type the user of the library has setup in their module augmentations
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
One of our usages uses plain Webpack and returns a string for the file path, the other is Next.js which'll return an object with the file path, width, height, etc.
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
What that bundler is and what it returns for those imports is up to the user of this library
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
And so a bundler of some type is going to process them
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
They are images being imported
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
These aren't JSON, I'm not sure where JSON comes into this at all?
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
Fwiw, the workaround is just to do z.infer<typeof ambassadorImageSchema> & { src: ImageImport };, but I'd really rather z.infer just work correctly
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
Writing custom CI/CD to implement my own version of type-checking sounds like an incredibly complex solution, unless I'm not following
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
The objects are a known type we set via module augmentation
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
Hence wanting the type to propagate through from the imports correctly, so the end type is correct where we're using it
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
For one of our usages they'll be URLs, for the other they'll be objects
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
Yah, the schema works, but if you try to z.infer from it, it doesn't work (updated my example link as I'd pasted one w/o the infer)
26 replies
ZZod
Created by MattIPv4 on 4/14/2025 in #questions
MattIPv4 - :WaveDoggo: I think this might be a ...
26 replies