Skip to content

Add an option to generate type aliases for schemas #1543

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

Closed
1 task
tgross35 opened this issue Feb 15, 2024 · 2 comments
Closed
1 task

Add an option to generate type aliases for schemas #1543

tgross35 opened this issue Feb 15, 2024 · 2 comments
Labels
enhancement New feature or request openapi-ts Relevant to the openapi-typescript library PRs welcome PRs are welcome to solve this issue!

Comments

@tgross35
Copy link

Description

Schemas are probably the closest representation of TS types and are reused in similar ways. It is somewhat awkward to refer to them using their string paths all the time.

Proposal

Add a flag like --generate-schema-type-aliases that will generate type aliases for schema types. For example, with the below:

export interface components {
    schemas: {
        /** About */
        About: {
            name: string;
            version: string;
            /** Format: date */
            build_date: string;
        };
    responses: never;
    parameters: never;
    requestBodies: never;
    headers: never;
    pathItems: never;
}

With the flag, there would be a new alias that could be directly imported:

type About = components["schemas"]["About"];

Checklist

@tgross35 tgross35 added enhancement New feature or request PRs welcome PRs are welcome to solve this issue! openapi-ts Relevant to the openapi-typescript library labels Feb 15, 2024
@drwpow
Copy link
Contributor

drwpow commented Feb 15, 2024

This has been proposed before, and there are limitations to this. Chiefly, OpenAPI lets you use invalid characters that aren’t valid identifiers, and handling conflicts is tricky (Object.foo and Object-foo are 2 valid schema object names that shouldn’t conflict). So yes, agreed—it’s slightly-verbose for consumers of schemas with IDs that don’t use special characters. But unusable for many schemas that do.

Anyways, this is a dupe of the issue #1414, and is being built out in a PR here (#1260), so please add additional thoughts/comments there to track this.

@drwpow drwpow closed this as completed Feb 15, 2024
@tgross35
Copy link
Author

Thanks for the quick response, sorry I missed those!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request openapi-ts Relevant to the openapi-typescript library PRs welcome PRs are welcome to solve this issue!
Projects
None yet
Development

No branches or pull requests

2 participants