Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[REQ][typescript-*] String Enums #3451

Closed
eseliger opened this issue Jul 24, 2019 · 12 comments · Fixed by #3464
Closed

[REQ][typescript-*] String Enums #3451

eseliger opened this issue Jul 24, 2019 · 12 comments · Fixed by #3464

Comments

@eseliger
Copy link
Contributor

eseliger commented Jul 24, 2019

Is your feature request related to a problem? Please describe.

Since TS version 2.4 string enums are supported, which are the preferred way of defining enums that can have string values (over the legacy object-type-dualism). https://www.typescriptlang.org/docs/handbook/enums.html#string-enums

Describe the solution you'd like

I implemented it already on a branch for internal use at my company, which is using an additional property called "stringEnums" for now. No breaking change since it needs to be enabled, off by default

Describe alternatives you've considered

Other solutions that would be possible are:

  • Default to string enums, which would be incompatible with TS<2.4 and be a breaking change
  • Add a proprietary flag to the openapi spec. I think this is not really desirable because most problems that are solved this way are rather specific to the operation, whereas this is more of a global flag IMO.

Comparison of outputs

before:

export interface User {
    title: User.TitleEnum
    name: string
}
export namespace User {
	export type TitleEnum = 'Mr' | 'Mrs' | 'Ms'
	export const TitleEnum = {
	    Mr: 'Mr' as Title,
	    Mrs: 'Mrs' as Title,
	    Ms: 'Ms' as Title,
	}
}

with string enums enabled:

export interface User {
    title: UserTitle
    name: string
}
export enum UserTitle {
    Mr = 'Mr',
    Mrs = 'Mrs',
    Ms = 'Ms',
}
@macjohnny
Copy link
Member

@eseliger a contribution would be welcome!

@eseliger
Copy link
Contributor Author

@macjohnny I just realized when I wanted to port the changes to the other typescript clients, that most of them use string enums already, but typescript-angular doesn't.

What would you prefer?

  • Align it and make it a breaking change
  • Only implement it with the flag for typescript-angular and might remove it in v5

this one I meant 🤦‍♂

@macjohnny
Copy link
Member

I would prefer to go with the flag, and decide later whether to default to true and introduce a breaking change. The typescript-generators are subject to refactoring, see #802

@bodograumann
Copy link
Contributor

This is not implemented for typescript-inversify.

Out of interest: What is your use case of enums in contrast to union types? I have found that the latter are sufficient.

@macjohnny
Copy link
Member

Enum values can be re-used elsewhere

@ffMathy
Copy link
Contributor

ffMathy commented Jan 10, 2020

An exported type can also be used anywhere, no?

@Morriz
Copy link

Morriz commented Jun 14, 2021

this would be nice to see...has there been any activity on this request?

@bodograumann
Copy link
Contributor

Which generator are you using, @Morriz ?

@Morriz
Copy link

Morriz commented Jun 14, 2021

Thanks for asking! Just upgraded express-openapi from 8.0.0 to 9.0.3 and that solved it ;)

@Morriz
Copy link

Morriz commented Jun 14, 2021

Grrr. Too quick. The typescript that gets generated is fine, but the dist/*js are not using the strings.

@bodograumann
Copy link
Contributor

Could you please create a new issue, @Morriz ?

@Morriz
Copy link

Morriz commented Jun 17, 2021

not needed...the stuff shown in the debugger is not what is used I deduce. TS that generates enum NOT using strings is shown, but the JS is using strings.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants