Skip to content

Commit cbbf2e4

Browse files
authored
Resolve aliases before using getTypereferenceType (microsoft#24594)
1 parent b3a4b72 commit cbbf2e4

5 files changed

+243
-3
lines changed

src/compiler/checker.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -9204,12 +9204,13 @@ namespace ts {
92049204
}
92059205

92069206
function resolveImportSymbolType(node: ImportTypeNode, links: NodeLinks, symbol: Symbol, meaning: SymbolFlags) {
9207-
links.resolvedSymbol = symbol;
9207+
const resolvedSymbol = resolveSymbol(symbol);
9208+
links.resolvedSymbol = resolvedSymbol;
92089209
if (meaning === SymbolFlags.Value) {
9209-
return links.resolvedType = getTypeOfSymbol(symbol);
9210+
return links.resolvedType = getTypeOfSymbol(symbol); // intentionally doesn't use resolved symbol so type is cached as expected on the alias
92109211
}
92119212
else {
9212-
return links.resolvedType = getTypeReferenceType(node, symbol);
9213+
return links.resolvedType = getTypeReferenceType(node, resolvedSymbol); // getTypeReferenceType doesn't handle aliases - it must get the resolved symbol
92139214
}
92149215
}
92159216

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//// [tests/cases/compiler/declarationsForIndirectTypeAliasReference.ts] ////
2+
3+
//// [b.ts]
4+
export {
5+
Hash,
6+
StringHash, StringHash2
7+
};
8+
9+
interface Hash<T> {
10+
[key: string]: T;
11+
}
12+
13+
type StringHash = Hash<string>;
14+
15+
interface StringHash2 extends Hash<string> {}
16+
//// [a.ts]
17+
import {StringHash, StringHash2} from "./b";
18+
19+
export {
20+
doSome
21+
}
22+
23+
const MAP: StringHash = {
24+
a: "a"
25+
};
26+
27+
const MAP2: StringHash2 = {
28+
a: "a"
29+
};
30+
31+
function doSome(arg1: string,
32+
arg2 = MAP,
33+
arg3 = MAP2) {
34+
}
35+
36+
//// [b.js]
37+
"use strict";
38+
exports.__esModule = true;
39+
//// [a.js]
40+
"use strict";
41+
exports.__esModule = true;
42+
var MAP = {
43+
a: "a"
44+
};
45+
var MAP2 = {
46+
a: "a"
47+
};
48+
function doSome(arg1, arg2, arg3) {
49+
if (arg2 === void 0) { arg2 = MAP; }
50+
if (arg3 === void 0) { arg3 = MAP2; }
51+
}
52+
exports.doSome = doSome;
53+
54+
55+
//// [b.d.ts]
56+
export { Hash, StringHash, StringHash2 };
57+
interface Hash<T> {
58+
[key: string]: T;
59+
}
60+
declare type StringHash = Hash<string>;
61+
interface StringHash2 extends Hash<string> {
62+
}
63+
//// [a.d.ts]
64+
import { StringHash2 } from "./b";
65+
export { doSome };
66+
declare function doSome(arg1: string, arg2?: import("./b").Hash<string>, arg3?: StringHash2): void;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
=== tests/cases/compiler/b.ts ===
2+
export {
3+
Hash,
4+
>Hash : Symbol(Hash, Decl(b.ts, 0, 8))
5+
6+
StringHash, StringHash2
7+
>StringHash : Symbol(StringHash, Decl(b.ts, 1, 9))
8+
>StringHash2 : Symbol(StringHash2, Decl(b.ts, 2, 15))
9+
10+
};
11+
12+
interface Hash<T> {
13+
>Hash : Symbol(Hash, Decl(b.ts, 3, 2))
14+
>T : Symbol(T, Decl(b.ts, 5, 15))
15+
16+
[key: string]: T;
17+
>key : Symbol(key, Decl(b.ts, 6, 5))
18+
>T : Symbol(T, Decl(b.ts, 5, 15))
19+
}
20+
21+
type StringHash = Hash<string>;
22+
>StringHash : Symbol(StringHash, Decl(b.ts, 7, 1))
23+
>Hash : Symbol(Hash, Decl(b.ts, 3, 2))
24+
25+
interface StringHash2 extends Hash<string> {}
26+
>StringHash2 : Symbol(StringHash2, Decl(b.ts, 9, 31))
27+
>Hash : Symbol(Hash, Decl(b.ts, 3, 2))
28+
29+
=== tests/cases/compiler/a.ts ===
30+
import {StringHash, StringHash2} from "./b";
31+
>StringHash : Symbol(StringHash, Decl(a.ts, 0, 8))
32+
>StringHash2 : Symbol(StringHash2, Decl(a.ts, 0, 19))
33+
34+
export {
35+
doSome
36+
>doSome : Symbol(doSome, Decl(a.ts, 2, 8))
37+
}
38+
39+
const MAP: StringHash = {
40+
>MAP : Symbol(MAP, Decl(a.ts, 6, 5))
41+
>StringHash : Symbol(StringHash, Decl(a.ts, 0, 8))
42+
43+
a: "a"
44+
>a : Symbol(a, Decl(a.ts, 6, 25))
45+
46+
};
47+
48+
const MAP2: StringHash2 = {
49+
>MAP2 : Symbol(MAP2, Decl(a.ts, 10, 5))
50+
>StringHash2 : Symbol(StringHash2, Decl(a.ts, 0, 19))
51+
52+
a: "a"
53+
>a : Symbol(a, Decl(a.ts, 10, 27))
54+
55+
};
56+
57+
function doSome(arg1: string,
58+
>doSome : Symbol(doSome, Decl(a.ts, 12, 2))
59+
>arg1 : Symbol(arg1, Decl(a.ts, 14, 16))
60+
61+
arg2 = MAP,
62+
>arg2 : Symbol(arg2, Decl(a.ts, 14, 29))
63+
>MAP : Symbol(MAP, Decl(a.ts, 6, 5))
64+
65+
arg3 = MAP2) {
66+
>arg3 : Symbol(arg3, Decl(a.ts, 15, 27))
67+
>MAP2 : Symbol(MAP2, Decl(a.ts, 10, 5))
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
=== tests/cases/compiler/b.ts ===
2+
export {
3+
Hash,
4+
>Hash : any
5+
6+
StringHash, StringHash2
7+
>StringHash : any
8+
>StringHash2 : any
9+
10+
};
11+
12+
interface Hash<T> {
13+
>Hash : Hash<T>
14+
>T : T
15+
16+
[key: string]: T;
17+
>key : string
18+
>T : T
19+
}
20+
21+
type StringHash = Hash<string>;
22+
>StringHash : Hash<string>
23+
>Hash : Hash<T>
24+
25+
interface StringHash2 extends Hash<string> {}
26+
>StringHash2 : StringHash2
27+
>Hash : Hash<T>
28+
29+
=== tests/cases/compiler/a.ts ===
30+
import {StringHash, StringHash2} from "./b";
31+
>StringHash : any
32+
>StringHash2 : any
33+
34+
export {
35+
doSome
36+
>doSome : (arg1: string, arg2?: import("tests/cases/compiler/b").Hash<string>, arg3?: StringHash2) => void
37+
}
38+
39+
const MAP: StringHash = {
40+
>MAP : import("tests/cases/compiler/b").Hash<string>
41+
>StringHash : import("tests/cases/compiler/b").Hash<string>
42+
>{ a: "a"} : { a: string; }
43+
44+
a: "a"
45+
>a : string
46+
>"a" : "a"
47+
48+
};
49+
50+
const MAP2: StringHash2 = {
51+
>MAP2 : StringHash2
52+
>StringHash2 : StringHash2
53+
>{ a: "a"} : { a: string; }
54+
55+
a: "a"
56+
>a : string
57+
>"a" : "a"
58+
59+
};
60+
61+
function doSome(arg1: string,
62+
>doSome : (arg1: string, arg2?: import("tests/cases/compiler/b").Hash<string>, arg3?: StringHash2) => void
63+
>arg1 : string
64+
65+
arg2 = MAP,
66+
>arg2 : import("tests/cases/compiler/b").Hash<string>
67+
>MAP : import("tests/cases/compiler/b").Hash<string>
68+
69+
arg3 = MAP2) {
70+
>arg3 : StringHash2
71+
>MAP2 : StringHash2
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// @declaration: true
2+
// @filename: b.ts
3+
export {
4+
Hash,
5+
StringHash, StringHash2
6+
};
7+
8+
interface Hash<T> {
9+
[key: string]: T;
10+
}
11+
12+
type StringHash = Hash<string>;
13+
14+
interface StringHash2 extends Hash<string> {}
15+
// @filename: a.ts
16+
import {StringHash, StringHash2} from "./b";
17+
18+
export {
19+
doSome
20+
}
21+
22+
const MAP: StringHash = {
23+
a: "a"
24+
};
25+
26+
const MAP2: StringHash2 = {
27+
a: "a"
28+
};
29+
30+
function doSome(arg1: string,
31+
arg2 = MAP,
32+
arg3 = MAP2) {
33+
}

0 commit comments

Comments
 (0)