123 lines
2.3 KiB
Text
123 lines
2.3 KiB
Text
=== tests/cases/conformance/types/typeRelationships/typeInference/unionTypeInference.ts ===
|
|
// Verify that inferences made *to* a type parameter in a union type are secondary
|
|
// to inferences made directly to that type parameter
|
|
|
|
function f<T>(x: T, y: string|T): T {
|
|
>f : <T>(x: T, y: string | T) => T
|
|
>T : T
|
|
>x : T
|
|
>T : T
|
|
>y : string | T
|
|
>T : T
|
|
>T : T
|
|
|
|
return x;
|
|
>x : T
|
|
}
|
|
|
|
var a1: number;
|
|
>a1 : number
|
|
|
|
var a1 = f(1, 2);
|
|
>a1 : number
|
|
>f(1, 2) : number
|
|
>f : <T>(x: T, y: string | T) => T
|
|
>1 : number
|
|
>2 : number
|
|
|
|
var a2: number;
|
|
>a2 : number
|
|
|
|
var a2 = f(1, "hello");
|
|
>a2 : number
|
|
>f(1, "hello") : number
|
|
>f : <T>(x: T, y: string | T) => T
|
|
>1 : number
|
|
>"hello" : string
|
|
|
|
var a3: number;
|
|
>a3 : number
|
|
|
|
var a3 = f(1, a1 || "hello");
|
|
>a3 : number
|
|
>f(1, a1 || "hello") : number
|
|
>f : <T>(x: T, y: string | T) => T
|
|
>1 : number
|
|
>a1 || "hello" : string | number
|
|
>a1 : number
|
|
>"hello" : string
|
|
|
|
var a4: any;
|
|
>a4 : any
|
|
|
|
var a4 = f(undefined, "abc");
|
|
>a4 : any
|
|
>f(undefined, "abc") : any
|
|
>f : <T>(x: T, y: string | T) => T
|
|
>undefined : undefined
|
|
>"abc" : string
|
|
|
|
function g<T>(value: [string, T]): T {
|
|
>g : <T>(value: [string, T]) => T
|
|
>T : T
|
|
>value : [string, T]
|
|
>T : T
|
|
>T : T
|
|
|
|
return value[1];
|
|
>value[1] : T
|
|
>value : [string, T]
|
|
>1 : number
|
|
}
|
|
|
|
var b1: boolean;
|
|
>b1 : boolean
|
|
|
|
var b1 = g(["string", true]);
|
|
>b1 : boolean
|
|
>g(["string", true]) : boolean
|
|
>g : <T>(value: [string, T]) => T
|
|
>["string", true] : [string, boolean]
|
|
>"string" : string
|
|
>true : boolean
|
|
|
|
function h<T>(x: string|boolean|T): T {
|
|
>h : <T>(x: string | boolean | T) => T
|
|
>T : T
|
|
>x : string | boolean | T
|
|
>T : T
|
|
>T : T
|
|
|
|
return typeof x === "string" || typeof x === "boolean" ? undefined : x;
|
|
>typeof x === "string" || typeof x === "boolean" ? undefined : x : T
|
|
>typeof x === "string" || typeof x === "boolean" : boolean
|
|
>typeof x === "string" : boolean
|
|
>typeof x : string
|
|
>x : string | boolean | T
|
|
>"string" : string
|
|
>typeof x === "boolean" : boolean
|
|
>typeof x : string
|
|
>x : boolean | T
|
|
>"boolean" : string
|
|
>undefined : undefined
|
|
>x : T
|
|
}
|
|
|
|
var c1: number;
|
|
>c1 : number
|
|
|
|
var c1 = h(5);
|
|
>c1 : number
|
|
>h(5) : number
|
|
>h : <T>(x: string | boolean | T) => T
|
|
>5 : number
|
|
|
|
var c2: string;
|
|
>c2 : string
|
|
|
|
var c2 = h("abc");
|
|
>c2 : string
|
|
>h("abc") : string
|
|
>h : <T>(x: string | boolean | T) => T
|
|
>"abc" : string
|
|
|