Properly reduce intersections of string literal and template literal types (#41162)

* Properly reduce single element intersections

* Add regression test

* Accept new baselines
This commit is contained in:
Anders Hejlsberg
2020-10-19 13:05:29 -07:00
committed by GitHub
parent 15cec9d1f7
commit 4638c685b1
6 changed files with 43 additions and 9 deletions

View File

@@ -13373,6 +13373,9 @@ namespace ts {
includes & TypeFlags.VoidLike && includes & (TypeFlags.DisjointDomains & ~TypeFlags.VoidLike)) {
return neverType;
}
if (includes & TypeFlags.TemplateLiteral && includes & TypeFlags.StringLiteral && extractRedundantTemplateLiterals(typeSet)) {
return neverType;
}
if (includes & TypeFlags.Any) {
return includes & TypeFlags.IncludesWildcard ? wildcardType : anyType;
}
@@ -13424,12 +13427,7 @@ namespace ts {
}
}
else {
if (includes & TypeFlags.TemplateLiteral && includes & TypeFlags.StringLiteral && extractRedundantTemplateLiterals(typeSet)) {
result = neverType;
}
else {
result = createIntersectionType(typeSet, aliasSymbol, aliasTypeArguments);
}
result = createIntersectionType(typeSet, aliasSymbol, aliasTypeArguments);
}
intersectionTypes.set(id, result);
}

View File

@@ -332,4 +332,10 @@ tests/cases/conformance/types/literal/templateLiteralTypesPatterns.ts(160,7): er
const exampleBad: B = "anything"; // fails
~~~~~~~~~~
!!! error TS2322: Type '"anything"' is not assignable to type '`${number} ${number}`'.
const exampleGood: B = "1 2"; // ok
const exampleGood: B = "1 2"; // ok
// Repro from #41161
var aa: '0';
var aa: '0' & `${number}`;

View File

@@ -159,7 +159,13 @@ const shouldWork2: AGen<string> = null as any as AGen<number>;
type A = `${number}`;
type B = `${A} ${A}`;
const exampleBad: B = "anything"; // fails
const exampleGood: B = "1 2"; // ok
const exampleGood: B = "1 2"; // ok
// Repro from #41161
var aa: '0';
var aa: '0' & `${number}`;
//// [templateLiteralTypesPatterns.js]
"use strict";
@@ -280,3 +286,6 @@ var shouldWork1 = null;
var shouldWork2 = null;
var exampleBad = "anything"; // fails
var exampleGood = "1 2"; // ok
// Repro from #41161
var aa;
var aa;

View File

@@ -393,3 +393,11 @@ const exampleGood: B = "1 2"; // ok
>exampleGood : Symbol(exampleGood, Decl(templateLiteralTypesPatterns.ts, 160, 5))
>B : Symbol(B, Decl(templateLiteralTypesPatterns.ts, 157, 21))
// Repro from #41161
var aa: '0';
>aa : Symbol(aa, Decl(templateLiteralTypesPatterns.ts, 164, 3), Decl(templateLiteralTypesPatterns.ts, 165, 3))
var aa: '0' & `${number}`;
>aa : Symbol(aa, Decl(templateLiteralTypesPatterns.ts, 164, 3), Decl(templateLiteralTypesPatterns.ts, 165, 3))

View File

@@ -552,3 +552,11 @@ const exampleGood: B = "1 2"; // ok
>exampleGood : `${number} ${number}`
>"1 2" : "1 2"
// Repro from #41161
var aa: '0';
>aa : "0"
var aa: '0' & `${number}`;
>aa : "0"

View File

@@ -159,4 +159,9 @@ const shouldWork2: AGen<string> = null as any as AGen<number>;
type A = `${number}`;
type B = `${A} ${A}`;
const exampleBad: B = "anything"; // fails
const exampleGood: B = "1 2"; // ok
const exampleGood: B = "1 2"; // ok
// Repro from #41161
var aa: '0';
var aa: '0' & `${number}`;