Skip to content

Commit

Permalink
Merge pull request #12386 from mariusschulz/union-and-intersection-ty…
Browse files Browse the repository at this point in the history
…pes-with-leading-operator

Union and intersection types with leading operator
  • Loading branch information
ahejlsberg authored Nov 28, 2016
2 parents 5dd4c9e + d040db4 commit c89b1eb
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2558,6 +2558,8 @@ namespace ts {
case SyntaxKind.OpenBraceToken:
case SyntaxKind.OpenBracketToken:
case SyntaxKind.LessThanToken:
case SyntaxKind.BarToken:
case SyntaxKind.AmpersandToken:
case SyntaxKind.NewKeyword:
case SyntaxKind.StringLiteral:
case SyntaxKind.NumericLiteral:
Expand Down Expand Up @@ -2617,6 +2619,7 @@ namespace ts {
}

function parseUnionOrIntersectionType(kind: SyntaxKind, parseConstituentType: () => TypeNode, operator: SyntaxKind): TypeNode {
parseOptional(operator);
let type = parseConstituentType();
if (token() === operator) {
const types = createNodeArray<TypeNode>([type], type.pos);
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/intersectionTypeWithLeadingOperator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [intersectionTypeWithLeadingOperator.ts]
type A = & string;
type B =
& { foo: string }
& { bar: number };

type C = [& { foo: 1 } & { bar: 2 }, & { foo: 3 } & { bar: 4 }];


//// [intersectionTypeWithLeadingOperator.js]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/intersectionTypeWithLeadingOperator.ts ===
type A = & string;
>A : Symbol(A, Decl(intersectionTypeWithLeadingOperator.ts, 0, 0))

type B =
>B : Symbol(B, Decl(intersectionTypeWithLeadingOperator.ts, 0, 18))

& { foo: string }
>foo : Symbol(foo, Decl(intersectionTypeWithLeadingOperator.ts, 2, 5))

& { bar: number };
>bar : Symbol(bar, Decl(intersectionTypeWithLeadingOperator.ts, 3, 5))

type C = [& { foo: 1 } & { bar: 2 }, & { foo: 3 } & { bar: 4 }];
>C : Symbol(C, Decl(intersectionTypeWithLeadingOperator.ts, 3, 20))
>foo : Symbol(foo, Decl(intersectionTypeWithLeadingOperator.ts, 5, 13))
>bar : Symbol(bar, Decl(intersectionTypeWithLeadingOperator.ts, 5, 26))
>foo : Symbol(foo, Decl(intersectionTypeWithLeadingOperator.ts, 5, 40))
>bar : Symbol(bar, Decl(intersectionTypeWithLeadingOperator.ts, 5, 53))

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/intersectionTypeWithLeadingOperator.ts ===
type A = & string;
>A : string

type B =
>B : B

& { foo: string }
>foo : string

& { bar: number };
>bar : number

type C = [& { foo: 1 } & { bar: 2 }, & { foo: 3 } & { bar: 4 }];
>C : [{ foo: 1; } & { bar: 2; }, { foo: 3; } & { bar: 4; }]
>foo : 1
>bar : 2
>foo : 3
>bar : 4

10 changes: 10 additions & 0 deletions tests/baselines/reference/unionTypeWithLeadingOperator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [unionTypeWithLeadingOperator.ts]
type A = | string;
type B =
| { type: "INCREMENT" }
| { type: "DECREMENT" };

type C = [| 0 | 1, | "foo" | "bar"];


//// [unionTypeWithLeadingOperator.js]
16 changes: 16 additions & 0 deletions tests/baselines/reference/unionTypeWithLeadingOperator.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/unionTypeWithLeadingOperator.ts ===
type A = | string;
>A : Symbol(A, Decl(unionTypeWithLeadingOperator.ts, 0, 0))

type B =
>B : Symbol(B, Decl(unionTypeWithLeadingOperator.ts, 0, 18))

| { type: "INCREMENT" }
>type : Symbol(type, Decl(unionTypeWithLeadingOperator.ts, 2, 5))

| { type: "DECREMENT" };
>type : Symbol(type, Decl(unionTypeWithLeadingOperator.ts, 3, 5))

type C = [| 0 | 1, | "foo" | "bar"];
>C : Symbol(C, Decl(unionTypeWithLeadingOperator.ts, 3, 26))

16 changes: 16 additions & 0 deletions tests/baselines/reference/unionTypeWithLeadingOperator.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/unionTypeWithLeadingOperator.ts ===
type A = | string;
>A : string

type B =
>B : B

| { type: "INCREMENT" }
>type : "INCREMENT"

| { type: "DECREMENT" };
>type : "DECREMENT"

type C = [| 0 | 1, | "foo" | "bar"];
>C : [0 | 1, "foo" | "bar"]

6 changes: 6 additions & 0 deletions tests/cases/compiler/intersectionTypeWithLeadingOperator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type A = & string;
type B =
& { foo: string }
& { bar: number };

type C = [& { foo: 1 } & { bar: 2 }, & { foo: 3 } & { bar: 4 }];
6 changes: 6 additions & 0 deletions tests/cases/compiler/unionTypeWithLeadingOperator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type A = | string;
type B =
| { type: "INCREMENT" }
| { type: "DECREMENT" };

type C = [| 0 | 1, | "foo" | "bar"];

0 comments on commit c89b1eb

Please sign in to comment.