Skip to content

Improve type inference from Zod schemas - #114

Merged
jspahrsummers merged 5 commits into
mainfrom
justin/improve-type-inference
Jan 6, 2025
Merged

jspahrsummers merged 5 commits into
mainfrom
justin/improve-type-inference

Conversation

@jspahrsummers

@jspahrsummers jspahrsummers commented Jan 3, 2025 •

Copy link
Copy Markdown
Member

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;
}

@jerome3o-anthropic jerome3o-anthropic left a comment

Copy link
Copy Markdown
Member

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!

Comment thread src/types.ts
? { [K in keyof T]: Flatten<T[K]> }
: T;

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

Copy link
Copy Markdown
Member

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
Copy Markdown
Member

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
Copy Markdown
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
@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