@@ -135,20 +135,16 @@ function getImportLine(dependency: Protobuf.Type | Protobuf.Enum | Protobuf.Serv
135
135
/* If the dependency is defined within a message, it will be generated in that
136
136
* message's file and exported using its typeInterfaceName. */
137
137
if ( dependency . parent instanceof Protobuf . Type ) {
138
- if ( dependency instanceof Protobuf . Type ) {
138
+ if ( dependency instanceof Protobuf . Type || dependency instanceof Protobuf . Enum ) {
139
139
importedTypes = `${ inputName ( typeInterfaceName ) } , ${ outputName ( typeInterfaceName ) } ` ;
140
- } else if ( dependency instanceof Protobuf . Enum ) {
141
- importedTypes = `${ typeInterfaceName } ` ;
142
140
} else if ( dependency instanceof Protobuf . Service ) {
143
141
importedTypes = `${ typeInterfaceName } Client, ${ typeInterfaceName } Definition` ;
144
142
} else {
145
143
throw new Error ( 'Invalid object passed to getImportLine' ) ;
146
144
}
147
145
} else {
148
- if ( dependency instanceof Protobuf . Type ) {
146
+ if ( dependency instanceof Protobuf . Type || dependency instanceof Protobuf . Enum ) {
149
147
importedTypes = `${ inputName ( dependency . name ) } as ${ inputName ( typeInterfaceName ) } , ${ outputName ( dependency . name ) } as ${ outputName ( typeInterfaceName ) } ` ;
150
- } else if ( dependency instanceof Protobuf . Enum ) {
151
- importedTypes = `${ dependency . name } as ${ typeInterfaceName } ` ;
152
148
} else if ( dependency instanceof Protobuf . Service ) {
153
149
importedTypes = `${ dependency . name } Client as ${ typeInterfaceName } Client, ${ dependency . name } Definition as ${ typeInterfaceName } Definition` ;
154
150
} else {
@@ -213,14 +209,14 @@ function getTypeNamePermissive(fieldType: string, resolvedType: Protobuf.Type |
213
209
throw new Error ( 'Found field with no usable type' ) ;
214
210
}
215
211
const typeInterfaceName = getTypeInterfaceName ( resolvedType ) ;
216
- if ( resolvedType instanceof Protobuf . Type ) {
212
+ if ( resolvedType instanceof Protobuf . Type || resolvedType instanceof Protobuf . Enum ) {
217
213
if ( repeated || map ) {
218
214
return inputName ( typeInterfaceName ) ;
219
215
} else {
220
216
return `${ inputName ( typeInterfaceName ) } | null` ;
221
217
}
222
218
} else {
223
- return ` ${ typeInterfaceName } | keyof typeof ${ typeInterfaceName } ` ;
219
+ return '' ;
224
220
}
225
221
}
226
222
}
@@ -315,7 +311,7 @@ function getTypeNameRestricted(fieldType: string, resolvedType: Protobuf.Type |
315
311
throw new Error ( 'Found field with no usable type' ) ;
316
312
}
317
313
const typeInterfaceName = getTypeInterfaceName ( resolvedType ) ;
318
- if ( resolvedType instanceof Protobuf . Type ) {
314
+ if ( resolvedType instanceof Protobuf . Type || resolvedType instanceof Protobuf . Enum ) {
319
315
/* null is only used to represent absent message values if the defaults
320
316
* option is set, and only for non-repeated, non-map fields. */
321
317
if ( options . defaults && ! repeated && ! map ) {
@@ -324,11 +320,7 @@ function getTypeNameRestricted(fieldType: string, resolvedType: Protobuf.Type |
324
320
return ` $ { outputName ( typeInterfaceName ) } `;
325
321
}
326
322
} else {
327
- if ( options . enums == String ) {
328
- return `keyof typeof ${typeInterfaceName } `;
329
- } else {
330
- return typeInterfaceName;
331
- }
323
+ return '';
332
324
}
333
325
}
334
326
}
@@ -455,21 +447,46 @@ function generateMessageInterfaces(formatter: TextFormatter, messageType: Protob
455
447
}
456
448
457
449
function generateEnumInterface ( formatter : TextFormatter , enumType : Protobuf . Enum , options : GeneratorOptions , nameOverride ? : string ) {
450
+ const { inputName , outputName } = useNameFmter ( options ) ;
451
+ const name = nameOverride ?? enumType . name ;
458
452
formatter . writeLine ( `/ / Original file : ${( enumType . filename ?? 'null ') ?. replace ( / \\/ g , '/ ') } `) ;
459
453
formatter . writeLine ( '') ;
460
454
if ( options . includeComments ) {
461
455
formatComment ( formatter , enumType . comment ) ;
462
456
}
463
- formatter . writeLine ( `export enum ${ nameOverride ?? enumType . name } {` ) ;
457
+ formatter . writeLine ( `export const ${name } = { `) ;
464
458
formatter . indent ( ) ;
465
459
for ( const key of Object . keys ( enumType . values ) ) {
466
460
if ( options . includeComments ) {
467
461
formatComment ( formatter , enumType . comments [ key ] ) ;
468
462
}
469
- formatter . writeLine ( `${ key } = ${ enumType . values [ key ] } ,` ) ;
463
+ formatter . writeLine ( `${ key } : ${ options . enums == String ? `' ${ key } '` : enumType . values [ key ] } ,` ) ;
470
464
}
471
465
formatter . unindent ( ) ;
472
- formatter . writeLine ( '}' ) ;
466
+ formatter . writeLine ( '} as const;' ) ;
467
+
468
+ // Permissive Type
469
+ formatter . writeLine ( '' ) ;
470
+ if ( options . includeComments ) {
471
+ formatComment ( formatter , enumType . comment ) ;
472
+ }
473
+ formatter . writeLine ( `export type ${ inputName ( name ) } =` )
474
+ formatter . indent ( ) ;
475
+ for ( const key of Object . keys ( enumType . values ) ) {
476
+ if ( options . includeComments ) {
477
+ formatComment ( formatter , enumType . comments [ key ] ) ;
478
+ }
479
+ formatter . writeLine ( `| '${ key } '` ) ;
480
+ formatter . writeLine ( `| ${ enumType . values [ key ] } ` ) ;
481
+ }
482
+ formatter . unindent ( ) ;
483
+
484
+ // Restrictive Type
485
+ formatter . writeLine ( '' ) ;
486
+ if ( options . includeComments ) {
487
+ formatComment ( formatter , enumType . comment ) ;
488
+ }
489
+ formatter . writeLine ( `export type ${ outputName ( name ) } = typeof ${ name } [keyof typeof ${ name } ]` )
473
490
}
474
491
475
492
/**
0 commit comments