Skip to content

Fix fresh literals in intersections #19697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7589,11 +7589,11 @@ namespace ts {
}
}

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

Expand Down
13 changes: 13 additions & 0 deletions tests/baselines/reference/freshLiteralTypesInIntersections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [freshLiteralTypesInIntersections.ts]
// Repro from #19657

declare function func<A extends string, B extends A>(a: A, b: B[]): (ab: A & B) => void;
const q = func("x" as "x" | "y", ["x"]);
q("x");


//// [freshLiteralTypesInIntersections.js]
"use strict";
// Repro from #19657
var q = func("x", ["x"]);
q("x");
23 changes: 23 additions & 0 deletions tests/baselines/reference/freshLiteralTypesInIntersections.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/freshLiteralTypesInIntersections.ts ===
// Repro from #19657

declare function func<A extends string, B extends A>(a: A, b: B[]): (ab: A & B) => void;
>func : Symbol(func, Decl(freshLiteralTypesInIntersections.ts, 0, 0))
>A : Symbol(A, Decl(freshLiteralTypesInIntersections.ts, 2, 22))
>B : Symbol(B, Decl(freshLiteralTypesInIntersections.ts, 2, 39))
>A : Symbol(A, Decl(freshLiteralTypesInIntersections.ts, 2, 22))
>a : Symbol(a, Decl(freshLiteralTypesInIntersections.ts, 2, 53))
>A : Symbol(A, Decl(freshLiteralTypesInIntersections.ts, 2, 22))
>b : Symbol(b, Decl(freshLiteralTypesInIntersections.ts, 2, 58))
>B : Symbol(B, Decl(freshLiteralTypesInIntersections.ts, 2, 39))
>ab : Symbol(ab, Decl(freshLiteralTypesInIntersections.ts, 2, 69))
>A : Symbol(A, Decl(freshLiteralTypesInIntersections.ts, 2, 22))
>B : Symbol(B, Decl(freshLiteralTypesInIntersections.ts, 2, 39))

const q = func("x" as "x" | "y", ["x"]);
>q : Symbol(q, Decl(freshLiteralTypesInIntersections.ts, 3, 5))
>func : Symbol(func, Decl(freshLiteralTypesInIntersections.ts, 0, 0))

q("x");
>q : Symbol(q, Decl(freshLiteralTypesInIntersections.ts, 3, 5))

30 changes: 30 additions & 0 deletions tests/baselines/reference/freshLiteralTypesInIntersections.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/compiler/freshLiteralTypesInIntersections.ts ===
// Repro from #19657

declare function func<A extends string, B extends A>(a: A, b: B[]): (ab: A & B) => void;
>func : <A extends string, B extends A>(a: A, b: B[]) => (ab: A & B) => void
>A : A
>B : B
>A : A
>a : A
>A : A
>b : B[]
>B : B
>ab : A & B
>A : A
>B : B

const q = func("x" as "x" | "y", ["x"]);
>q : (ab: "x") => void
>func("x" as "x" | "y", ["x"]) : (ab: "x") => void
>func : <A extends string, B extends A>(a: A, b: B[]) => (ab: A & B) => void
>"x" as "x" | "y" : "x" | "y"
>"x" : "x"
>["x"] : "x"[]
>"x" : "x"

q("x");
>q("x") : void
>q : (ab: "x") => void
>"x" : "x"

7 changes: 7 additions & 0 deletions tests/cases/compiler/freshLiteralTypesInIntersections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @strict: true

// Repro from #19657

declare function func<A extends string, B extends A>(a: A, b: B[]): (ab: A & B) => void;
const q = func("x" as "x" | "y", ["x"]);
q("x");