Skip to content

Zod infers are exported as any #904

@angrybacon

Description

@angrybacon

Reproduction link or steps

  1. Build https://github.com/angrybacon/scrydrop
  2. Inspect dist/index.d.ts
  3. Notice the schema declarations (any)

To avoid having to build yourself, you can inspect the code and the published code directly

schemas.ts

export const ScryCountResponseSchema = z. ...;
export const ScrySearchResponseSchema = z. ...;
export const ScrySingleResponseSchema = z. ...;
export type ScryCountResponse = z.infer<typeof ScryCountResponseSchema>;
export type ScrySearchResponse = z.infer<typeof ScrySearchResponseSchema>;
export type ScrySingleResponse = z.infer<typeof ScrySingleResponseSchema>;

index.d.ts

declare const ScryCountResponseSchema: any;
declare const ScrySearchResponseSchema: any;
declare const ScrySingleResponseSchema: any;
type ScryCountResponse = z.infer<typeof ScryCountResponseSchema>;
type ScrySearchResponse = z.infer<typeof ScrySearchResponseSchema>;
type ScrySingleResponse = z.infer<typeof ScrySingleResponseSchema>;

What is expected?

I am not sure whether this is expected from tsdown's perspective, but from a tsdown user's point of view (mine) I was expecting a copy of the full type to be exported and not the any failback/fallback. Unsure what went wrong that it warranted any but my goal is to export the methods and the type that they yield without having the consumer project depend on Zod directly at all

I am not sure whether this is expected from tsdown's perspective

Feel free to label as a feature request accordingly

What is actually happening?

The resulting .d.ts declares schemas as any, making all types that use it also any. One workaround is to exports the schemas (already the case) and then ask the consumer project to also depend on Zod in do the infer<typeof Schema> in its code but that's not an acceptable workaround in my view

Any additional comments?

I've tried to copy the properties a bit naively and with no success

+type Prettify<T> = { [K in keyof T]: T[K] } & {};

-export type Foo = z.infer<typeof FooSchema>;
+export type Foo = Prettify<z.infer<typeof FooSchema>>;

If that's the right direction but the wrong way to go about it I'd be interested

Metadata

Metadata

Assignees

No one assigned

    Type

    Priority

    None yet

    Effort

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions