@@ -293,7 +293,7 @@ function genExport(_export: Export) {
293293 exportAsDefault : _export . asDefault ,
294294 } ) ;
295295 case "Enum" :
296- if ( _export . asDefault ) {
296+ if ( _export . asDefault && ! _export . expression . deprecatedAliasFor ) {
297297 // TS does not allow export of enums as default
298298 // see https://github.com/microsoft/TypeScript/issues/3320
299299 return (
@@ -559,7 +559,7 @@ function genMethodOrFunction(
559559 text += ")" ;
560560
561561 let hasReturnType = ast . returns !== undefined && ast . returns . type ;
562- text += `: ${ hasReturnType ? genType ( ast . returns . type ) : "void" } ` ;
562+ text += `: ${ hasReturnType ? genType ( ast . returns . type , "returnValue" ) : "void" } ` ;
563563
564564 return text ;
565565}
@@ -609,7 +609,7 @@ function genInterfaceProperty(ast: Variable) {
609609 text += applyTsIgnore ( ast ) ;
610610 text +=
611611 `${ ast . name } ${ ast . optional ? "?" : "" } : ${
612- ast . type ? genType ( ast . type ) : "any"
612+ ast . type ? genType ( ast . type , "property" ) : "any"
613613 } ` + NL ;
614614 return text ;
615615}
@@ -626,14 +626,16 @@ function genConstExport(
626626 if ( options . export && options . exportAsDefault ) {
627627 text += JSDOC ( ast ) + NL ;
628628 text += applyTsIgnore ( ast ) ;
629- text += `const ${ ast . name } : ${ ast . type ? genType ( ast . type ) : "any" } ;` + NL ;
629+ text +=
630+ `const ${ ast . name } : ${ ast . type ? genType ( ast . type , "const" ) : "any" } ;` +
631+ NL ;
630632 text += NL ;
631633 text += `export default ${ ast . name } ;` + NL ;
632634 } else if ( options . export ) {
633635 text += JSDOC ( ast ) + NL ;
634636 text += applyTsIgnore ( ast ) ;
635637 text +=
636- `export const ${ ast . name } : ${ ast . type ? genType ( ast . type ) : "any" } ;` +
638+ `export const ${ ast . name } : ${ ast . type ? genType ( ast . type , "const" ) : "any" } ;` +
637639 NL ;
638640 }
639641 return text ;
@@ -648,7 +650,8 @@ function genField(ast: Variable) {
648650 text += JSDOC ( ast ) + NL ;
649651 text += applyTsIgnore ( ast ) ;
650652 text += ast . static ? "static " : "" ;
651- text += `${ ast . name } : ${ ast . type ? genType ( ast . type ) : "any" } ` + NL ;
653+ text +=
654+ `${ ast . name } : ${ ast . type ? genType ( ast . type , "property" ) : "any" } ` + NL ;
652655 return text ;
653656}
654657
@@ -677,7 +680,7 @@ function genParameter(ast: Parameter) {
677680 } ) ;
678681 text += "}" + NL ;
679682 } else {
680- text += `: ${ ast . type ? genType ( ast . type ) : "any" } ` ;
683+ text += `: ${ ast . type ? genType ( ast . type , "parameter" ) : "any" } ` ;
681684 }
682685
683686 return text ;
@@ -694,6 +697,10 @@ function genEnum(
694697 exportAsDefault : false ,
695698 } ,
696699) {
700+ if ( options . export && ast . deprecatedAliasFor ) {
701+ return genDeprecatedAliasForEnum ( ast , options ) ;
702+ }
703+
697704 let text = "" ;
698705 text += JSDOC ( ast ) + NL ;
699706 text +=
@@ -707,6 +714,38 @@ function genEnum(
707714 return text ;
708715}
709716
717+ /**
718+ * @param ast
719+ * @return
720+ */
721+ function genDeprecatedAliasForEnum (
722+ ast : Enum ,
723+ options : { export : boolean ; exportAsDefault ?: boolean } = {
724+ export : undefined ,
725+ exportAsDefault : false ,
726+ } ,
727+ ) {
728+ if ( ! options . export ) {
729+ console . error (
730+ "deprecated alias is only supported for exported enums" ,
731+ ast ,
732+ options ,
733+ ) ;
734+ throw new TypeError (
735+ `deprecated alias is only supported for exported enums (${ ast . name } )` ,
736+ ) ;
737+ }
738+ let text = "" ;
739+ text += "export {" ;
740+ text += JSDOC ( ast ) + NL ;
741+ text +=
742+ `${ ast . deprecatedAliasFor } as ${ options . exportAsDefault ? "default " : ast . name } ` +
743+ NL ;
744+ text += "}" + NL ;
745+
746+ return text ;
747+ }
748+
710749/**
711750 *
712751 * @param ast
@@ -731,7 +770,7 @@ function genEnumValue(ast: Variable, withValue = false) {
731770function genVariable ( ast : Variable ) {
732771 let text = "" ;
733772 text += JSDOC ( ast ) + NL ;
734- text += `export const ${ ast . name } : ${ genType ( ast . type ) } ;` + NL ;
773+ text += `export const ${ ast . name } : ${ genType ( ast . type , "const" ) } ;` + NL ;
735774
736775 return text ;
737776}
@@ -781,9 +820,10 @@ function hasSimpleElementType(ast: ArrayType): boolean {
781820
782821/**
783822 * @param ast
823+ * @param usage Context in which the type is used
784824 * @returns
785825 */
786- function genType ( ast : Type ) : string {
826+ function genType ( ast : Type , usage : string = "unknown" ) : string {
787827 let text ;
788828 switch ( ast . kind ) {
789829 case "TypeReference" :
@@ -799,49 +839,49 @@ function genType(ast: Type): string {
799839 if ( ast . nullable ) {
800840 text += `|null` ;
801841 }
802- if ( ast . isStandardEnum ) {
842+ if ( ast . isStandardEnum && usage !== "returnValue" ) {
803843 text = `(${ text } | keyof typeof ${ ast . typeName } )` ; // TODO parentheses not always required
804844 }
805845 return text ;
806846 case "ArrayType" :
807847 if ( hasSimpleElementType ( ast ) ) {
808- return `${ genType ( ast . elementType ) } []` ;
848+ return `${ genType ( ast . elementType , usage ) } []` ;
809849 }
810- return `Array<${ genType ( ast . elementType ) } >` ;
850+ return `Array<${ genType ( ast . elementType , usage ) } >` ;
811851 case "LiteralType" :
812852 return String ( ast . literal ) ;
813853 case "TypeLiteral" :
814854 return `{${ NL } ${ _ . map ( ast . members , ( prop ) => {
815855 let ptext = "" ;
816856 ptext += JSDOC ( prop ) + NL ;
817857 ptext +=
818- `${ prop . name } ${ prop . optional ? "?" : "" } : ${ genType ( prop . type ) } ,` +
858+ `${ prop . name } ${ prop . optional ? "?" : "" } : ${ genType ( prop . type , usage ) } ,` +
819859 NL ;
820860 return ptext ;
821861 } ) . join ( "" ) } }`;
822862 case "UnionType" :
823863 const unionTypes : string [ ] = _ . map ( ast . types , ( variantType ) => {
824864 if ( variantType . kind === "FunctionType" ) {
825- return `(${ genType ( variantType ) } )` ;
865+ return `(${ genType ( variantType , usage ) } )` ;
826866 }
827- return genType ( variantType ) ;
867+ return genType ( variantType , usage ) ;
828868 } ) ;
829869 return unionTypes . join ( " | " ) ;
830870 case "IntersectionType" :
831871 const intersectionTypes : string [ ] = _ . map ( ast . types , ( variantType ) => {
832872 if ( variantType . kind === "FunctionType" ) {
833- return `(${ genType ( variantType ) } )` ;
873+ return `(${ genType ( variantType , usage ) } )` ;
834874 }
835- return genType ( variantType ) ;
875+ return genType ( variantType , usage ) ;
836876 } ) ;
837877 return intersectionTypes . join ( " & " ) ;
838878 case "FunctionType" :
839879 text = "" ;
840880 if ( ! _ . isEmpty ( ast . typeParameters ) ) {
841881 text += `<${ _ . map ( ast . typeParameters , ( param ) => param . name ) . join ( ", " ) } >` ; // TODO defaults, constraints, expressions
842882 }
843- text += `(${ _ . map ( ast . parameters , ( param ) => `${ param . name } : ${ genType ( param . type ) } ` ) . join ( ", " ) } )` ;
844- text += ` => ${ ast . type ? genType ( ast . type ) : "void" } ` ;
883+ text += `(${ _ . map ( ast . parameters , ( param ) => `${ param . name } : ${ genType ( param . type , "parameter" ) } ` ) . join ( ", " ) } )` ;
884+ text += ` => ${ ast . type ? genType ( ast . type , "returnValue" ) : "void" } ` ;
845885 return text ;
846886 case "NativeTSTypeExpression" :
847887 // native TS type expression, emit the 'type' string "as is"
0 commit comments