diff --git a/tests/cases/conformance/types/typeRelationships/typeInference/unionTypeInference.ts b/tests/cases/conformance/types/typeRelationships/typeInference/unionTypeInference.ts index 39def706622..14ce8e91fc7 100644 --- a/tests/cases/conformance/types/typeRelationships/typeInference/unionTypeInference.ts +++ b/tests/cases/conformance/types/typeRelationships/typeInference/unionTypeInference.ts @@ -1,31 +1,42 @@ -// Verify that inferences made *to* a type parameter in a union type are secondary -// to inferences made directly to that type parameter +// @strict: true -function f(x: T, y: string|T): T { - return x; -} +declare const b: boolean; +declare const s: string; +declare const sn: string | number; -var a1: number; -var a1 = f(1, 2); -var a2: number; -var a2 = f(1, "hello"); -var a3: number; -var a3 = f(1, a1 || "hello"); -var a4: any; -var a4 = f(undefined, "abc"); +declare function f1(x: T, y: string | T): T; -function g(value: [string, T]): T { - return value[1]; -} +const a1 = f1(1, 2); // 1 | 2 +const a2 = f1(1, "hello"); // 1 +const a3 = f1(1, sn); // number +const a4 = f1(undefined, "abc"); // undefined +const a5 = f1("foo", "bar"); // "foo" +const a6 = f1(true, false); // boolean +const a7 = f1("hello", 1); // Error -var b1: boolean; -var b1 = g(["string", true]); +declare function f2(value: [string, T]): T; -function h(x: string|boolean|T): T { - return typeof x === "string" || typeof x === "boolean" ? undefined : x; -} +var b1 = f2(["string", true]); // boolean -var c1: number; -var c1 = h(5); -var c2: string; -var c2 = h("abc"); +declare function f3(x: string | false | T): T; + +const c1 = f3(5); // 5 +const c2 = f3(sn); // number +const c3 = f3(true); // true +const c4 = f3(b); // true +const c5 = f3("abc"); // never + +declare function f4(x: string & T): T; + +var d1 = f4("abc"); +var d2 = f4(s); +var d3 = f4(42); // Error + +// Repros from #32434 + +declare function foo(x: T | Promise): void; +declare let x: false | Promise; +foo(x); + +declare function bar(x: T, y: string | T): T; +const y = bar(1, 2);