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

Please extend zod generator with TS types from zod itself #1011

Open
kopach opened this issue May 22, 2024 · 4 comments
Open

Please extend zod generator with TS types from zod itself #1011

kopach opened this issue May 22, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@kopach
Copy link

kopach commented May 22, 2024

What is the problem this feature would solve?

Types generated from OpenAPI schema by TypeScript generator are far less strict than one provided by ZOD.
Would be really great if we could use TS types provided by ZOD itself since we do use zod parsing anyway

External documents/projects?

docs reference below

https://zod.dev/?id=type-inference

You can extract the TypeScript type of any schema with z.infer<typeof mySchema>

const A = z.string();
type A = z.infer<typeof A>; // string

const u: A = 12; // TypeError
const u: A = "asdf"; // compiles

What is the feature you are proposing to solve the problem?

add option (enabled by default) to pluginZod which would add z.infer<typeof mySchema> to every zod schema generated by kubb (right in the same file as schema)

What alternatives have you considered?

As workaround I do add those types using hooks like so

  hooks: {
    done: [
      'replace-in-file --configFile=replace.config.cjs', // this should go as first hook otherwise won't work
      'prettier --loglevel silent generatedTypes/ --write', // we can apply code fomratting after
    ],
  },

and content of replace.config.cjs (this is config file for https://www.npmjs.com/package/replace-in-file)

module.exports = {
  files: 'generatedTypes/zod/*.ts',
  from: /(export const (\w+) .*)$/gm, // this will find every line starting with export
  to: '$1\nexport type $2=z.infer<typeof $2>', // this will add one more line after one with export wrapping everything after export with z.infer
};

It works quite well for me so far and I really don't see any downside of having this natively handled by kubb

@kopach kopach added the enhancement New feature or request label May 22, 2024
@stijnvanhulle
Copy link
Collaborator

Hi, Do you have some examples of where TypeScript is less strict? We could adapt the TypeScript plugin to have the same strict types but being dependent on something like Zod is not great(not everybody wants to use Zod and I don't want to force people to use Zod for just types).

@csaba-veezla
Copy link

I feel, that the ts plugin should not change.
The zod plugin should be able to export types as well, by providing a type output path.
And plugins that have a dependency on the ts plugin, shouldn't or more like they should require zod(with ts path) or ts.

@Wykks
Copy link

Wykks commented Jun 19, 2024

Hello!

Very useful project, thanks a lot @stijnvanhulle!

I have the same concern about zod, it would be better to be able to export types with zod as well using zod.infer, this way we don't have the extras types when using zod

@stijnvanhulle
Copy link
Collaborator

So if I'm following, you guys want to export all types of the generated Zod schemas(based on z.infer)?

Something like this?

export const petSchema = z.object({ firstName: z.string() })
export type PetSchema = z.infer<typeof petSchema>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants