Skip to content
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

fix(types): fix type cache crash #3841

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2421,7 +2421,7 @@ export class ZodObject<
private _cached: { shape: T; keys: string[] } | null = null;

_getCached(): { shape: T; keys: string[] } {
if (this._cached !== null) return this._cached;
if (this._cached) return this._cached;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see the type above is {shape:T;keys:string[]} or null, with a default to null.

So this questions as to why the null check is not correct?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's definitely weird. Seems like it shouldn't happen, at least according to Typescript. Is it possible that somehow shape and keys could end up undefined? Would that cause _cached to be undefined?

I'm not sure how this is happening, if it's some kind of node.js interpreter error or race condition, or some weird edge case that Genkit just ran across... but this patch does fix it at least, and seems still correct. What do you think we should do?

const shape = this._def.shape();
const keys = util.objectKeys(shape);
return (this._cached = { shape, keys });
Expand Down