@@ -10,6 +10,7 @@ export interface TypeScriptPluginParsedConfig extends ParsedTypesConfig {
10
10
avoidOptionals : AvoidOptionalsConfig ;
11
11
constEnums : boolean ;
12
12
enumsAsTypes : boolean ;
13
+ enumsAsConst : boolean ;
13
14
fieldWrapperValue : string ;
14
15
immutableTypes : boolean ;
15
16
maybeValue : string ;
@@ -26,6 +27,7 @@ export class TsVisitor<TRawConfig extends TypeScriptPluginConfig = TypeScriptPlu
26
27
fieldWrapperValue : getConfigValue ( pluginConfig . fieldWrapperValue , 'T' ) ,
27
28
constEnums : getConfigValue ( pluginConfig . constEnums , false ) ,
28
29
enumsAsTypes : getConfigValue ( pluginConfig . enumsAsTypes , false ) ,
30
+ enumsAsConst : getConfigValue ( pluginConfig . enumsAsConst , false ) ,
29
31
immutableTypes : getConfigValue ( pluginConfig . immutableTypes , false ) ,
30
32
wrapFieldDefinitions : getConfigValue ( pluginConfig . wrapFieldDefinitions , false ) ,
31
33
...( additionalConfig || { } ) ,
@@ -133,14 +135,45 @@ export class TsVisitor<TRawConfig extends TypeScriptPluginConfig = TypeScriptPlu
133
135
} )
134
136
. join ( ' |\n' )
135
137
) . string ;
136
- } else {
137
- return new DeclarationBlock ( this . _declarationBlockConfig )
138
+ }
139
+
140
+ if ( this . config . enumsAsConst ) {
141
+ const typeName = `export type ${ enumTypeName } = typeof ${ enumTypeName } [keyof typeof ${ enumTypeName } ];` ;
142
+ const enumAsConst = new DeclarationBlock ( {
143
+ ...this . _declarationBlockConfig ,
144
+ blockTransformer : block => {
145
+ return block + ' as const' ;
146
+ } ,
147
+ } )
138
148
. export ( )
139
- . asKind ( this . config . constEnums ? 'const enum' : 'enum ')
149
+ . asKind ( 'const' )
140
150
. withName ( enumTypeName )
141
151
. withComment ( ( node . description as any ) as string )
142
- . withBlock ( this . buildEnumValuesBlock ( enumName , node . values ) ) . string ;
152
+ . withBlock (
153
+ node . values
154
+ . map ( enumOption => {
155
+ const optionName = this . convertName ( enumOption , { useTypesPrefix : false , transformUnderscore : true } ) ;
156
+ const comment = transformComment ( ( enumOption . description as any ) as string , 1 ) ;
157
+ let enumValue : string | number = enumOption . name as any ;
158
+
159
+ if ( this . config . enumValues [ enumName ] && this . config . enumValues [ enumName ] . mappedValues && typeof this . config . enumValues [ enumName ] . mappedValues [ enumValue ] !== 'undefined' ) {
160
+ enumValue = this . config . enumValues [ enumName ] . mappedValues [ enumValue ] ;
161
+ }
162
+
163
+ return comment + indent ( `${ optionName } : ${ wrapWithSingleQuotes ( enumValue ) } ` ) ;
164
+ } )
165
+ . join ( ',\n' )
166
+ ) . string ;
167
+
168
+ return [ enumAsConst , typeName ] . join ( '\n' ) ;
143
169
}
170
+
171
+ return new DeclarationBlock ( this . _declarationBlockConfig )
172
+ . export ( )
173
+ . asKind ( this . config . constEnums ? 'const enum' : 'enum' )
174
+ . withName ( enumTypeName )
175
+ . withComment ( ( node . description as any ) as string )
176
+ . withBlock ( this . buildEnumValuesBlock ( enumName , node . values ) ) . string ;
144
177
}
145
178
146
179
protected getPunctuation ( declarationKind : DeclarationKind ) : string {
0 commit comments