@@ -2183,9 +2183,14 @@ namespace ts {
21832183 // The specified symbol flags need to be reinterpreted as type flags
21842184 buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Type, SymbolFormatFlags.None, nextFlags);
21852185 }
2186- else if (!(flags & TypeFormatFlags.InTypeAlias) && type.flags & ( TypeFlags.Anonymous | TypeFlags.UnionOrIntersection) && type.aliasSymbol &&
2186+ else if (!(flags & TypeFormatFlags.InTypeAlias) && (( type.flags & TypeFlags.Anonymous && !(<AnonymousType>type).target) || type.flags & TypeFlags.UnionOrIntersection) && type.aliasSymbol &&
21872187 isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === SymbolAccessibility.Accessible) {
2188- // Only write out inferred type with its corresponding type-alias if type-alias is visible
2188+ // We emit inferred type as type-alias at the current localtion if all the following is true
2189+ // the input type is has alias symbol that is accessible
2190+ // the input type is a union, intersection or anonymous type that is fully instantiated (if not we want to keep dive into)
2191+ // e.g.: export type Bar<X, Y> = () => [X, Y];
2192+ // export type Foo<Y> = Bar<any, Y>;
2193+ // export const y = (x: Foo<string>) => 1 // we want to emit as ...x: () => [any, string])
21892194 const typeArguments = type.aliasTypeArguments;
21902195 writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags);
21912196 }
@@ -5446,7 +5451,26 @@ namespace ts {
54465451 return false;
54475452 }
54485453
5454+ function isSetOfLiteralsFromSameEnum(types: TypeSet): boolean {
5455+ const first = types[0];
5456+ if (first.flags & TypeFlags.EnumLiteral) {
5457+ const firstEnum = getParentOfSymbol(first.symbol);
5458+ for (let i = 1; i < types.length; i++) {
5459+ const other = types[i];
5460+ if (!(other.flags & TypeFlags.EnumLiteral) || (firstEnum !== getParentOfSymbol(other.symbol))) {
5461+ return false;
5462+ }
5463+ }
5464+ return true;
5465+ }
5466+
5467+ return false;
5468+ }
5469+
54495470 function removeSubtypes(types: TypeSet) {
5471+ if (types.length === 0 || isSetOfLiteralsFromSameEnum(types)) {
5472+ return;
5473+ }
54505474 let i = types.length;
54515475 while (i > 0) {
54525476 i--;
@@ -9354,7 +9378,7 @@ namespace ts {
93549378 captureLexicalThis(node, container);
93559379 }
93569380 if (isFunctionLike(container) &&
9357- (!isInParameterInitializerBeforeContainingFunction(node) || getFunctionLikeThisParameter (container))) {
9381+ (!isInParameterInitializerBeforeContainingFunction(node) || getThisParameter (container))) {
93589382 // Note: a parameter initializer should refer to class-this unless function-this is explicitly annotated.
93599383
93609384 // If this is a function in a JS file, it might be a class method. Check if it's the RHS
@@ -15538,10 +15562,6 @@ namespace ts {
1553815562 }
1553915563 }
1554015564
15541- function parameterIsThisKeyword(parameter: ParameterDeclaration) {
15542- return parameter.name && (<Identifier>parameter.name).originalKeywordKind === SyntaxKind.ThisKeyword;
15543- }
15544-
1554515565 function parameterNameStartsWithUnderscore(parameter: ParameterDeclaration) {
1554615566 return parameter.name && parameter.name.kind === SyntaxKind.Identifier && (<Identifier>parameter.name).text.charCodeAt(0) === CharacterCodes._;
1554715567 }
@@ -20122,18 +20142,8 @@ namespace ts {
2012220142 }
2012320143
2012420144 function getAccessorThisParameter(accessor: AccessorDeclaration): ParameterDeclaration {
20125- if (accessor.parameters.length === (accessor.kind === SyntaxKind.GetAccessor ? 1 : 2) &&
20126- accessor.parameters[0].name.kind === SyntaxKind.Identifier &&
20127- (<Identifier>accessor.parameters[0].name).originalKeywordKind === SyntaxKind.ThisKeyword) {
20128- return accessor.parameters[0];
20129- }
20130- }
20131-
20132- function getFunctionLikeThisParameter(func: FunctionLikeDeclaration) {
20133- if (func.parameters.length &&
20134- func.parameters[0].name.kind === SyntaxKind.Identifier &&
20135- (<Identifier>func.parameters[0].name).originalKeywordKind === SyntaxKind.ThisKeyword) {
20136- return func.parameters[0];
20145+ if (accessor.parameters.length === (accessor.kind === SyntaxKind.GetAccessor ? 1 : 2)) {
20146+ return getThisParameter(accessor);
2013720147 }
2013820148 }
2013920149
0 commit comments