swagger-typescript-codegen icon indicating copy to clipboard operation
swagger-typescript-codegen copied to clipboard

[feature] concise response type alias

Open scottc opened this issue 5 years ago • 4 comments

The response types can be quite long, and inconvenient to use.

const x: Promise<ResponseWithBody<200, OkBody> | ResponseWithBody<404, NotFoundBody> | ResponseWithBody<400, BadRequestBody>> = undefined;

I propose generating some concise type aliases.

type SomeAlias1 = ResponseWithBody<200, OkBody>;
type SomeAlias2 = ResponseWithBody<404, NotFoundBody>;
type SomeAlias3 = ResponseWithBody<400, BadRequestBody>;

type SomeUnionResponseAlias = SomeAlias1 | SomeAlias2 | SomeAlias3;

The alias's should be backwards compatible, optional to use and work as drop in replacements.

scottc avatar Jun 07 '19 04:06 scottc

Hi Scott,

I really like the idea.

Just to clarify, when you say optional you mean you would like like to be able to control if they are generated or not?

Markionium avatar Jun 07 '19 06:06 Markionium

Just to clarify, when you say optional you mean you would like like to be able to control if they are generated or not?

My original intent was "optional to use" the aliases.

Typescript type definitions are only used at compile time, and there is no additional code generated in the output .js file. So I can't think of any drawbacks of always generating these additional types. Would be up-to the developer to decide which types they want to import and use.

scottc avatar Jun 07 '19 07:06 scottc

Really like this idea as well! Ensuring we export both the verbose and the consise types would make them optional to use as well. Should not cause any bundle size issues either for the consumer, as as you mention these are just compile time types

mtennoe avatar Jun 07 '19 10:06 mtennoe

@scottc - Wanna give this a shot?

mtennoe avatar Jun 17 '19 06:06 mtennoe