File: contextualTypeWithUnionTypeObjectLiteral.types

package info (click to toggle)
node-typescript 4.9.5%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 533,908 kB
  • sloc: javascript: 2,018,330; makefile: 7; sh: 1
file content (213 lines) | stat: -rw-r--r-- 6,504 bytes parent folder | download | duplicates (5)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
=== tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts ===
var str: string;
>str : string

var num: number;
>num : number

var strOrNumber: string | number = str || num;
>strOrNumber : string | number
>str || num : string | number
>str : string
>num : number

var objStr: { prop: string };
>objStr : { prop: string; }
>prop : string

var objNum: { prop: number };
>objNum : { prop: number; }
>prop : number

var objStrOrNum1: { prop: string } | { prop: number } = objStr || objNum;
>objStrOrNum1 : { prop: string; } | { prop: number; }
>prop : string
>prop : number
>objStr || objNum : { prop: string; } | { prop: number; }
>objStr : { prop: string; }
>objNum : { prop: number; }

var objStrOrNum2: { prop: string | number } = objStr || objNum;
>objStrOrNum2 : { prop: string | number; }
>prop : string | number
>objStr || objNum : { prop: string; } | { prop: number; }
>objStr : { prop: string; }
>objNum : { prop: number; }

// Below is error because :
// Spec says:
// S is a union type and each constituent type of S is assignable to T.
// T is a union type and S is assignable to at least one constituent type of T.
// In case of objStrOrNum3, the S is not union Type but object Literal so we go to next step. 
// Since T is union Type we only allow the assignment of either object with property of type string or object with property of type number but do not allow object with property of type string | number
var objStrOrNum3: { prop: string } | { prop: number } = {
>objStrOrNum3 : { prop: string; } | { prop: number; }
>prop : string
>prop : number
>{    prop: strOrNumber} : { prop: string | number; }

    prop: strOrNumber
>prop : string | number
>strOrNumber : string | number

};
var objStrOrNum4: { prop: string | number } = {
>objStrOrNum4 : { prop: string | number; }
>prop : string | number
>{    prop: strOrNumber} : { prop: string | number; }

    prop: strOrNumber
>prop : string | number
>strOrNumber : string | number

};
var objStrOrNum5: { prop: string; anotherP: string; } | { prop: number } = { prop: strOrNumber };
>objStrOrNum5 : { prop: string; anotherP: string; } | { prop: number; }
>prop : string
>anotherP : string
>prop : number
>{ prop: strOrNumber } : { prop: string | number; }
>prop : string | number
>strOrNumber : string | number

var objStrOrNum6: { prop: string; anotherP: string; } | { prop: number } = {
>objStrOrNum6 : { prop: string; anotherP: string; } | { prop: number; }
>prop : string
>anotherP : string
>prop : number
>{    prop: strOrNumber,    anotherP: str} : { prop: string | number; anotherP: string; }

    prop: strOrNumber,
>prop : string | number
>strOrNumber : string | number

    anotherP: str
>anotherP : string
>str : string

};
var objStrOrNum7: { prop: string; anotherP: string; } | { prop: number; anotherP1: number } = {
>objStrOrNum7 : { prop: string; anotherP: string; } | { prop: number; anotherP1: number; }
>prop : string
>anotherP : string
>prop : number
>anotherP1 : number
>{    prop: strOrNumber,    anotherP: str} : { prop: string | number; anotherP: string; }

    prop: strOrNumber,
>prop : string | number
>strOrNumber : string | number

    anotherP: str
>anotherP : string
>str : string

};
var objStrOrNum8: { prop: string; anotherP: string; } | { prop: number; anotherP1: number } = {
>objStrOrNum8 : { prop: string; anotherP: string; } | { prop: number; anotherP1: number; }
>prop : string
>anotherP : string
>prop : number
>anotherP1 : number
>{    prop: strOrNumber,    anotherP: str,    anotherP1: num} : { prop: string | number; anotherP: string; anotherP1: number; }

    prop: strOrNumber,
>prop : string | number
>strOrNumber : string | number

    anotherP: str,
>anotherP : string
>str : string

    anotherP1: num
>anotherP1 : number
>num : number

};
interface I11 {
    commonMethodDifferentReturnType(a: string, b: number): string;
>commonMethodDifferentReturnType : (a: string, b: number) => string
>a : string
>b : number
}
interface I21 {
    commonMethodDifferentReturnType(a: string, b: number): number;
>commonMethodDifferentReturnType : (a: string, b: number) => number
>a : string
>b : number
}
var i11: I11;
>i11 : I11

var i21: I21;
>i21 : I21

var i11Ori21: I11 | I21 = i11;
>i11Ori21 : I11 | I21
>i11 : I11

var i11Ori21: I11 | I21 = i21;
>i11Ori21 : I11 | I21
>i21 : I21

var i11Ori21: I11 | I21 = { // Like i1
>i11Ori21 : I11 | I21
>{ // Like i1    commonMethodDifferentReturnType: (a, b) => {        var z = a.charAt(b);        return z;    },} : { commonMethodDifferentReturnType: (a: string, b: number) => string; }

    commonMethodDifferentReturnType: (a, b) => {
>commonMethodDifferentReturnType : (a: string, b: number) => string
>(a, b) => {        var z = a.charAt(b);        return z;    } : (a: string, b: number) => string
>a : string
>b : number

        var z = a.charAt(b);
>z : string
>a.charAt(b) : string
>a.charAt : (pos: number) => string
>a : string
>charAt : (pos: number) => string
>b : number

        return z;
>z : string

    },
};
var i11Ori21: I11 | I21 = { // Like i2
>i11Ori21 : I11 | I21
>{ // Like i2    commonMethodDifferentReturnType: (a, b) => {        var z = a.charCodeAt(b);        return z;    },} : { commonMethodDifferentReturnType: (a: string, b: number) => number; }

    commonMethodDifferentReturnType: (a, b) => {
>commonMethodDifferentReturnType : (a: string, b: number) => number
>(a, b) => {        var z = a.charCodeAt(b);        return z;    } : (a: string, b: number) => number
>a : string
>b : number

        var z = a.charCodeAt(b);
>z : number
>a.charCodeAt(b) : number
>a.charCodeAt : (index: number) => number
>a : string
>charCodeAt : (index: number) => number
>b : number

        return z;
>z : number

    },
};
var strOrNumber: string | number;
>strOrNumber : string | number

var i11Ori21: I11 | I21 = { // Like i1 and i2 both
>i11Ori21 : I11 | I21
>{ // Like i1 and i2 both    commonMethodDifferentReturnType: (a, b) => strOrNumber,} : { commonMethodDifferentReturnType: (a: string, b: number) => string | number; }

    commonMethodDifferentReturnType: (a, b) => strOrNumber,
>commonMethodDifferentReturnType : (a: string, b: number) => string | number
>(a, b) => strOrNumber : (a: string, b: number) => string | number
>a : string
>b : number
>strOrNumber : string | number

};