File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -35,6 +35,8 @@ The `EmitOptions` are the root options. They include two properties:
3535
3636 - ` filter ` ** (enumObject: CSharpEnum) => boolean**
3737
38+ - ` useConst ` ** boolean**
39+
3840 - ` structEmitOptions ` ** StructEmitOptions**
3941 configures default struct settings. This settings hierachy is flat.
4042
Original file line number Diff line number Diff line change @@ -281,4 +281,33 @@ The following TypeScript code would be generated:
281281declare type MyEnum =
282282 ' FirstOption' |
283283 ' SecondOption'
284+ ` ` `
285+
286+ # Declare enums as const
287+ ` ` ` typescript
288+ var typescriptCode = emitter .emit (<EmitOptions >{
289+ defaults: <DefaultEmitOptions >{
290+ enumEmitOptions: <EnumEmitOptions >{
291+ useConst: true
292+ }
293+ }
294+ });
295+ ```
296+
297+ Given the following CSharp model code:
298+
299+ ``` csharp
300+ public enum MyEnum {
301+ FirstOption ,
302+ SecondOption
303+ }
304+ ```
305+
306+ The following TypeScript code would be generated:
307+
308+ ``` typescript
309+ const enum MyEnum {
310+ FirstOption ,
311+ SecondOption = 1
312+ }
284313```
Original file line number Diff line number Diff line change @@ -215,6 +215,10 @@ export class Emitter {
215215 options . strategy = "default" ;
216216 }
217217
218+ if ( ! options . useConst ) {
219+ options . useConst = false ;
220+ }
221+
218222 return options ;
219223 }
220224
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ export interface EnumEmitOptionsBase {
99 declare ?: boolean ;
1010 strategy ?: "default" | "string-union" ;
1111 filter ?: ( enumObject : CSharpEnum ) => boolean ;
12+ useConst ?: boolean ;
1213}
1314
1415export interface EnumEmitOptions extends EnumEmitOptionsBase {
@@ -51,6 +52,9 @@ export class EnumEmitter {
5152 if ( options . declare )
5253 modifiers . push ( ts . createToken ( ts . SyntaxKind . DeclareKeyword ) ) ;
5354
55+ if ( options . useConst )
56+ modifiers . push ( ts . createToken ( ts . SyntaxKind . ConstKeyword ) ) ;
57+
5458 var node : ts . Statement ;
5559
5660 if ( options . strategy === "string-union" ) {
You can’t perform that action at this time.
0 commit comments