Skip to content

Incorrect TypeScript generated when using true TS enums and top-level type exports without prefix #2341

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

Open
1 of 2 tasks
conTroll opened this issue May 28, 2025 · 2 comments
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@conTroll
Copy link

conTroll commented May 28, 2025

openapi-typescript version

7.8.0

Node.js version

22.16.0

OS + version

macOS 15.5

Description

When generating the types with true TS enums instead of union types and top-level type exports without the Schema prefix the resulting TS file has errors, because of the overlapping export definitions. For example, given an enum type in the openAPI schema called Permission, you get the following two exports will happen under the same name in the resulting TS file:

export type Permission = components['schemas']['Permission'];

export enum Permission {
    Read = "Read",
    Write = "Write"
}

This does not happen if we export union types instead of enums, then the first export type Permission will be the same:

export type Permission = components['schemas']['Permission'];

but the union type is defined inline in the schema and not exported separately:

/** @enum {string} */
Permission: "Read" | "Write"

Reproduction

Generate a TS API for an OpenAPI schema that contains any enum type with the following options:

--root-types --root-types-no-schema-prefix --enum

Expected result

The resulting code passes the TS compiler without errors.

Required

  • My OpenAPI schema is valid and passes the Redocly validator (npx @redocly/cli@latest lint)

Extra

@conTroll conTroll added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels May 28, 2025
@duncanbeevers
Copy link
Contributor

This is a valid bug, but one of the main hazards in trying to expose different parts of your schema as top-level exports.
I believe this issue a duplicate of #2099 (and likely many others).

@conTroll Given the names of the component schemas you're using here, what would you expect the resulting exports to look like?

@conTroll
Copy link
Author

conTroll commented May 29, 2025

I checked your referenced issue and you are correct, this is a duplicate of #2099 .

My expectation would be the following:

The top-level type exports referring to components['schemas']['...'] can be omitted, if there is a corresponding enum export under that same name. It would be still registered under components['schemas'] so references would not break. In my generated code if I delete the following lines

export type Permission = components['schemas']['Permission'];

for only the enum types I get exactly what I would expect to get, and our app works as expected with the queries being called in Tanstack Query using openapi-fetch 0.14.0 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

2 participants