• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//// [intersectionTypeNormalization.ts]
2interface A { a: string }
3interface B { b: string }
4interface C { c: string }
5interface D { d: string }
6
7// Identical ways of writing the same type
8type X1 = (A | B) & (C | D);
9type X2 = A & (C | D) | B & (C | D)
10type X3 = A & C | A & D | B & C | B & D;
11
12var x: X1;
13var x: X2;
14var x: X3;
15
16interface X { x: string }
17interface Y { y: string }
18
19// Identical ways of writing the same type
20type Y1 = (A | X & Y) & (C | D);
21type Y2 = A & (C | D) | X & Y & (C | D)
22type Y3 = A & C | A & D | X & Y & C | X & Y & D;
23
24var y: Y1;
25var y: Y2;
26var y: Y3;
27
28interface M { m: string }
29interface N { n: string }
30
31// Identical ways of writing the same type
32type Z1 = (A | X & (M | N)) & (C | D);
33type Z2 = A & (C | D) | X & (M | N) & (C | D)
34type Z3 = A & C | A & D | X & (M | N) & C | X & (M | N) & D;
35type Z4 = A & C | A & D | X & M & C | X & N & C | X & M & D | X & N & D;
36
37var z: Z1;
38var z: Z2;
39var z: Z3;
40var z: Z4;
41
42// Repro from #9919
43
44type ToString = {
45    toString(): string;
46}
47
48type BoxedValue = { kind: 'int',    num: number }
49                | { kind: 'string', str: string }
50
51type IntersectionFail = BoxedValue & ToString
52
53type IntersectionInline = { kind: 'int',    num: number } & ToString
54                        | { kind: 'string', str: string } & ToString
55
56function getValueAsString(value: IntersectionFail): string {
57    if (value.kind === 'int') {
58        return '' + value.num;
59    }
60    return value.str;
61}
62
63// Repro from #12535
64
65namespace enums {
66    export const enum A {
67        a1,
68        a2,
69        a3,
70       // ... elements omitted for the sake of clarity
71        a75,
72        a76,
73        a77,
74    }
75    export const enum B {
76        b1,
77        b2,
78       // ... elements omitted for the sake of clarity
79        b86,
80        b87,
81    }
82    export const enum C {
83        c1,
84        c2,
85       // ... elements omitted for the sake of clarity
86        c210,
87        c211,
88    }
89    export type Genre = A | B | C;
90}
91
92type Foo = {
93    genreId: enums.Genre;
94};
95
96type Bar = {
97    genreId: enums.Genre;
98};
99
100type FooBar = Foo & Bar;
101
102function foo(so: any) {
103    const val = so as FooBar;
104    const isGenre = val.genreId;
105    return isGenre;
106}
107
108//// [intersectionTypeNormalization.js]
109var x;
110var x;
111var x;
112var y;
113var y;
114var y;
115var z;
116var z;
117var z;
118var z;
119function getValueAsString(value) {
120    if (value.kind === 'int') {
121        return '' + value.num;
122    }
123    return value.str;
124}
125function foo(so) {
126    var val = so;
127    var isGenre = val.genreId;
128    return isGenre;
129}
130