Skip to content

Commit

Permalink
Properly reduce intersections of string literal and template literal …
Browse files Browse the repository at this point in the history
…types (#41162)

* Properly reduce single element intersections

* Add regression test

* Accept new baselines
  • Loading branch information
ahejlsberg committed Oct 19, 2020
1 parent 15cec9d commit 4638c68
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
10 changes: 4 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

11 changes: 10 additions & 1 deletion tests/baselines/reference/templateLiteralTypesPatterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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))

8 changes: 8 additions & 0 deletions tests/baselines/reference/templateLiteralTypesPatterns.types
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

0 comments on commit 4638c68

Please sign in to comment.