Skip to content

Commit a8d3cd6

Browse files
authored
Merge pull request #19697 from Microsoft/fixFreshLiteralsInIntersections
Fix fresh literals in intersections
2 parents add8b49 + 18b5ade commit a8d3cd6

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

src/compiler/checker.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7592,11 +7592,11 @@ namespace ts {
75927592
}
75937593
}
75947594

7595-
// Add the given types to the given type set. Order is preserved, duplicates are removed,
7596-
// and nested types of the given kind are flattened into the set.
7595+
// Add the given types to the given type set. Order is preserved, freshness is removed from literal
7596+
// types, duplicates are removed, and nested types of the given kind are flattened into the set.
75977597
function addTypesToIntersection(typeSet: TypeSet, types: Type[]) {
75987598
for (const type of types) {
7599-
addTypeToIntersection(typeSet, type);
7599+
addTypeToIntersection(typeSet, getRegularTypeOfLiteralType(type));
76007600
}
76017601
}
76027602

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [freshLiteralTypesInIntersections.ts]
2+
// Repro from #19657
3+
4+
declare function func<A extends string, B extends A>(a: A, b: B[]): (ab: A & B) => void;
5+
const q = func("x" as "x" | "y", ["x"]);
6+
q("x");
7+
8+
9+
//// [freshLiteralTypesInIntersections.js]
10+
"use strict";
11+
// Repro from #19657
12+
var q = func("x", ["x"]);
13+
q("x");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/compiler/freshLiteralTypesInIntersections.ts ===
2+
// Repro from #19657
3+
4+
declare function func<A extends string, B extends A>(a: A, b: B[]): (ab: A & B) => void;
5+
>func : Symbol(func, Decl(freshLiteralTypesInIntersections.ts, 0, 0))
6+
>A : Symbol(A, Decl(freshLiteralTypesInIntersections.ts, 2, 22))
7+
>B : Symbol(B, Decl(freshLiteralTypesInIntersections.ts, 2, 39))
8+
>A : Symbol(A, Decl(freshLiteralTypesInIntersections.ts, 2, 22))
9+
>a : Symbol(a, Decl(freshLiteralTypesInIntersections.ts, 2, 53))
10+
>A : Symbol(A, Decl(freshLiteralTypesInIntersections.ts, 2, 22))
11+
>b : Symbol(b, Decl(freshLiteralTypesInIntersections.ts, 2, 58))
12+
>B : Symbol(B, Decl(freshLiteralTypesInIntersections.ts, 2, 39))
13+
>ab : Symbol(ab, Decl(freshLiteralTypesInIntersections.ts, 2, 69))
14+
>A : Symbol(A, Decl(freshLiteralTypesInIntersections.ts, 2, 22))
15+
>B : Symbol(B, Decl(freshLiteralTypesInIntersections.ts, 2, 39))
16+
17+
const q = func("x" as "x" | "y", ["x"]);
18+
>q : Symbol(q, Decl(freshLiteralTypesInIntersections.ts, 3, 5))
19+
>func : Symbol(func, Decl(freshLiteralTypesInIntersections.ts, 0, 0))
20+
21+
q("x");
22+
>q : Symbol(q, Decl(freshLiteralTypesInIntersections.ts, 3, 5))
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/freshLiteralTypesInIntersections.ts ===
2+
// Repro from #19657
3+
4+
declare function func<A extends string, B extends A>(a: A, b: B[]): (ab: A & B) => void;
5+
>func : <A extends string, B extends A>(a: A, b: B[]) => (ab: A & B) => void
6+
>A : A
7+
>B : B
8+
>A : A
9+
>a : A
10+
>A : A
11+
>b : B[]
12+
>B : B
13+
>ab : A & B
14+
>A : A
15+
>B : B
16+
17+
const q = func("x" as "x" | "y", ["x"]);
18+
>q : (ab: "x") => void
19+
>func("x" as "x" | "y", ["x"]) : (ab: "x") => void
20+
>func : <A extends string, B extends A>(a: A, b: B[]) => (ab: A & B) => void
21+
>"x" as "x" | "y" : "x" | "y"
22+
>"x" : "x"
23+
>["x"] : "x"[]
24+
>"x" : "x"
25+
26+
q("x");
27+
>q("x") : void
28+
>q : (ab: "x") => void
29+
>"x" : "x"
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @strict: true
2+
3+
// Repro from #19657
4+
5+
declare function func<A extends string, B extends A>(a: A, b: B[]): (ab: A & B) => void;
6+
const q = func("x" as "x" | "y", ["x"]);
7+
q("x");

0 commit comments

Comments
 (0)