@@ -818,7 +818,7 @@ function convertVariable(
818818 const type = context . checker . getTypeOfSymbolAtLocation ( symbol , declaration ) ;
819819
820820 if (
821- isEnumLiteral ( declaration as ts . VariableDeclaration ) &&
821+ isEnumLike ( context . checker , type , declaration ) &&
822822 symbol . getJsDocTags ( ) . some ( ( tag ) => tag . name === "enum" )
823823 ) {
824824 return convertVariableAsEnum ( context , symbol , exportSymbol ) ;
@@ -852,27 +852,15 @@ function convertVariable(
852852 context . finalizeDeclarationReflection ( reflection , symbol , exportSymbol ) ;
853853}
854854
855- function isEnumLiteral ( declaration : ts . VariableDeclaration ) {
856- if (
857- ! hasAllFlags ( declaration . parent . flags , ts . NodeFlags . Const ) ||
858- ! declaration . initializer ||
859- ! ts . isAsExpression ( declaration . initializer ) ||
860- declaration . initializer . type . getText ( ) !== "const"
861- ) {
862- // Not an as-const expression
863- return false ;
864- }
865-
866- const body = declaration . initializer . expression ;
867- if ( ! ts . isObjectLiteralExpression ( body ) ) {
855+ function isEnumLike ( checker : ts . TypeChecker , type : ts . Type , location : ts . Node ) {
856+ if ( ! hasAllFlags ( type . flags , ts . TypeFlags . Object ) ) {
868857 return false ;
869858 }
870859
871- return body . properties . every (
872- ( prop ) =>
873- ts . isPropertyAssignment ( prop ) &&
874- ts . isStringLiteral ( prop . initializer )
875- ) ;
860+ return type . getProperties ( ) . every ( ( prop ) => {
861+ const propType = checker . getTypeOfSymbolAtLocation ( prop , location ) ;
862+ return propType . isStringLiteral ( ) ;
863+ } ) ;
876864}
877865
878866function convertVariableAsEnum (
@@ -889,22 +877,24 @@ function convertVariableAsEnum(
889877 const rc = context . withScope ( reflection ) ;
890878
891879 const declaration = symbol . declarations ! [ 0 ] as ts . VariableDeclaration ;
892- const init = ( declaration . initializer as ts . AsExpression )
893- . expression as ts . ObjectLiteralExpression ;
880+ const type = context . checker . getTypeAtLocation ( declaration ) ;
894881
895- for ( const prop of init . properties as readonly ts . PropertyAssignment [ ] ) {
896- const childSymbol = context . checker . getSymbolAtLocation ( prop . name ) ;
882+ for ( const prop of type . getProperties ( ) ) {
897883 const reflection = rc . createDeclarationReflection (
898884 ReflectionKind . EnumMember ,
899- childSymbol ,
885+ prop ,
900886 void 0
901887 ) ;
902888
903- reflection . defaultValue = JSON . stringify (
904- ( prop . initializer as ts . StringLiteral ) . text
889+ const propType = context . checker . getTypeOfSymbolAtLocation (
890+ prop ,
891+ declaration
905892 ) ;
893+ assert ( propType . isStringLiteral ( ) ) ;
894+
895+ reflection . defaultValue = JSON . stringify ( propType . value ) ;
906896
907- rc . finalizeDeclarationReflection ( reflection , childSymbol , void 0 ) ;
897+ rc . finalizeDeclarationReflection ( reflection , prop , void 0 ) ;
908898 }
909899
910900 // Skip converting the type alias, if there is one
0 commit comments