Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Hi everyone,
Currently we generate TypeScript definitions from OpenAPI specs. These definitions include deeply nested paths like components["schemas"]["Example"]. This depends for sure on the project complexity. Using the Example type within the code is really unhandy, except to manually destruct this into a static type again in a separate file. This makes the code quite unreadable and IDE autocompletion and type-checking become less intuitive or the developer has a lot of manually effort maintaining the types.

The following two options I frequently see in our projects:

  1. Developers must repeatedly reference verbose nested types. Using nested types directly in the code is hard to maintain when the API changes.
  2. Instead of using them directly within the code, developers often destructure nested types into a separate file, creating additional work and complexity.

What I currently do, instead of the two above options, I automatically extract the nested type paths like components["schemas"]["Example"] into standalone, static type definitions (e.g., export interface Example { ... }), providing a more concise, flat hierarchy of types for easier usage.

I would personally add an option: --flat-schema, including the --enum option already. This would flatten the components["schemas"] Dtos and generate enums from type unions.

Currently I basically to this by traversing through the AST and extracting the types and interfaces from the generated types file:

const visit = (node: ts.Node) => {
  if (ts.isInterfaceDeclaration(node) && node.name.text === "components") {
  // Locate the 'schemas' property
        // push interfaces
        // push types
        // create Enums from Type Unions

At the end I have two files, one is the generated api.d.ts and second the types.d.ts. The developer would then reference in the code only the interfaces, types and enums from the types.d.ts. From e.g. this:

const { mutate: update } = useMutation({
    mutationFn: (args: schemas["components"]["ExampleDTO"]) => {
      return client.PUT(...);
    },
  });

to this:

const { mutate: update } = useMutation({
    mutationFn: (args: ExampleDTO) => {
      return client.PUT(...);
    },
  });

I would like to suggest adding such functionality. If needed I can share the function. Any thoughts on this?

Best regards,
reQ

You must be logged in to vote

Replies: 1 comment

Comment options

I realised this has been supported for a long time after the merge of this PR #1876
Add this flag to the command --root-types --root-types-no-schema-prefix

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
💡
Ideas
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.