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

Importing infer type makes all props unknown? #3536

Open
bruceharrison1984 opened this issue May 30, 2024 · 1 comment
Open

Importing infer type makes all props unknown? #3536

bruceharrison1984 opened this issue May 30, 2024 · 1 comment

Comments

@bruceharrison1984
Copy link

bruceharrison1984 commented May 30, 2024

I'm having an issue where I have one package to define Zod schemas and types, and I import those types in other packages. The issue I'm having is that the exported type drops all property types and makes them unknown.

// refresh.ts - exported package
import { GrantType } from '@/types/GrantType';
import z from 'zod';

export const refreshTokenParams = z.object({
  refresh_token: z.string(),
  scope: z.string().optional(),
  client_id: z.string(),
  grant_type: z.literal(GrantType.RefreshToken),
});
export type RefreshTokenParams = z.infer<typeof refreshTokenParams>;

Importing the type where is its needed results in all property types being wiped away:
image

If I take the exact same Zod schema and type and declare them in the package where I am importing them, the type information is persisted. I feel like this is something to do more with TS typings than Zod, but I've hit a wall with how to move forward.

edit
It appears my issue is due to using a an enum type within z.literal. Changing this to a regular string fixes type-inference, but then I lose the ability to reference my source enum for the value. This is further complicated because I'm using these types as part of a discriminatedUnion, with the enum values being used as the discriminator.

@maxmarchuk
Copy link

I have a very similar issue with "zod": "^3.23.8". regardless of the types i'm using in my schema, it is generating each field as optional (even if i don't say .optional()).

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

No branches or pull requests

2 participants