mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-16 06:35:35 -05:00
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:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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}`;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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))
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -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}`;
|
||||
|
||||
Reference in New Issue
Block a user