Merge pull request #19697 from Microsoft/fixFreshLiteralsInIntersections

Fix fresh literals in intersections
This commit is contained in:
Anders Hejlsberg 2017-11-02 17:37:55 -07:00 committed by GitHub
commit a8d3cd6dfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 3 deletions

View File

@ -7592,11 +7592,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));
}
}

View File

@ -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");

View File

@ -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))

View File

@ -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"

View File

@ -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");