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

Improve type inference from Zod schemas #114

Merged
merged 5 commits into from
Jan 6, 2025

Conversation

jspahrsummers
Copy link
Member

@jspahrsummers jspahrsummers commented Jan 3, 2025

Adds some type utilities that attempt to flatten and simplify inferred Zod types, for better readability in hoverover, typechecking, and IDE features.

Since TypeScript is structurally typed, not nominally typed, this shouldn't be a breaking change (as long as the resultant object types are indeed the same structurally!).

For example, a couple of types before:

type ReadResourceRequest = {
    params: {
        uri: string;
        _meta?: z.objectOutputType<{
            progressToken: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber]>>;
        }, z.ZodTypeAny, "passthrough"> | undefined;
    } & {
        ...;
    };
    method: "resources/read";
}

type ListPromptsRequest = {
    method: "prompts/list";
    params?: z.objectOutputType<z.objectUtil.extendShape<{
        _meta: z.ZodOptional<z.ZodObject<{
            progressToken: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber]>>;
        }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
            progressToken: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber]>>;
        }, z.ZodTypeAny, "passthrough">, z.objectInputType<...>>>;
    }, {
        ...;
    }>, z.ZodTypeAny, "passthrough"> | undefined;
}

And after:

type ReadResourceRequest = {
    params: {
        [x: string]: unknown;
        uri: string;
        _meta?: {
            [x: string]: unknown;
            progressToken?: string | number | undefined;
        } | undefined;
    };
    method: "resources/read";
}

type ListPromptsRequest = {
    method: "prompts/list";
    params?: {
        [x: string]: unknown;
        _meta?: {
            [x: string]: unknown;
            progressToken?: string | number | undefined;
        } | undefined;
        cursor?: string | undefined;
    } | undefined;
}

Copy link
Contributor

@jerome3o-anthropic jerome3o-anthropic left a comment

Choose a reason for hiding this comment

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

awesome - tested in my ide and the hover tips are soo much better.

I don't really understand why there isn't a nested z.infer already. But this works!

? { [K in keyof T]: Flatten<T[K]> }
: T;

type Infer<Schema extends ZodTypeAny> = Flatten<z.infer<Schema>>;
Copy link
Contributor

Choose a reason for hiding this comment

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

q: why doesn't z.infer just do this already?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like the weirdness only happens on zod types with .passthrough().

Copy link
Member Author

Choose a reason for hiding this comment

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

Honestly, no idea. The only relevant thing I could find is https://github.com/colinhacks/zod/blob/f7ad26147ba291cb3fb257545972a8e00e767470/src/helpers/partialUtil.ts, which doesn't apply to general z.infer usage—only benefitted a feature that is now deprecated in Zod.

I'm surprised not to see even an issue in the backlog that looks related here. 🤷

@jspahrsummers jspahrsummers merged commit 278cfb1 into main Jan 6, 2025
4 checks passed
@jspahrsummers jspahrsummers deleted the justin/improve-type-inference branch January 6, 2025 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants