Skip to content

Commit c89b1eb

Browse files
authored
Merge pull request #12386 from mariusschulz/union-and-intersection-types-with-leading-operator
Union and intersection types with leading operator
2 parents 5dd4c9e + d040db4 commit c89b1eb

9 files changed

+107
-0
lines changed

src/compiler/parser.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2558,6 +2558,8 @@ namespace ts {
25582558
case SyntaxKind.OpenBraceToken:
25592559
case SyntaxKind.OpenBracketToken:
25602560
case SyntaxKind.LessThanToken:
2561+
case SyntaxKind.BarToken:
2562+
case SyntaxKind.AmpersandToken:
25612563
case SyntaxKind.NewKeyword:
25622564
case SyntaxKind.StringLiteral:
25632565
case SyntaxKind.NumericLiteral:
@@ -2617,6 +2619,7 @@ namespace ts {
26172619
}
26182620

26192621
function parseUnionOrIntersectionType(kind: SyntaxKind, parseConstituentType: () => TypeNode, operator: SyntaxKind): TypeNode {
2622+
parseOptional(operator);
26202623
let type = parseConstituentType();
26212624
if (token() === operator) {
26222625
const types = createNodeArray<TypeNode>([type], type.pos);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [intersectionTypeWithLeadingOperator.ts]
2+
type A = & string;
3+
type B =
4+
& { foo: string }
5+
& { bar: number };
6+
7+
type C = [& { foo: 1 } & { bar: 2 }, & { foo: 3 } & { bar: 4 }];
8+
9+
10+
//// [intersectionTypeWithLeadingOperator.js]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/intersectionTypeWithLeadingOperator.ts ===
2+
type A = & string;
3+
>A : Symbol(A, Decl(intersectionTypeWithLeadingOperator.ts, 0, 0))
4+
5+
type B =
6+
>B : Symbol(B, Decl(intersectionTypeWithLeadingOperator.ts, 0, 18))
7+
8+
& { foo: string }
9+
>foo : Symbol(foo, Decl(intersectionTypeWithLeadingOperator.ts, 2, 5))
10+
11+
& { bar: number };
12+
>bar : Symbol(bar, Decl(intersectionTypeWithLeadingOperator.ts, 3, 5))
13+
14+
type C = [& { foo: 1 } & { bar: 2 }, & { foo: 3 } & { bar: 4 }];
15+
>C : Symbol(C, Decl(intersectionTypeWithLeadingOperator.ts, 3, 20))
16+
>foo : Symbol(foo, Decl(intersectionTypeWithLeadingOperator.ts, 5, 13))
17+
>bar : Symbol(bar, Decl(intersectionTypeWithLeadingOperator.ts, 5, 26))
18+
>foo : Symbol(foo, Decl(intersectionTypeWithLeadingOperator.ts, 5, 40))
19+
>bar : Symbol(bar, Decl(intersectionTypeWithLeadingOperator.ts, 5, 53))
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/intersectionTypeWithLeadingOperator.ts ===
2+
type A = & string;
3+
>A : string
4+
5+
type B =
6+
>B : B
7+
8+
& { foo: string }
9+
>foo : string
10+
11+
& { bar: number };
12+
>bar : number
13+
14+
type C = [& { foo: 1 } & { bar: 2 }, & { foo: 3 } & { bar: 4 }];
15+
>C : [{ foo: 1; } & { bar: 2; }, { foo: 3; } & { bar: 4; }]
16+
>foo : 1
17+
>bar : 2
18+
>foo : 3
19+
>bar : 4
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [unionTypeWithLeadingOperator.ts]
2+
type A = | string;
3+
type B =
4+
| { type: "INCREMENT" }
5+
| { type: "DECREMENT" };
6+
7+
type C = [| 0 | 1, | "foo" | "bar"];
8+
9+
10+
//// [unionTypeWithLeadingOperator.js]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/unionTypeWithLeadingOperator.ts ===
2+
type A = | string;
3+
>A : Symbol(A, Decl(unionTypeWithLeadingOperator.ts, 0, 0))
4+
5+
type B =
6+
>B : Symbol(B, Decl(unionTypeWithLeadingOperator.ts, 0, 18))
7+
8+
| { type: "INCREMENT" }
9+
>type : Symbol(type, Decl(unionTypeWithLeadingOperator.ts, 2, 5))
10+
11+
| { type: "DECREMENT" };
12+
>type : Symbol(type, Decl(unionTypeWithLeadingOperator.ts, 3, 5))
13+
14+
type C = [| 0 | 1, | "foo" | "bar"];
15+
>C : Symbol(C, Decl(unionTypeWithLeadingOperator.ts, 3, 26))
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/unionTypeWithLeadingOperator.ts ===
2+
type A = | string;
3+
>A : string
4+
5+
type B =
6+
>B : B
7+
8+
| { type: "INCREMENT" }
9+
>type : "INCREMENT"
10+
11+
| { type: "DECREMENT" };
12+
>type : "DECREMENT"
13+
14+
type C = [| 0 | 1, | "foo" | "bar"];
15+
>C : [0 | 1, "foo" | "bar"]
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type A = & string;
2+
type B =
3+
& { foo: string }
4+
& { bar: number };
5+
6+
type C = [& { foo: 1 } & { bar: 2 }, & { foo: 3 } & { bar: 4 }];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type A = | string;
2+
type B =
3+
| { type: "INCREMENT" }
4+
| { type: "DECREMENT" };
5+
6+
type C = [| 0 | 1, | "foo" | "bar"];

0 commit comments

Comments
 (0)