Skip to content

Commit 4638c68

Browse files
authored
Properly reduce intersections of string literal and template literal types (#41162)
* Properly reduce single element intersections * Add regression test * Accept new baselines
1 parent 15cec9d commit 4638c68

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13373,6 +13373,9 @@ namespace ts {
1337313373
includes & TypeFlags.VoidLike && includes & (TypeFlags.DisjointDomains & ~TypeFlags.VoidLike)) {
1337413374
return neverType;
1337513375
}
13376+
if (includes & TypeFlags.TemplateLiteral && includes & TypeFlags.StringLiteral && extractRedundantTemplateLiterals(typeSet)) {
13377+
return neverType;
13378+
}
1337613379
if (includes & TypeFlags.Any) {
1337713380
return includes & TypeFlags.IncludesWildcard ? wildcardType : anyType;
1337813381
}
@@ -13424,12 +13427,7 @@ namespace ts {
1342413427
}
1342513428
}
1342613429
else {
13427-
if (includes & TypeFlags.TemplateLiteral && includes & TypeFlags.StringLiteral && extractRedundantTemplateLiterals(typeSet)) {
13428-
result = neverType;
13429-
}
13430-
else {
13431-
result = createIntersectionType(typeSet, aliasSymbol, aliasTypeArguments);
13432-
}
13430+
result = createIntersectionType(typeSet, aliasSymbol, aliasTypeArguments);
1343313431
}
1343413432
intersectionTypes.set(id, result);
1343513433
}

tests/baselines/reference/templateLiteralTypesPatterns.errors.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,10 @@ tests/cases/conformance/types/literal/templateLiteralTypesPatterns.ts(160,7): er
332332
const exampleBad: B = "anything"; // fails
333333
~~~~~~~~~~
334334
!!! error TS2322: Type '"anything"' is not assignable to type '`${number} ${number}`'.
335-
const exampleGood: B = "1 2"; // ok
335+
const exampleGood: B = "1 2"; // ok
336+
337+
// Repro from #41161
338+
339+
var aa: '0';
340+
var aa: '0' & `${number}`;
341+

tests/baselines/reference/templateLiteralTypesPatterns.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,13 @@ const shouldWork2: AGen<string> = null as any as AGen<number>;
159159
type A = `${number}`;
160160
type B = `${A} ${A}`;
161161
const exampleBad: B = "anything"; // fails
162-
const exampleGood: B = "1 2"; // ok
162+
const exampleGood: B = "1 2"; // ok
163+
164+
// Repro from #41161
165+
166+
var aa: '0';
167+
var aa: '0' & `${number}`;
168+
163169

164170
//// [templateLiteralTypesPatterns.js]
165171
"use strict";
@@ -280,3 +286,6 @@ var shouldWork1 = null;
280286
var shouldWork2 = null;
281287
var exampleBad = "anything"; // fails
282288
var exampleGood = "1 2"; // ok
289+
// Repro from #41161
290+
var aa;
291+
var aa;

tests/baselines/reference/templateLiteralTypesPatterns.symbols

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,11 @@ const exampleGood: B = "1 2"; // ok
393393
>exampleGood : Symbol(exampleGood, Decl(templateLiteralTypesPatterns.ts, 160, 5))
394394
>B : Symbol(B, Decl(templateLiteralTypesPatterns.ts, 157, 21))
395395

396+
// Repro from #41161
397+
398+
var aa: '0';
399+
>aa : Symbol(aa, Decl(templateLiteralTypesPatterns.ts, 164, 3), Decl(templateLiteralTypesPatterns.ts, 165, 3))
400+
401+
var aa: '0' & `${number}`;
402+
>aa : Symbol(aa, Decl(templateLiteralTypesPatterns.ts, 164, 3), Decl(templateLiteralTypesPatterns.ts, 165, 3))
403+

tests/baselines/reference/templateLiteralTypesPatterns.types

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,11 @@ const exampleGood: B = "1 2"; // ok
552552
>exampleGood : `${number} ${number}`
553553
>"1 2" : "1 2"
554554

555+
// Repro from #41161
556+
557+
var aa: '0';
558+
>aa : "0"
559+
560+
var aa: '0' & `${number}`;
561+
>aa : "0"
562+

tests/cases/conformance/types/literal/templateLiteralTypesPatterns.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,9 @@ const shouldWork2: AGen<string> = null as any as AGen<number>;
159159
type A = `${number}`;
160160
type B = `${A} ${A}`;
161161
const exampleBad: B = "anything"; // fails
162-
const exampleGood: B = "1 2"; // ok
162+
const exampleGood: B = "1 2"; // ok
163+
164+
// Repro from #41161
165+
166+
var aa: '0';
167+
var aa: '0' & `${number}`;

0 commit comments

Comments
 (0)