File: unionTypeInference.types

package info (click to toggle)
node-typescript 3.3.3333-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 324,548 kB
  • sloc: makefile: 6; sh: 3
file content (113 lines) | stat: -rw-r--r-- 2,442 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
=== 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
>x : T
>y : string | T

    return x;
>x : T
}

var a1: number;
>a1 : number

var a1 = f(1, 2);
>a1 : number
>f(1, 2) : any
>f : <T>(x: T, y: string | T) => T
>1 : 1
>2 : 2

var a2: number;
>a2 : number

var a2 = f(1, "hello");
>a2 : number
>f(1, "hello") : 1
>f : <T>(x: T, y: string | T) => T
>1 : 1
>"hello" : "hello"

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 : 1
>a1 || "hello" : number | "hello"
>a1 : number
>"hello" : "hello"

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" : "abc"

function g<T>(value: [string, T]): T {
>g : <T>(value: [string, T]) => T
>value : [string, T]

    return value[1];
>value[1] : T
>value : [string, T]
>1 : 1
}

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, true]
>"string" : "string"
>true : true

function h<T>(x: string|boolean|T): T {
>h : <T>(x: string | boolean | T) => T
>x : string | boolean | 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" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | boolean | T
>"string" : "string"
>typeof x === "boolean" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : boolean | T
>"boolean" : "boolean"
>undefined : undefined
>x : T
}

var c1: number;
>c1 : number

var c1 = h(5);
>c1 : number
>h(5) : 5
>h : <T>(x: string | boolean | T) => T
>5 : 5

var c2: string;
>c2 : string

var c2 = h("abc");
>c2 : string
>h("abc") : "abc"
>h : <T>(x: string | boolean | T) => T
>"abc" : "abc"