[REQ][typescript-*] String Enums
Created by: eseliger
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',
}