Skip to content

Commit 547856a

Browse files
authored
Strip literal freshness from type queries (#25471)
* Strip literal freshness from type queries * Rename to fix typo
1 parent ea73ee7 commit 547856a

File tree

5 files changed

+155
-1
lines changed

5 files changed

+155
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8165,7 +8165,7 @@ namespace ts {
81658165
// The expression is processed as an identifier expression (section 4.3)
81668166
// or property access expression(section 4.10),
81678167
// the widened type(section 3.9) of which becomes the result.
8168-
links.resolvedType = getWidenedType(checkExpression(node.exprName));
8168+
links.resolvedType = getRegularTypeOfLiteralType(getWidenedType(checkExpression(node.exprName)));
81698169
}
81708170
return links.resolvedType;
81718171
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [typeofStripsFreshness.ts]
2+
interface Collection<T> {
3+
elems: T[];
4+
}
5+
interface CollectionStatic {
6+
new <T>(): Collection<T>;
7+
}
8+
declare const Collection: CollectionStatic;
9+
10+
const ALL = "all";
11+
type All = typeof ALL;
12+
13+
const result: Collection<All> = new Collection();
14+
15+
const ANOTHER = "another";
16+
type Another = typeof ANOTHER;
17+
18+
type Both = Another | All;
19+
20+
const result2: Collection<Both> = new Collection();
21+
22+
23+
//// [typeofStripsFreshness.js]
24+
var ALL = "all";
25+
var result = new Collection();
26+
var ANOTHER = "another";
27+
var result2 = new Collection();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
=== tests/cases/compiler/typeofStripsFreshness.ts ===
2+
interface Collection<T> {
3+
>Collection : Symbol(Collection, Decl(typeofStripsFreshness.ts, 0, 0), Decl(typeofStripsFreshness.ts, 6, 13))
4+
>T : Symbol(T, Decl(typeofStripsFreshness.ts, 0, 21))
5+
6+
elems: T[];
7+
>elems : Symbol(Collection.elems, Decl(typeofStripsFreshness.ts, 0, 25))
8+
>T : Symbol(T, Decl(typeofStripsFreshness.ts, 0, 21))
9+
}
10+
interface CollectionStatic {
11+
>CollectionStatic : Symbol(CollectionStatic, Decl(typeofStripsFreshness.ts, 2, 1))
12+
13+
new <T>(): Collection<T>;
14+
>T : Symbol(T, Decl(typeofStripsFreshness.ts, 4, 9))
15+
>Collection : Symbol(Collection, Decl(typeofStripsFreshness.ts, 0, 0), Decl(typeofStripsFreshness.ts, 6, 13))
16+
>T : Symbol(T, Decl(typeofStripsFreshness.ts, 4, 9))
17+
}
18+
declare const Collection: CollectionStatic;
19+
>Collection : Symbol(Collection, Decl(typeofStripsFreshness.ts, 0, 0), Decl(typeofStripsFreshness.ts, 6, 13))
20+
>CollectionStatic : Symbol(CollectionStatic, Decl(typeofStripsFreshness.ts, 2, 1))
21+
22+
const ALL = "all";
23+
>ALL : Symbol(ALL, Decl(typeofStripsFreshness.ts, 8, 5))
24+
25+
type All = typeof ALL;
26+
>All : Symbol(All, Decl(typeofStripsFreshness.ts, 8, 18))
27+
>ALL : Symbol(ALL, Decl(typeofStripsFreshness.ts, 8, 5))
28+
29+
const result: Collection<All> = new Collection();
30+
>result : Symbol(result, Decl(typeofStripsFreshness.ts, 11, 5))
31+
>Collection : Symbol(Collection, Decl(typeofStripsFreshness.ts, 0, 0), Decl(typeofStripsFreshness.ts, 6, 13))
32+
>All : Symbol(All, Decl(typeofStripsFreshness.ts, 8, 18))
33+
>Collection : Symbol(Collection, Decl(typeofStripsFreshness.ts, 0, 0), Decl(typeofStripsFreshness.ts, 6, 13))
34+
35+
const ANOTHER = "another";
36+
>ANOTHER : Symbol(ANOTHER, Decl(typeofStripsFreshness.ts, 13, 5))
37+
38+
type Another = typeof ANOTHER;
39+
>Another : Symbol(Another, Decl(typeofStripsFreshness.ts, 13, 26))
40+
>ANOTHER : Symbol(ANOTHER, Decl(typeofStripsFreshness.ts, 13, 5))
41+
42+
type Both = Another | All;
43+
>Both : Symbol(Both, Decl(typeofStripsFreshness.ts, 14, 30))
44+
>Another : Symbol(Another, Decl(typeofStripsFreshness.ts, 13, 26))
45+
>All : Symbol(All, Decl(typeofStripsFreshness.ts, 8, 18))
46+
47+
const result2: Collection<Both> = new Collection();
48+
>result2 : Symbol(result2, Decl(typeofStripsFreshness.ts, 18, 5))
49+
>Collection : Symbol(Collection, Decl(typeofStripsFreshness.ts, 0, 0), Decl(typeofStripsFreshness.ts, 6, 13))
50+
>Both : Symbol(Both, Decl(typeofStripsFreshness.ts, 14, 30))
51+
>Collection : Symbol(Collection, Decl(typeofStripsFreshness.ts, 0, 0), Decl(typeofStripsFreshness.ts, 6, 13))
52+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
=== tests/cases/compiler/typeofStripsFreshness.ts ===
2+
interface Collection<T> {
3+
>Collection : Collection<T>
4+
>T : T
5+
6+
elems: T[];
7+
>elems : T[]
8+
>T : T
9+
}
10+
interface CollectionStatic {
11+
>CollectionStatic : CollectionStatic
12+
13+
new <T>(): Collection<T>;
14+
>T : T
15+
>Collection : Collection<T>
16+
>T : T
17+
}
18+
declare const Collection: CollectionStatic;
19+
>Collection : CollectionStatic
20+
>CollectionStatic : CollectionStatic
21+
22+
const ALL = "all";
23+
>ALL : "all"
24+
>"all" : "all"
25+
26+
type All = typeof ALL;
27+
>All : "all"
28+
>ALL : "all"
29+
30+
const result: Collection<All> = new Collection();
31+
>result : Collection<"all">
32+
>Collection : Collection<T>
33+
>All : "all"
34+
>new Collection() : Collection<"all">
35+
>Collection : CollectionStatic
36+
37+
const ANOTHER = "another";
38+
>ANOTHER : "another"
39+
>"another" : "another"
40+
41+
type Another = typeof ANOTHER;
42+
>Another : "another"
43+
>ANOTHER : "another"
44+
45+
type Both = Another | All;
46+
>Both : Both
47+
>Another : "another"
48+
>All : "all"
49+
50+
const result2: Collection<Both> = new Collection();
51+
>result2 : Collection<Both>
52+
>Collection : Collection<T>
53+
>Both : Both
54+
>new Collection() : Collection<Both>
55+
>Collection : CollectionStatic
56+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
interface Collection<T> {
2+
elems: T[];
3+
}
4+
interface CollectionStatic {
5+
new <T>(): Collection<T>;
6+
}
7+
declare const Collection: CollectionStatic;
8+
9+
const ALL = "all";
10+
type All = typeof ALL;
11+
12+
const result: Collection<All> = new Collection();
13+
14+
const ANOTHER = "another";
15+
type Another = typeof ANOTHER;
16+
17+
type Both = Another | All;
18+
19+
const result2: Collection<Both> = new Collection();

0 commit comments

Comments
 (0)