File tree Expand file tree Collapse file tree 4 files changed +39
-0
lines changed Expand file tree Collapse file tree 4 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,8 @@ The `EmitOptions` are the root options. They include two properties:
35
35
36
36
- ` filter ` ** (enumObject: CSharpEnum) => boolean**
37
37
38
+ - ` useConst ` ** boolean**
39
+
38
40
- ` structEmitOptions ` ** StructEmitOptions**
39
41
configures default struct settings. This settings hierachy is flat.
40
42
Original file line number Diff line number Diff line change @@ -281,4 +281,33 @@ The following TypeScript code would be generated:
281
281
declare type MyEnum =
282
282
' FirstOption' |
283
283
' 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
+ }
284
313
```
Original file line number Diff line number Diff line change @@ -215,6 +215,10 @@ export class Emitter {
215
215
options . strategy = "default" ;
216
216
}
217
217
218
+ if ( ! options . useConst ) {
219
+ options . useConst = false ;
220
+ }
221
+
218
222
return options ;
219
223
}
220
224
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ export interface EnumEmitOptionsBase {
9
9
declare ?: boolean ;
10
10
strategy ?: "default" | "string-union" ;
11
11
filter ?: ( enumObject : CSharpEnum ) => boolean ;
12
+ useConst ?: boolean ;
12
13
}
13
14
14
15
export interface EnumEmitOptions extends EnumEmitOptionsBase {
@@ -51,6 +52,9 @@ export class EnumEmitter {
51
52
if ( options . declare )
52
53
modifiers . push ( ts . createToken ( ts . SyntaxKind . DeclareKeyword ) ) ;
53
54
55
+ if ( options . useConst )
56
+ modifiers . push ( ts . createToken ( ts . SyntaxKind . ConstKeyword ) ) ;
57
+
54
58
var node : ts . Statement ;
55
59
56
60
if ( options . strategy === "string-union" ) {
You can’t perform that action at this time.
0 commit comments