diff --git a/tests/cases/conformance/types/typeRelationships/typeInference/unionAndIntersectionInference1.ts b/tests/cases/conformance/types/typeRelationships/typeInference/unionAndIntersectionInference1.ts new file mode 100644 index 0000000000..7066c3e679 --- /dev/null +++ b/tests/cases/conformance/types/typeRelationships/typeInference/unionAndIntersectionInference1.ts @@ -0,0 +1,72 @@ +// Repro from #2264 + +interface Y { 'i am a very certain type': Y } +var y: Y = undefined; +function destructure( + something: a | Y, + haveValue: (value: a) => r, + haveY: (value: Y) => r +): r { + return something === y ? haveY(y) : haveValue(something); +} + +var value = Math.random() > 0.5 ? 'hey!' : undefined; + +var result = destructure(value, text => 'string', y => 'other one'); // text: string, y: Y + +// Repro from #4212 + +function isVoid(value: void | a): value is void { + return undefined; +} + +function isNonVoid(value: void | a) : value is a { + return undefined; +} + +function foo1(value: void|a): void { + if (isVoid(value)) { + value; // value is void + } else { + value; // value is a + } +} + +function baz1(value: void|a): void { + if (isNonVoid(value)) { + value; // value is a + } else { + value; // value is void + } +} + +// Repro from #5417 + +type Maybe = T | void; + +function get(x: U | void): U { + return null; // just an example +} + +let foo: Maybe; +get(foo).toUpperCase(); // Ok + +// Repro from #5456 + +interface Man { + walks: boolean; +} + +interface Bear { + roars: boolean; +} + +interface Pig { + oinks: boolean; +} + +declare function pigify(y: T & Bear): T & Pig; +declare var mbp: Man & Bear; + +pigify(mbp).oinks; // OK, mbp is treated as Pig +pigify(mbp).walks; // Ok, mbp is treated as Man diff --git a/tests/cases/conformance/types/typeRelationships/typeInference/unionAndIntersectionInference2.ts b/tests/cases/conformance/types/typeRelationships/typeInference/unionAndIntersectionInference2.ts new file mode 100644 index 0000000000..1b6a928ce0 --- /dev/null +++ b/tests/cases/conformance/types/typeRelationships/typeInference/unionAndIntersectionInference2.ts @@ -0,0 +1,23 @@ +declare function f1(x: T | string): T; + +var a1: string; +var b1: string | string[]; +var c1: string[] | string; +var d1: string | { name: string }; +var e1: number | string | boolean; +f1(a1); // string +f1(b1); // string[] +f1(c1); // string[] +f1(d1); // { name: string } +f1(e1); // number | boolean + +declare function f2(x: T & { name: string }): T; + +var a2: string & { name: string }; +var b2: { name: string } & string[]; +var c2: string & { name: string } & number; +var d2: string & { name: string } & number & { name: string }; +f2(a2); // string +f2(b2); // string[] +f2(c2); // string & number +f2(d2); // string & number