• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//// [assignmentCompatWithDiscriminatedUnion.ts]
2// see 'typeRelatedToDiscriminatedType' in checker.ts:
3
4// IteratorResult
5namespace Example1 {
6    type S = { done: boolean, value: number };
7    type T =
8        | { done: true, value: number }     // T0
9        | { done: false, value: number };   // T1
10
11    declare let s: S;
12    declare let t: T;
13
14    // S is assignable to T0 when S["done"] is true
15    // S is assignable to T1 when S["done"] is false
16    t = s;
17}
18
19// Dropping constituents of T
20namespace Example2 {
21    type S = { a: 0 | 2, b: 4 };
22    type T = { a: 0,     b: 1 | 4 }     // T0
23           | { a: 1,     b: 2 }         // T1
24           | { a: 2,     b: 3 | 4 };    // T2
25    declare let s: S;
26    declare let t: T;
27
28    // S is assignable to T0 when S["a"] is 0
29    // S is assignable to T2 when S["a"] is 2
30    t = s;
31}
32
33// Unmatched discriminants
34namespace Example3 {
35    type S = { a: 0 | 2, b: 4 };
36    type T = { a: 0,     b: 1 | 4 }     // T0
37           | { a: 1,     b: 2 | 4 }     // T1
38           | { a: 2,     b: 3 };        // T2
39    declare let s: S;
40    declare let t: T;
41
42    // S is assignable to T0 when S["a"] is 0
43    // S is *not* assignable to T1 when S["b"] is 4
44    // S is *not* assignable to T2 when S["a"] is 2
45    t = s;
46}
47
48// Unmatched non-discriminants
49namespace Example4 {
50    type S = { a: 0 | 2, b: 4 };
51    type T = { a: 0,     b: 1 | 4 }             // T0
52           | { a: 1,     b: 2 }                 // T1
53           | { a: 2,     b: 3 | 4, c: string }; // T2
54    declare let s: S;
55    declare let t: T;
56
57    // S is assignable to T0 when S["a"] is 0
58    // S is *not* assignable to T2 when S["a"] is 2 as S is missing "c"
59    t = s;
60}
61
62// Maximum discriminant combinations
63namespace Example5 {
64    // NOTE: The maximum number of discriminant type combinations is currently 25.
65    //       3 discriminant properties with 3 types a piece
66    //       is 27 possible combinations.
67    type N = 0 | 1 | 2;
68    type S = { a: N, b: N, c: N };
69    type T = { a: 0, b: N, c: N }
70           | { a: 1, b: N, c: N }
71           | { a: 2, b: N, c: N }
72           | { a: N, b: 0, c: N }
73           | { a: N, b: 1, c: N }
74           | { a: N, b: 2, c: N }
75           | { a: N, b: N, c: 0 }
76           | { a: N, b: N, c: 1 }
77           | { a: N, b: N, c: 2 };
78    declare let s: S;
79    declare let t: T;
80
81    // S *should* be assignable but the number of
82    // combinations is too complex.
83    t = s;
84}
85
86// https://github.com/Microsoft/TypeScript/issues/14865
87namespace GH14865 {
88    type Style1 = {
89        type: "A";
90        data: string;
91    } | {
92        type: "B";
93        data: string;
94    };
95
96    type Style2 = {
97        type: "A" | "B";
98        data: string;
99    }
100
101    const a: Style2 = { type: "A", data: "whatevs" };
102    let b: Style1;
103    a.type; // "A" | "B"
104    b.type; // "A" | "B"
105    b = a; // should be assignable
106}
107
108// https://github.com/Microsoft/TypeScript/issues/30170
109namespace GH30170 {
110    interface Blue {
111        color: 'blue'
112    }
113    interface Yellow {
114        color?: 'yellow',
115    }
116    function draw(val: Blue | Yellow) { }
117
118    function drawWithColor(currentColor: 'blue' | 'yellow' | undefined) {
119        return draw({ color: currentColor });
120    }
121}
122
123// https://github.com/Microsoft/TypeScript/issues/12052
124namespace GH12052 {
125    interface ILinearAxis { type: "linear"; }
126
127    interface ICategoricalAxis { type: "categorical"; }
128
129    type IAxis = ILinearAxis | ICategoricalAxis;
130    type IAxisType = "linear" | "categorical";
131
132    function getAxisType(): IAxisType {
133        if (1 == 1) {
134            return "categorical";
135        } else {
136            return "linear";
137        }
138    }
139
140    const bad: IAxis = { type: getAxisType() };
141    const good: IAxis = { type: undefined };
142    good.type = getAxisType();
143}
144
145// https://github.com/Microsoft/TypeScript/issues/18421
146namespace GH18421 {
147    interface ThingTypeOne {
148        type: 'one';
149    }
150
151    interface ThingTypeTwo {
152        type: 'two';
153    }
154
155    type ThingType = 'one' | 'two';
156
157    type Thing = ThingTypeOne | ThingTypeTwo;
158
159    function makeNewThing(thingType: ThingType): Thing {
160        return {
161            type: thingType
162        };
163    }
164}
165
166// https://github.com/Microsoft/TypeScript/issues/15907
167namespace GH15907 {
168    type Action = { type: 'activate' } | { type: 'disactivate' };
169
170    function dispatchAction(action: Action): void {
171
172    }
173
174    const active = true;
175
176    dispatchAction({ type : (active? 'disactivate' : 'activate') });
177}
178
179// https://github.com/Microsoft/TypeScript/issues/20889
180namespace GH20889 {
181    interface A1 {
182        type: "A1";
183    }
184    interface A2 {
185        type: "A2";
186    }
187    type AU = A1 | A2;
188
189    function foo(obj1: AU) {
190        const obj2: AU = {
191            type: obj1.type
192        };
193    }
194}
195
196// https://github.com/microsoft/TypeScript/issues/39357
197namespace GH39357 {
198    type A = ["a", number] | ["b", number] | ["c", string];
199    type B = "a" | "b" | "c";
200    declare const b: B;
201    const a: A = b === "a" || b === "b" ? [b, 1] : ["c", ""];
202}
203
204
205//// [assignmentCompatWithDiscriminatedUnion.js]
206// see 'typeRelatedToDiscriminatedType' in checker.ts:
207// IteratorResult
208var Example1;
209(function (Example1) {
210    // S is assignable to T0 when S["done"] is true
211    // S is assignable to T1 when S["done"] is false
212    t = s;
213})(Example1 || (Example1 = {}));
214// Dropping constituents of T
215var Example2;
216(function (Example2) {
217    // S is assignable to T0 when S["a"] is 0
218    // S is assignable to T2 when S["a"] is 2
219    t = s;
220})(Example2 || (Example2 = {}));
221// Unmatched discriminants
222var Example3;
223(function (Example3) {
224    // S is assignable to T0 when S["a"] is 0
225    // S is *not* assignable to T1 when S["b"] is 4
226    // S is *not* assignable to T2 when S["a"] is 2
227    t = s;
228})(Example3 || (Example3 = {}));
229// Unmatched non-discriminants
230var Example4;
231(function (Example4) {
232    // S is assignable to T0 when S["a"] is 0
233    // S is *not* assignable to T2 when S["a"] is 2 as S is missing "c"
234    t = s;
235})(Example4 || (Example4 = {}));
236// Maximum discriminant combinations
237var Example5;
238(function (Example5) {
239    // S *should* be assignable but the number of
240    // combinations is too complex.
241    t = s;
242})(Example5 || (Example5 = {}));
243// https://github.com/Microsoft/TypeScript/issues/14865
244var GH14865;
245(function (GH14865) {
246    var a = { type: "A", data: "whatevs" };
247    var b;
248    a.type; // "A" | "B"
249    b.type; // "A" | "B"
250    b = a; // should be assignable
251})(GH14865 || (GH14865 = {}));
252// https://github.com/Microsoft/TypeScript/issues/30170
253var GH30170;
254(function (GH30170) {
255    function draw(val) { }
256    function drawWithColor(currentColor) {
257        return draw({ color: currentColor });
258    }
259})(GH30170 || (GH30170 = {}));
260// https://github.com/Microsoft/TypeScript/issues/12052
261var GH12052;
262(function (GH12052) {
263    function getAxisType() {
264        if (1 == 1) {
265            return "categorical";
266        }
267        else {
268            return "linear";
269        }
270    }
271    var bad = { type: getAxisType() };
272    var good = { type: undefined };
273    good.type = getAxisType();
274})(GH12052 || (GH12052 = {}));
275// https://github.com/Microsoft/TypeScript/issues/18421
276var GH18421;
277(function (GH18421) {
278    function makeNewThing(thingType) {
279        return {
280            type: thingType
281        };
282    }
283})(GH18421 || (GH18421 = {}));
284// https://github.com/Microsoft/TypeScript/issues/15907
285var GH15907;
286(function (GH15907) {
287    function dispatchAction(action) {
288    }
289    var active = true;
290    dispatchAction({ type: (active ? 'disactivate' : 'activate') });
291})(GH15907 || (GH15907 = {}));
292// https://github.com/Microsoft/TypeScript/issues/20889
293var GH20889;
294(function (GH20889) {
295    function foo(obj1) {
296        var obj2 = {
297            type: obj1.type
298        };
299    }
300})(GH20889 || (GH20889 = {}));
301// https://github.com/microsoft/TypeScript/issues/39357
302var GH39357;
303(function (GH39357) {
304    var a = b === "a" || b === "b" ? [b, 1] : ["c", ""];
305})(GH39357 || (GH39357 = {}));
306