-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Any thoughts on making my makeNullablePropsOptional
function simpler to avoid dreaded "Type instantiation is excessively deep and possibly infinite."?
#3905
Comments
What version of Zod are you on? I can't reproduce on the latest version. If you don't have |
Oh. Using this on a basic schema doesn't induce the error. I have a pretty hefty schema that merges a bunch of DAO schemas together and then does the null->optional stuff. That makes TS croak, but if I take off my helper, it's fine. I'm in a monorepo, I use The complexity of the schema is because I have a pretty complex SQL query that joins three tables. Here's what schema that matches the query results looks like. const resultSchema = makeNullablePropsOptional(
selectVesselSchema.omit({ scrapeVersion: true, updated: true, epfd: true }).merge(
selectPositionSchema.omit({ mmsi: true, received: true, accuracy: true }).merge(
selectVoyageSchema.omit({ mmsi: true, received: true, destination: true }).merge(
z.object({
status: navigationStatusSchema.nullable(),
mostRecentPositionReceived: selectPositionSchema.shape.received,
mostRecentVoyageReceived: selectVoyageSchema.shape.received.nullable(),
destination: selectVoyageSchema.shape.destination.nullable(),
month: selectVoyageSchema.shape.month.nullable(),
day: selectVoyageSchema.shape.day.nullable(),
hour: selectVoyageSchema.shape.hour.nullable(),
minute: selectVoyageSchema.shape.minute.nullable(),
}),
),
),
),
).array()
export type GetVesselsWithLatestPositionAndVoyageResult = z.infer<typeof resultSchema> All of those I should say that the error is occurring on a branch that bumps from |
There's virtually no changes between 3.24 and 3.24.1 that could cause this, so it seems probably this is due to multiple Zod versions in node_modules or a VS Code caching issue. If you haven't already, try wiping node_modules and running Reload Window. You might also try making the return type of your utility instead of relying on that "inline" export function makeNullablePropsOptional<TInputSchema extends z.AnyZodObject>(inputSchema: TInputSchema): ZodObject<{
[key in keyof TInputSchema["shape"]]: TInputSchema["shape"][key] extends z.ZodNullable<infer T>
? z.ZodOptional<T>
: TInputSchema["shape"][key]
}> {
...
} |
Thanks for the insights. I'll keep messing around with it. I'm certain that I have only a single Do you think there's any chance that this change in 4e219d6 could have made any impact? I ask because I've tried again to toggle between 3.24.0 and 3.24.1 and I've confirmed that this alone induces the error. |
@colinhacks, I did more digging. At least for me, this single change from declare const objectType: <T extends ZodRawShape>(
shape: T,
params?: RawCreateParams
) => ZodObject<
T,
"strip",
ZodTypeAny,
{
[k in keyof objectUtil.addQuestionMarks<
baseObjectOutputType<T>,
any
>]: objectUtil.addQuestionMarks<baseObjectOutputType<T>, any>[k];
},
{ [k_1 in keyof baseObjectInputType<T>]: baseObjectInputType<T>[k_1] }
>;
declare const objectType: <T extends ZodRawShape>(
shape: T,
params?: RawCreateParams
) => ZodObject<
T,
"strip",
ZodTypeAny,
objectOutputType<T, ZodTypeAny, "strip">,
objectInputType<T, ZodTypeAny, "strip">
>; If I overwrite the I'm traveling the next couple of days, but I'll try to get a minimal repro for you. |
@colinhacks, here's a minimal repro. |
yes, 3.24.1 is causing "Type instantiation is excessively deep and possibly infinite" in our setup. |
I have a helper function
makeNullablePropsOptional
that was inspired by #2050. I use it to turnnull
's coming from my database intoundefined
's within my app.Unfortunately, after bumping
zod
recently, I started flirting with "Type instantiation is excessively deep and possibly infinite." errors. I'm relatively confident that use of this helper function correlates with the error.Does anyone have any insight into how I might simplify the implementation in a way that might appease the TypeScript gods?
The text was updated successfully, but these errors were encountered: