Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`;