@@ -54,38 +54,19 @@ export function generateType(
5454 * export type EnumExample = "ONE" | "TWO";
5555 * export const EnumExample = { ONE: "ONE" as "ONE", TWO: "TWO" as "TWO" };
5656 * ```
57- *
58- * We do not use TypeScript Enums because they can not be assigned to an equivalent enum, making interop across
59- * libraries more difficult
6057 */
6158export async function generateEnum ( definition : IEnumDefinition , simpleAst : SimpleAst ) : Promise < void > {
6259 const sourceFile = simpleAst . createSourceFile ( definition . typeName ) ;
6360
64- const enumType = sourceFile . addTypeAlias ( {
61+ sourceFile . addEnum ( {
62+ docs : definition . docs != null ? [ { description : definition . docs } ] : undefined ,
6563 isExported : true ,
64+ members : definition . values . map ( ( { docs, value } ) => ( {
65+ docs : docs != null ? [ { description : docs } ] : undefined ,
66+ name : value ,
67+ value,
68+ } ) ) ,
6669 name : definition . typeName . name ,
67- type : definition . values . map ( ( { value } ) => doubleQuote ( value ) ) . join ( " | " ) ,
68- } ) ;
69- if ( definition . docs != null ) {
70- enumType . addJsDoc ( definition . docs ) ;
71- }
72-
73- const variableStatement = sourceFile . addVariableStatement ( {
74- declarationKind : VariableDeclarationKind . Const ,
75- declarations : [
76- {
77- initializer : "{}" ,
78- name : definition . typeName . name ,
79- } ,
80- ] ,
81- isExported : true ,
82- } ) ;
83- const objectLiteralExpr = variableStatement . getDeclarations ( ) [ 0 ] . getInitializer ( ) as ObjectLiteralExpression ;
84- definition . values . forEach ( value => {
85- objectLiteralExpr . addPropertyAssignment ( {
86- initializer : `${ doubleQuote ( value . value ) } as ${ doubleQuote ( value . value ) } ` ,
87- name : value . value ,
88- } ) ;
8970 } ) ;
9071
9172 sourceFile . formatText ( ) ;
0 commit comments