Interestingly, I am encountering different errors when running the code locally (with zod v3.22.4) compared to on stackblitz using the same version v3.22.4. It seems like both errors may require some fixing.
The stackblitz example, which mirrors the code I have locally, is as follows:
import { z } from 'zod';
type BuildCommandToDecompressWithUnarchiver = {
overwrite?: boolean;
password?: string;
output: {
directory: {
path: string;
};
};
input: {
file: {
path: string;
};
};
};
const BuildCommandToDecompressWithUnarchiverModel: z.ZodType<BuildCommandToDecompressWithUnarchiver> =
z.object({
overwrite: z.optional(z.boolean()).default(false),
password: z.optional(z.string()),
output: z.object({
directory: z.object({
path: z.string(),
}),
}),
input: z.object({
file: z.object({
path: z.string(),
}),
}),
});
export function buildCommandToDecompressWithUnarchiver(source) {
const input = BuildCommandToDecompressWithUnarchiverModel.parse(source);
const cmd = [
`unar`,
`${input.input.file.path}`,
`-o`,
`${input.output.directory.path}`,
];
cmd.push('--quiet');
if (input.overwrite) {
cmd.push(`-f`);
}
if (input.password) {
cmd.push(`-p`, input.password);
}
return cmd;
}
console.log(
BuildCommandToDecompressWithUnarchiverModel.parse({
output: {
directory: {
path: 'foo',
},
},
input: {
file: {
path: 'x.zip',
},
},
})
);
First and foremost, it's worth mentioning that I need
z.ZodType<BuildCommandToDecompressWithUnarchiver>
due to the frequent usage of nested schemas in my definitions. This pattern is essential for successful compilation. Therefore, this structure should remain intact.
However, when testing the code on stackblitz, the error message received is:
Type 'ZodObject<{ overwrite: ZodDefault<ZodOptional<ZodBoolean>>; password: ZodOptional<ZodString>; output: ZodObject<{ directory: ZodObject<{ path: ZodString; }, "strip", ZodTypeAny, { ...; }, { ...; }>; }, "strip", ZodTypeAny, { ...; }, { ...; }>; input: ZodObject<...>; }, "strip", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'ZodType<BuildCommandToDecompressWithUnarchiver, ZodTypeDef, BuildCommandToDecompressWithUnarchiver>'.
The types of '_type.output.directory' are incompatible between these types.
Type '{ path?: string; }' is not assignable to type '{ path: string; }'.
Property 'path' is optional in type '{ path?: string; }' but required in type '{ path: string; }'.(2322)
I am uncertain why the error suggests that 'path' is defined as optional when it isn't specified anywhere in the code. Where could this misconception be arising from?
Conversely, when executing the code locally, a distinct error is observed:
Unsafe argument of type `any` assigned to a parameter of type `string`.eslint@typescript-eslint/no-unsafe-argument
(property) password?: string
https://i.sstatic.net/B1QkB.png
This particular "Unsafe argument of type any
assigned to a parameter of type string
" error recurs in multiple sections across my codebase, all utilizing the same schema definition pattern with zod. Any insights on resolving this issue (and perhaps understanding why stackblitz displays a dissimilar error at times, deviating from the local environment)?
To circumvent the final error, I have resorted to using input.password as string
, although this isn't an ideal solution since the data should already be guaranteed as a string by this stage.