Skip to content

Commit 2282477

Browse files
authored
Merge pull request #12068 from Microsoft/fixTypeAliasDisplay
Fix type alias display
2 parents 84f8f8b + be0358c commit 2282477

8 files changed

+204
-23
lines changed

src/compiler/checker.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,14 +2229,8 @@ namespace ts {
22292229
// The specified symbol flags need to be reinterpreted as type flags
22302230
buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Type, SymbolFormatFlags.None, nextFlags);
22312231
}
2232-
else if (!(flags & TypeFormatFlags.InTypeAlias) && ((getObjectFlags(type) & ObjectFlags.Anonymous && !(<AnonymousType>type).target) || type.flags & TypeFlags.UnionOrIntersection) && type.aliasSymbol &&
2232+
else if (!(flags & TypeFormatFlags.InTypeAlias) && (getObjectFlags(type) & ObjectFlags.Anonymous || type.flags & TypeFlags.UnionOrIntersection) && type.aliasSymbol &&
22332233
isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === SymbolAccessibility.Accessible) {
2234-
// We emit inferred type as type-alias at the current localtion if all the following is true
2235-
// the input type is has alias symbol that is accessible
2236-
// the input type is a union, intersection or anonymous type that is fully instantiated (if not we want to keep dive into)
2237-
// e.g.: export type Bar<X, Y> = () => [X, Y];
2238-
// export type Foo<Y> = Bar<any, Y>;
2239-
// export const y = (x: Foo<string>) => 1 // we want to emit as ...x: () => [any, string])
22402234
const typeArguments = type.aliasTypeArguments;
22412235
writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags);
22422236
}
@@ -4164,8 +4158,8 @@ namespace ts {
41644158
else {
41654159
mapper = createTypeMapper(typeParameters, typeArguments);
41664160
members = createInstantiatedSymbolTable(source.declaredProperties, mapper, /*mappingThisOnly*/ typeParameters.length === 1);
4167-
callSignatures = instantiateList(source.declaredCallSignatures, mapper, instantiateSignature);
4168-
constructSignatures = instantiateList(source.declaredConstructSignatures, mapper, instantiateSignature);
4161+
callSignatures = instantiateSignatures(source.declaredCallSignatures, mapper);
4162+
constructSignatures = instantiateSignatures(source.declaredConstructSignatures, mapper);
41694163
stringIndexInfo = instantiateIndexInfo(source.declaredStringIndexInfo, mapper);
41704164
numberIndexInfo = instantiateIndexInfo(source.declaredNumberIndexInfo, mapper);
41714165
}
@@ -4363,8 +4357,8 @@ namespace ts {
43634357
const symbol = type.symbol;
43644358
if (type.target) {
43654359
const members = createInstantiatedSymbolTable(getPropertiesOfObjectType(type.target), type.mapper, /*mappingThisOnly*/ false);
4366-
const callSignatures = instantiateList(getSignaturesOfType(type.target, SignatureKind.Call), type.mapper, instantiateSignature);
4367-
const constructSignatures = instantiateList(getSignaturesOfType(type.target, SignatureKind.Construct), type.mapper, instantiateSignature);
4360+
const callSignatures = instantiateSignatures(getSignaturesOfType(type.target, SignatureKind.Call), type.mapper);
4361+
const constructSignatures = instantiateSignatures(getSignaturesOfType(type.target, SignatureKind.Construct), type.mapper);
43684362
const stringIndexInfo = instantiateIndexInfo(getIndexInfoOfType(type.target, IndexKind.String), type.mapper);
43694363
const numberIndexInfo = instantiateIndexInfo(getIndexInfoOfType(type.target, IndexKind.Number), type.mapper);
43704364
setStructuredTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo);
@@ -6043,6 +6037,14 @@ namespace ts {
60436037
return items;
60446038
}
60456039

6040+
function instantiateTypes(types: Type[], mapper: TypeMapper) {
6041+
return instantiateList(types, mapper, instantiateType);
6042+
}
6043+
6044+
function instantiateSignatures(signatures: Signature[], mapper: TypeMapper) {
6045+
return instantiateList(signatures, mapper, instantiateSignature);
6046+
}
6047+
60466048
function createUnaryTypeMapper(source: Type, target: Type): TypeMapper {
60476049
return t => t === source ? target : t;
60486050
}
@@ -6069,7 +6071,6 @@ namespace ts {
60696071
count == 2 ? createBinaryTypeMapper(sources[0], targets ? targets[0] : anyType, sources[1], targets ? targets[1] : anyType) :
60706072
createArrayTypeMapper(sources, targets);
60716073
mapper.mappedTypes = sources;
6072-
mapper.targetTypes = targets;
60736074
return mapper;
60746075
}
60756076

@@ -6196,7 +6197,7 @@ namespace ts {
61966197
result.target = type;
61976198
result.mapper = mapper;
61986199
result.aliasSymbol = type.aliasSymbol;
6199-
result.aliasTypeArguments = mapper.targetTypes;
6200+
result.aliasTypeArguments = instantiateTypes(type.aliasTypeArguments, mapper);
62006201
mapper.instantiations[type.id] = result;
62016202
return result;
62026203
}
@@ -6272,14 +6273,14 @@ namespace ts {
62726273
instantiateAnonymousType(<AnonymousType>type, mapper) : type;
62736274
}
62746275
if ((<ObjectType>type).objectFlags & ObjectFlags.Reference) {
6275-
return createTypeReference((<TypeReference>type).target, instantiateList((<TypeReference>type).typeArguments, mapper, instantiateType));
6276+
return createTypeReference((<TypeReference>type).target, instantiateTypes((<TypeReference>type).typeArguments, mapper));
62766277
}
62776278
}
62786279
if (type.flags & TypeFlags.Union && !(type.flags & TypeFlags.Primitive)) {
6279-
return getUnionType(instantiateList((<UnionType>type).types, mapper, instantiateType), /*subtypeReduction*/ false, type.aliasSymbol, mapper.targetTypes);
6280+
return getUnionType(instantiateTypes((<UnionType>type).types, mapper), /*subtypeReduction*/ false, type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper));
62806281
}
62816282
if (type.flags & TypeFlags.Intersection) {
6282-
return getIntersectionType(instantiateList((<IntersectionType>type).types, mapper, instantiateType), type.aliasSymbol, mapper.targetTypes);
6283+
return getIntersectionType(instantiateTypes((<IntersectionType>type).types, mapper), type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper));
62836284
}
62846285
if (type.flags & TypeFlags.Index) {
62856286
return getIndexType(instantiateType((<IndexType>type).type, mapper));

src/compiler/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2943,7 +2943,6 @@ namespace ts {
29432943
export interface TypeMapper {
29442944
(t: TypeParameter): Type;
29452945
mappedTypes?: Type[]; // Types mapped by this mapper
2946-
targetTypes?: Type[]; // Types substituted for mapped types
29472946
instantiations?: Type[]; // Cache of instantiations created using this type mapper.
29482947
context?: InferenceContext; // The inference context this mapper was created from.
29492948
// Only inference mappers have this set (in createInferenceMapper).

tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ exports.y = function (x) { return 1; };
1212
//// [declarationEmitTypeAliasWithTypeParameters1.d.ts]
1313
export declare type Bar<X, Y> = () => [X, Y];
1414
export declare type Foo<Y> = Bar<any, Y>;
15-
export declare const y: (x: () => [any, string]) => number;
15+
export declare const y: (x: Bar<any, string>) => number;

tests/baselines/reference/declarationEmitTypeAliasWithTypeParameters1.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ export type Bar<X, Y> = () => [X, Y];
88
>Y : Y
99

1010
export type Foo<Y> = Bar<any, Y>;
11-
>Foo : () => [any, Y]
11+
>Foo : Bar<any, Y>
1212
>Y : Y
1313
>Bar : Bar<X, Y>
1414
>Y : Y
1515

1616
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]
17+
>y : (x: Bar<any, string>) => number
18+
>(x: Foo<string>) => 1 : (x: Bar<any, string>) => number
19+
>x : Bar<any, string>
20+
>Foo : Bar<any, Y>
2121
>1 : 1
2222

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [instantiatedTypeAliasDisplay.ts]
2+
3+
// Repros from #12066
4+
5+
interface X<A> {
6+
a: A;
7+
}
8+
interface Y<B> {
9+
b: B;
10+
}
11+
type Z<A, B> = X<A> | Y<B>;
12+
13+
declare function f1<A>(): Z<A, number>;
14+
declare function f2<A, B, C, D, E>(a: A, b: B, c: C, d: D): Z<A, string[]>;
15+
16+
const x1 = f1<string>(); // Z<string, number>
17+
const x2 = f2({}, {}, {}, {}); // Z<{}, string[]>
18+
19+
//// [instantiatedTypeAliasDisplay.js]
20+
// Repros from #12066
21+
var x1 = f1(); // Z<string, number>
22+
var x2 = f2({}, {}, {}, {}); // Z<{}, string[]>
23+
24+
25+
//// [instantiatedTypeAliasDisplay.d.ts]
26+
interface X<A> {
27+
a: A;
28+
}
29+
interface Y<B> {
30+
b: B;
31+
}
32+
declare type Z<A, B> = X<A> | Y<B>;
33+
declare function f1<A>(): Z<A, number>;
34+
declare function f2<A, B, C, D, E>(a: A, b: B, c: C, d: D): Z<A, string[]>;
35+
declare const x1: Z<string, number>;
36+
declare const x2: Z<{}, string[]>;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
=== tests/cases/compiler/instantiatedTypeAliasDisplay.ts ===
2+
3+
// Repros from #12066
4+
5+
interface X<A> {
6+
>X : Symbol(X, Decl(instantiatedTypeAliasDisplay.ts, 0, 0))
7+
>A : Symbol(A, Decl(instantiatedTypeAliasDisplay.ts, 3, 12))
8+
9+
a: A;
10+
>a : Symbol(X.a, Decl(instantiatedTypeAliasDisplay.ts, 3, 16))
11+
>A : Symbol(A, Decl(instantiatedTypeAliasDisplay.ts, 3, 12))
12+
}
13+
interface Y<B> {
14+
>Y : Symbol(Y, Decl(instantiatedTypeAliasDisplay.ts, 5, 1))
15+
>B : Symbol(B, Decl(instantiatedTypeAliasDisplay.ts, 6, 12))
16+
17+
b: B;
18+
>b : Symbol(Y.b, Decl(instantiatedTypeAliasDisplay.ts, 6, 16))
19+
>B : Symbol(B, Decl(instantiatedTypeAliasDisplay.ts, 6, 12))
20+
}
21+
type Z<A, B> = X<A> | Y<B>;
22+
>Z : Symbol(Z, Decl(instantiatedTypeAliasDisplay.ts, 8, 1))
23+
>A : Symbol(A, Decl(instantiatedTypeAliasDisplay.ts, 9, 7))
24+
>B : Symbol(B, Decl(instantiatedTypeAliasDisplay.ts, 9, 9))
25+
>X : Symbol(X, Decl(instantiatedTypeAliasDisplay.ts, 0, 0))
26+
>A : Symbol(A, Decl(instantiatedTypeAliasDisplay.ts, 9, 7))
27+
>Y : Symbol(Y, Decl(instantiatedTypeAliasDisplay.ts, 5, 1))
28+
>B : Symbol(B, Decl(instantiatedTypeAliasDisplay.ts, 9, 9))
29+
30+
declare function f1<A>(): Z<A, number>;
31+
>f1 : Symbol(f1, Decl(instantiatedTypeAliasDisplay.ts, 9, 27))
32+
>A : Symbol(A, Decl(instantiatedTypeAliasDisplay.ts, 11, 20))
33+
>Z : Symbol(Z, Decl(instantiatedTypeAliasDisplay.ts, 8, 1))
34+
>A : Symbol(A, Decl(instantiatedTypeAliasDisplay.ts, 11, 20))
35+
36+
declare function f2<A, B, C, D, E>(a: A, b: B, c: C, d: D): Z<A, string[]>;
37+
>f2 : Symbol(f2, Decl(instantiatedTypeAliasDisplay.ts, 11, 39))
38+
>A : Symbol(A, Decl(instantiatedTypeAliasDisplay.ts, 12, 20))
39+
>B : Symbol(B, Decl(instantiatedTypeAliasDisplay.ts, 12, 22))
40+
>C : Symbol(C, Decl(instantiatedTypeAliasDisplay.ts, 12, 25))
41+
>D : Symbol(D, Decl(instantiatedTypeAliasDisplay.ts, 12, 28))
42+
>E : Symbol(E, Decl(instantiatedTypeAliasDisplay.ts, 12, 31))
43+
>a : Symbol(a, Decl(instantiatedTypeAliasDisplay.ts, 12, 35))
44+
>A : Symbol(A, Decl(instantiatedTypeAliasDisplay.ts, 12, 20))
45+
>b : Symbol(b, Decl(instantiatedTypeAliasDisplay.ts, 12, 40))
46+
>B : Symbol(B, Decl(instantiatedTypeAliasDisplay.ts, 12, 22))
47+
>c : Symbol(c, Decl(instantiatedTypeAliasDisplay.ts, 12, 46))
48+
>C : Symbol(C, Decl(instantiatedTypeAliasDisplay.ts, 12, 25))
49+
>d : Symbol(d, Decl(instantiatedTypeAliasDisplay.ts, 12, 52))
50+
>D : Symbol(D, Decl(instantiatedTypeAliasDisplay.ts, 12, 28))
51+
>Z : Symbol(Z, Decl(instantiatedTypeAliasDisplay.ts, 8, 1))
52+
>A : Symbol(A, Decl(instantiatedTypeAliasDisplay.ts, 12, 20))
53+
54+
const x1 = f1<string>(); // Z<string, number>
55+
>x1 : Symbol(x1, Decl(instantiatedTypeAliasDisplay.ts, 14, 5))
56+
>f1 : Symbol(f1, Decl(instantiatedTypeAliasDisplay.ts, 9, 27))
57+
58+
const x2 = f2({}, {}, {}, {}); // Z<{}, string[]>
59+
>x2 : Symbol(x2, Decl(instantiatedTypeAliasDisplay.ts, 15, 5))
60+
>f2 : Symbol(f2, Decl(instantiatedTypeAliasDisplay.ts, 11, 39))
61+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
=== tests/cases/compiler/instantiatedTypeAliasDisplay.ts ===
2+
3+
// Repros from #12066
4+
5+
interface X<A> {
6+
>X : X<A>
7+
>A : A
8+
9+
a: A;
10+
>a : A
11+
>A : A
12+
}
13+
interface Y<B> {
14+
>Y : Y<B>
15+
>B : B
16+
17+
b: B;
18+
>b : B
19+
>B : B
20+
}
21+
type Z<A, B> = X<A> | Y<B>;
22+
>Z : Z<A, B>
23+
>A : A
24+
>B : B
25+
>X : X<A>
26+
>A : A
27+
>Y : Y<B>
28+
>B : B
29+
30+
declare function f1<A>(): Z<A, number>;
31+
>f1 : <A>() => Z<A, number>
32+
>A : A
33+
>Z : Z<A, B>
34+
>A : A
35+
36+
declare function f2<A, B, C, D, E>(a: A, b: B, c: C, d: D): Z<A, string[]>;
37+
>f2 : <A, B, C, D, E>(a: A, b: B, c: C, d: D) => Z<A, string[]>
38+
>A : A
39+
>B : B
40+
>C : C
41+
>D : D
42+
>E : E
43+
>a : A
44+
>A : A
45+
>b : B
46+
>B : B
47+
>c : C
48+
>C : C
49+
>d : D
50+
>D : D
51+
>Z : Z<A, B>
52+
>A : A
53+
54+
const x1 = f1<string>(); // Z<string, number>
55+
>x1 : Z<string, number>
56+
>f1<string>() : Z<string, number>
57+
>f1 : <A>() => Z<A, number>
58+
59+
const x2 = f2({}, {}, {}, {}); // Z<{}, string[]>
60+
>x2 : Z<{}, string[]>
61+
>f2({}, {}, {}, {}) : Z<{}, string[]>
62+
>f2 : <A, B, C, D, E>(a: A, b: B, c: C, d: D) => Z<A, string[]>
63+
>{} : {}
64+
>{} : {}
65+
>{} : {}
66+
>{} : {}
67+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @declaration: true
2+
3+
// Repros from #12066
4+
5+
interface X<A> {
6+
a: A;
7+
}
8+
interface Y<B> {
9+
b: B;
10+
}
11+
type Z<A, B> = X<A> | Y<B>;
12+
13+
declare function f1<A>(): Z<A, number>;
14+
declare function f2<A, B, C, D, E>(a: A, b: B, c: C, d: D): Z<A, string[]>;
15+
16+
const x1 = f1<string>(); // Z<string, number>
17+
const x2 = f2({}, {}, {}, {}); // Z<{}, string[]>

0 commit comments

Comments
 (0)