• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//// [unionAndIntersectionInference1.ts]
2// Repro from #2264
3
4interface Y { 'i am a very certain type': Y }
5var y: Y = <Y>undefined;
6function destructure<a, r>(
7    something: a | Y,
8    haveValue: (value: a) => r,
9    haveY: (value: Y) => r
10): r {
11    return something === y ? haveY(y) : haveValue(<a>something);
12}
13
14var value = Math.random() > 0.5 ? 'hey!' : <Y>undefined;
15
16var result = destructure(value, text => 'string', y => 'other one'); // text: string, y: Y
17
18// Repro from #4212
19
20function isVoid<a>(value: void | a): value is void {
21    return undefined;
22}
23
24function isNonVoid<a>(value: void | a) : value is a {
25    return undefined;
26}
27
28function foo1<a>(value: void|a): void {
29    if (isVoid(value)) {
30        value; // value is void
31    } else {
32        value; // value is a
33    }
34}
35
36function baz1<a>(value: void|a): void {
37      if (isNonVoid(value)) {
38          value; // value is a
39      } else {
40          value; // value is void
41      }
42}
43
44// Repro from #5417
45
46type Maybe<T> = T | void;
47
48function get<U>(x: U | void): U {
49   return null; // just an example
50}
51
52let foo: Maybe<string>;
53get(foo).toUpperCase(); // Ok
54
55// Repro from #5456
56
57interface Man {
58    walks: boolean;
59}
60
61interface Bear {
62    roars: boolean;
63}
64
65interface Pig {
66    oinks: boolean;
67}
68
69declare function pigify<T>(y: T & Bear): T & Pig;
70declare var mbp: Man & Bear;
71
72pigify(mbp).oinks; // OK, mbp is treated as Pig
73pigify(mbp).walks; // Ok, mbp is treated as Man
74
75// Repros from #29815
76
77interface ITest {
78  name: 'test'
79}
80
81const createTestAsync = (): Promise<ITest> => Promise.resolve().then(() => ({ name: 'test' }))
82
83const createTest = (): ITest => {
84  return { name: 'test' }
85}
86
87declare function f1<T, U>(x: T | U): T | U;
88declare function f2<T, U>(x: T, y: U): T | U;
89
90let x1: string = f1('a');
91let x2: string = f2('a', 'b');
92
93// Repro from #30442
94
95const func = <T>() => {};
96const assign = <T, U>(a: T, b: U) => Object.assign(a, b);
97const res: (() => void) & { func: any } = assign(() => {}, { func });
98
99
100//// [unionAndIntersectionInference1.js]
101// Repro from #2264
102var y = undefined;
103function destructure(something, haveValue, haveY) {
104    return something === y ? haveY(y) : haveValue(something);
105}
106var value = Math.random() > 0.5 ? 'hey!' : undefined;
107var result = destructure(value, text => 'string', y => 'other one'); // text: string, y: Y
108// Repro from #4212
109function isVoid(value) {
110    return undefined;
111}
112function isNonVoid(value) {
113    return undefined;
114}
115function foo1(value) {
116    if (isVoid(value)) {
117        value; // value is void
118    }
119    else {
120        value; // value is a
121    }
122}
123function baz1(value) {
124    if (isNonVoid(value)) {
125        value; // value is a
126    }
127    else {
128        value; // value is void
129    }
130}
131function get(x) {
132    return null; // just an example
133}
134let foo;
135get(foo).toUpperCase(); // Ok
136pigify(mbp).oinks; // OK, mbp is treated as Pig
137pigify(mbp).walks; // Ok, mbp is treated as Man
138const createTestAsync = () => Promise.resolve().then(() => ({ name: 'test' }));
139const createTest = () => {
140    return { name: 'test' };
141};
142let x1 = f1('a');
143let x2 = f2('a', 'b');
144// Repro from #30442
145const func = () => { };
146const assign = (a, b) => Object.assign(a, b);
147const res = assign(() => { }, { func });
148