Skip to content

Commit bb599af

Browse files
committed
Implement new fix for intersections
1 parent 8b1f1e5 commit bb599af

5 files changed

+43
-11
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17019,7 +17019,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1701917019
if (!addSpans((t as TemplateLiteralType).texts, (t as TemplateLiteralType).types)) return false;
1702017020
text += texts[i + 1];
1702117021
}
17022-
else if (isGenericIndexType(t) || isPatternLiteralPlaceholderType(t)) {
17022+
else if (isGenericIndexType(t) || isPatternLiteralPlaceholderType(t) || t.flags & TypeFlags.Intersection) {
1702317023
newTypes.push(t);
1702417024
newTexts.push(text);
1702517025
text = texts[i + 1];
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
tests/cases/compiler/deeplyNestedTemplateLiteralIntersection.ts(20,11): error TS2590: Expression produces a union type that is too complex to represent.
2+
3+
4+
==== tests/cases/compiler/deeplyNestedTemplateLiteralIntersection.ts (1 errors) ====
5+
type R = `${number}a` & {
6+
_thing: true;
7+
};
8+
9+
type _S = "1" | "2" | "3" | "4" | "5" | "6";
10+
11+
type S = `${_S}${_S}${_S}`;
12+
13+
14+
type T = R | S;
15+
type X = `${T} ${T}`;
16+
17+
export type Props = Partial<{
18+
x: X;
19+
}>;
20+
21+
const a1: Props = {};
22+
const a2: Props = {};
23+
24+
const b = { ...a1, ...a2 };
25+
~~~~~~~~~~~~~~~~
26+
!!! error TS2590: Expression produces a union type that is too complex to represent.
27+
28+
export { b };
29+

tests/baselines/reference/templateLiteralIntersection.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type OriginA1 = `${A}`
1616
>OriginA1 : "a"
1717

1818
type OriginA2 = `${MixA}`
19-
>OriginA2 : string
19+
>OriginA2 : `${MixA}`
2020

2121
type B = `${typeof a}`
2222
>B : "a"
@@ -30,14 +30,14 @@ type OriginB1 = `${B}`
3030
>OriginB1 : "a"
3131

3232
type OriginB2 = `${MixB}`
33-
>OriginB2 : string
33+
>OriginB2 : `${MixB}`
3434

3535
type MixC = { foo: string } & A
3636
>MixC : { foo: string; } & "a"
3737
>foo : string
3838

3939
type OriginC = `${MixC}`
40-
>OriginC : string
40+
>OriginC : `${MixC}`
4141

4242
type MixD<T extends string> =
4343
>MixD : `${T & { foo: string; }}`
@@ -46,7 +46,7 @@ type MixD<T extends string> =
4646
>foo : string
4747

4848
type OriginD = `${MixD<A & { foo: string }> & { foo: string }}`;
49-
>OriginD : string
49+
>OriginD : `${`${"a" & { foo: string; } & { foo: string; }}` & { foo: string; }}`
5050
>foo : string
5151
>foo : string
5252

tests/baselines/reference/templateLiteralIntersection2.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
tests/cases/compiler/templateLiteralIntersection2.ts(7,12): error TS2345: Argument of type '"foo/bar"' is not assignable to parameter of type '`${Path}/${Path}`'.
12
tests/cases/compiler/templateLiteralIntersection2.ts(20,10): error TS2345: Argument of type '""' is not assignable to parameter of type '`a${string}` & `${string}a`'.
23
Type '""' is not assignable to type '`a${string}`'.
34
tests/cases/compiler/templateLiteralIntersection2.ts(22,10): error TS2345: Argument of type '"ab"' is not assignable to parameter of type '`a${string}` & `${string}a`'.
45
Type '"ab"' is not assignable to type '`${string}a`'.
56

67

7-
==== tests/cases/compiler/templateLiteralIntersection2.ts (2 errors) ====
8+
==== tests/cases/compiler/templateLiteralIntersection2.ts (3 errors) ====
89
type Path = string & { _pathBrand: any };
910

1011
type JoinedPath = `${Path}/${Path}`;
1112

1213
declare function joinedPath(p: JoinedPath): void;
1314

1415
joinedPath("foo/bar");
16+
~~~~~~~~~
17+
!!! error TS2345: Argument of type '"foo/bar"' is not assignable to parameter of type '`${Path}/${Path}`'.
1518

1619
declare const somePath: Path;
1720

tests/baselines/reference/templateLiteralIntersection2.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ type Path = string & { _pathBrand: any };
44
>_pathBrand : any
55

66
type JoinedPath = `${Path}/${Path}`;
7-
>JoinedPath : string
7+
>JoinedPath : `${Path}/${Path}`
88

99
declare function joinedPath(p: JoinedPath): void;
1010
>joinedPath : (p: JoinedPath) => void
11-
>p : string
11+
>p : `${Path}/${Path}`
1212

1313
joinedPath("foo/bar");
1414
>joinedPath("foo/bar") : void
15-
>joinedPath : (p: string) => void
15+
>joinedPath : (p: `${Path}/${Path}`) => void
1616
>"foo/bar" : "foo/bar"
1717

1818
declare const somePath: Path;
1919
>somePath : Path
2020

2121
joinedPath(`${somePath}/${somePath}`);
2222
>joinedPath(`${somePath}/${somePath}`) : void
23-
>joinedPath : (p: string) => void
24-
>`${somePath}/${somePath}` : string
23+
>joinedPath : (p: `${Path}/${Path}`) => void
24+
>`${somePath}/${somePath}` : `${Path}/${Path}`
2525
>somePath : Path
2626
>somePath : Path
2727

0 commit comments

Comments
 (0)