diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index be7497c89c5..5cec8987384 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4073,8 +4073,7 @@ module ts { inferFromTypes(source, t); } } - // If no inferences were produced above and union contains a single naked type parameter, - // make a secondary inference to that type parameter + // If union contains a single naked type parameter, make a secondary inference to that type parameter if (typeParameterCount === 1) { inferiority++; inferFromTypes(source, typeParameter); diff --git a/tests/cases/conformance/types/typeRelationships/typeInference/unionTypeInference.ts b/tests/cases/conformance/types/typeRelationships/typeInference/unionTypeInference.ts index a4b5edba9eb..39def706622 100644 --- a/tests/cases/conformance/types/typeRelationships/typeInference/unionTypeInference.ts +++ b/tests/cases/conformance/types/typeRelationships/typeInference/unionTypeInference.ts @@ -4,17 +4,28 @@ function f(x: T, y: string|T): T { return x; } + +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"); + function g(value: [string, T]): T { return value[1]; } -var a: number; -var a = f(1, 2); -var b: number; -var b = f(1, "hello"); -var c: number; -var c = f(1, a || "hello"); -var d: any; -var d = f(undefined, "abc"); -var e: boolean; -var e = g(["string", true]); +var b1: boolean; +var b1 = g(["string", true]); + +function h(x: string|boolean|T): T { + return typeof x === "string" || typeof x === "boolean" ? undefined : x; +} + +var c1: number; +var c1 = h(5); +var c2: string; +var c2 = h("abc");