Skip to content

Commit a31415d

Browse files
authored
Merge pull request microsoft#11228 from Microsoft/master_11192
[Master] Fix 11192
2 parents f8b34c9 + a7f9d73 commit a31415d

5 files changed

+69
-2
lines changed

src/compiler/checker.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [declarationEmitTypeAliasWithTypeParameters1.ts]
2+
3+
export type Bar<X, Y> = () => [X, Y];
4+
export type Foo<Y> = Bar<any, Y>;
5+
export const y = (x: Foo<string>) => 1
6+
7+
//// [declarationEmitTypeAliasWithTypeParameters1.js]
8+
"use strict";
9+
exports.y = function (x) { return 1; };
10+
11+
12+
//// [declarationEmitTypeAliasWithTypeParameters1.d.ts]
13+
export declare type Bar<X, Y> = () => [X, Y];
14+
export declare type Foo<Y> = Bar<any, Y>;
15+
export declare const y: (x: () => [any, string]) => number;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/declarationEmitTypeAliasWithTypeParameters1.ts ===
2+
3+
export type Bar<X, Y> = () => [X, Y];
4+
>Bar : Symbol(Bar, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 0, 0))
5+
>X : Symbol(X, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 1, 16))
6+
>Y : Symbol(Y, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 1, 18))
7+
>X : Symbol(X, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 1, 16))
8+
>Y : Symbol(Y, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 1, 18))
9+
10+
export type Foo<Y> = Bar<any, Y>;
11+
>Foo : Symbol(Foo, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 1, 37))
12+
>Y : Symbol(Y, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 2, 16))
13+
>Bar : Symbol(Bar, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 0, 0))
14+
>Y : Symbol(Y, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 2, 16))
15+
16+
export const y = (x: Foo<string>) => 1
17+
>y : Symbol(y, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 3, 12))
18+
>x : Symbol(x, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 3, 18))
19+
>Foo : Symbol(Foo, Decl(declarationEmitTypeAliasWithTypeParameters1.ts, 1, 37))
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/declarationEmitTypeAliasWithTypeParameters1.ts ===
2+
3+
export type Bar<X, Y> = () => [X, Y];
4+
>Bar : Bar<X, Y>
5+
>X : X
6+
>Y : Y
7+
>X : X
8+
>Y : Y
9+
10+
export type Foo<Y> = Bar<any, Y>;
11+
>Foo : () => [any, Y]
12+
>Y : Y
13+
>Bar : Bar<X, Y>
14+
>Y : Y
15+
16+
export const y = (x: Foo<string>) => 1
17+
>y : (x: () => [any, string]) => number
18+
>(x: Foo<string>) => 1 : (x: () => [any, string]) => number
19+
>x : () => [any, string]
20+
>Foo : () => [any, Y]
21+
>1 : 1
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @declaration: true
2+
3+
export type Bar<X, Y> = () => [X, Y];
4+
export type Foo<Y> = Bar<any, Y>;
5+
export const y = (x: Foo<string>) => 1

0 commit comments

Comments
 (0)