Cannot read properties of undefined (reading '_parse')
See original GitHub issueHey, I’ve come across this issue in my project while composing schemas from imported zod objects/enums.Here is the code:
CountryCode.ts
import { z } from 'zod';
const countryCode = z.enum(['IE', 'GB']);
type CountryCode = z.infer<typeof countryCode>;
const countrySchema = z.object({
alpha2: z.string(),
alpha3: z.string(),
code: countryCode,
name: z.string(),
region: z.string(),
regionCode: z.number(),
subRegion: z.string(),
subRegionCode: z.number(),
});
type Country = z.infer<typeof countrySchema>;
export { countryCode, countrySchema };
export type { Country, CountryCode };
CurrencyCode.ts
import { z } from 'zod';
const currencyCode = z.enum(['EUR', 'GBP']);
type CurrencyCode = z.infer<typeof currencyCode>;
const currencySchema = z.object({
code: currencyCode,
name: z.string(),
minorUnit: z.number(),
});
type Currency = z.infer<typeof currencySchema>;
export { currencyCode, currencySchema };
export type { CurrencyCode, Currency };
If i import the above like so: invitePerson.ts
import { z } from 'zod';
import { countryCode, currencyCode } from '../../api';
const invitePersonSchema = z.object({
startDate: z.date().optional(),
firstName: z.string().min(1, 'First name is required'),
lastName: z.string().min(1, 'Last name is required'),
role: z.string().optional(),
dateOfBirth: z.date().optional(),
email: z.string().email('Email is required'),
residentialAddress: z.object({
addressLine1: z.string().optional(),
addressLine2: z.string().optional(),
addressLine3: z.string().optional(),
city: z.string().optional(),
countryCode: countryCode,
postalCode: z.string().optional(),
}),
annualSalary: z
.string()
.min(1, 'Annual salary is required')
.transform(value => parseInt(value, 10)),
salaryCurrency: currencyCode,
taxId: z.string().optional(),
});
type InvitePersonData = z.infer<typeof invitePersonSchema>;
export { invitePersonSchema };
export type { InvitePersonData };
I get the error in the title. However, if I define the enums directly in the invitePersonSchema
and don’t import, there is no error. I would have expected this to be allowed, so please forgive me if I am misusing the library in some way.
Note: this was already discussed here, but the last comment provides a reproducible example which I have copied below, just in case it helps: https://github.com/colinhacks/zod/issues/643
I ran into the same error message and found this issue. Now while my problem is likely a different another one, I thought sharing this might still help others ending up here.
Essentially what I was doing is exporting a schema from one file, using it in another one to compose another schema, and then importing that composed schema back into the first file. While there isn’t a cyclic dependency per se, it causes the mentioned error at runtime.
See this CodeSandbox for an example: https://codesandbox.io/s/patient-butterfly-hsg3q?file=/src/index.ts The message isn’t as descriptive as in my local (larger) project, but removing this exact kind of import solved it for me. (Maybe this is generally “wrong” in terms of my tsconfig or simply unsupported by zod and I don’t know it, but I would intuitively expect it to work 🙈.)
Issue Analytics
- State:
- Created a year ago
- Comments:12 (1 by maintainers)
Top GitHub Comments
I had a Zod schema defined in a file using schemas defined in another. I was having the about the same issue (
Cannot read properties of undefined (reading '_asyncParse')
.Moving the schemas in the same file fixed it.
I then got the issue again in another context. I looked at this issue which was mentioned earlier and avoiding the “circular import” mentioned here fixed it, definitely I hope.
Facing the same issue and this is a clear reproducible example without any circular dependency: https://codesandbox.io/s/patient-butterfly-hsg3q?file=/src/index.ts