• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) Microsoft Corporation. All rights reserved.
3* Copyright (c) 2023 Huawei Device Co., Ltd.
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8*     http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*
16* This file has been modified by Huawei to verify type inference by adding verification statements.
17*/
18
19// === tests/cases/conformance/types/objectTypeLiteral/indexSignatures/numericIndexingResults.ts ===
20declare function AssertType(value:any, type:string):void;
21class C {
22    [x: number]: string;
23    1 = '';
24    "2" = ''
25}
26
27let c: C;
28AssertType(c, "C");
29
30let r1 = c['1'];
31AssertType(r1, "string");
32AssertType(c['1'], "string");
33AssertType(c, "C");
34AssertType('1', "string");
35
36let r2 = c['2'];
37AssertType(r2, "string");
38AssertType(c['2'], "string");
39AssertType(c, "C");
40AssertType('2', "string");
41
42let r3 = c['3'];
43AssertType(r3, "string");
44AssertType(c['3'], "string");
45AssertType(c, "C");
46AssertType('3', "string");
47
48let r4 = c[1];
49AssertType(r4, "string");
50AssertType(c[1], "string");
51AssertType(c, "C");
52AssertType(1, "int");
53
54let r5 = c[2];
55AssertType(r5, "string");
56AssertType(c[2], "string");
57AssertType(c, "C");
58AssertType(2, "int");
59
60let r6 = c[3];
61AssertType(r6, "string");
62AssertType(c[3], "string");
63AssertType(c, "C");
64AssertType(3, "int");
65
66interface I {
67    [x: number]: string;
68    1: string;
69    "2": string;
70}
71
72let i: I
73AssertType(i, "I");
74
75let r1 = i['1'];
76AssertType(r1, "string");
77AssertType(i['1'], "string");
78AssertType(i, "I");
79AssertType('1', "string");
80
81let r2 = i['2'];
82AssertType(r2, "string");
83AssertType(i['2'], "string");
84AssertType(i, "I");
85AssertType('2', "string");
86
87let r3 = i['3'];
88AssertType(r3, "string");
89AssertType(i['3'], "string");
90AssertType(i, "I");
91AssertType('3', "string");
92
93let r4 = i[1];
94AssertType(r4, "string");
95AssertType(i[1], "string");
96AssertType(i, "I");
97AssertType(1, "int");
98
99let r5 = i[2];
100AssertType(r5, "string");
101AssertType(i[2], "string");
102AssertType(i, "I");
103AssertType(2, "int");
104
105let r6 = i[3];
106AssertType(r6, "string");
107AssertType(i[3], "string");
108AssertType(i, "I");
109AssertType(3, "int");
110
111let a: {
112AssertType(a, "{ [number]: string; 1: string; "2": string; }");
113
114    [x: number]: string;
115AssertType(x, "number");
116
117    1: string;
118AssertType(1, "string");
119
120    "2": string;
121AssertType("2", "string");
122}
123
124let r1 = a['1'];
125AssertType(r1, "string");
126AssertType(a['1'], "string");
127AssertType(a, "{ [number]: string; 1: string; "2": string; }");
128AssertType('1', "string");
129
130let r2 = a['2'];
131AssertType(r2, "string");
132AssertType(a['2'], "string");
133AssertType(a, "{ [number]: string; 1: string; "2": string; }");
134AssertType('2', "string");
135
136let r3 = a['3'];
137AssertType(r3, "string");
138AssertType(a['3'], "string");
139AssertType(a, "{ [number]: string; 1: string; "2": string; }");
140AssertType('3', "string");
141
142let r4 = a[1];
143AssertType(r4, "string");
144AssertType(a[1], "string");
145AssertType(a, "{ [number]: string; 1: string; "2": string; }");
146AssertType(1, "int");
147
148let r5 = a[2];
149AssertType(r5, "string");
150AssertType(a[2], "string");
151AssertType(a, "{ [number]: string; 1: string; "2": string; }");
152AssertType(2, "int");
153
154let r6 = a[3];
155AssertType(r6, "string");
156AssertType(a[3], "string");
157AssertType(a, "{ [number]: string; 1: string; "2": string; }");
158AssertType(3, "int");
159
160let b: { [x: number]: string } = { 1: '', "2": ''
161AssertType(b, "{ [number]: string; }");
162
163AssertType(x, "number");
164
165AssertType({ 1: '', "2": '' }, "{ 1: string; "2": string; }");
166
167AssertType(1, "string");
168
169AssertType('', "string");
170
171AssertType("2", "string");
172
173AssertType('', "string");
174}
175
176let r1a = b['1'];
177AssertType(r1a, "string");
178AssertType(b['1'], "string");
179AssertType(b, "{ [number]: string; }");
180AssertType('1', "string");
181
182let r2a = b['2'];
183AssertType(r2a, "string");
184AssertType(b['2'], "string");
185AssertType(b, "{ [number]: string; }");
186AssertType('2', "string");
187
188let r3 = b['3'];
189AssertType(r3, "string");
190AssertType(b['3'], "string");
191AssertType(b, "{ [number]: string; }");
192AssertType('3', "string");
193
194let r4 = b[1];
195AssertType(r4, "string");
196AssertType(b[1], "string");
197AssertType(b, "{ [number]: string; }");
198AssertType(1, "int");
199
200let r5 = b[2];
201AssertType(r5, "string");
202AssertType(b[2], "string");
203AssertType(b, "{ [number]: string; }");
204AssertType(2, "int");
205
206let r6 = b[3];
207AssertType(r6, "string");
208AssertType(b[3], "string");
209AssertType(b, "{ [number]: string; }");
210AssertType(3, "int");
211
212let b2: { [x: number]: string; 1: string; "2": string; } = { 1: '', "2": ''
213AssertType(b2, "{ [number]: string; 1: string; "2": string; }");
214
215AssertType(x, "number");
216
217AssertType(1, "string");
218
219AssertType("2", "string");
220
221AssertType({ 1: '', "2": '' }, "{ 1: string; "2": string; }");
222
223AssertType(1, "string");
224
225AssertType('', "string");
226
227AssertType("2", "string");
228
229AssertType('', "string");
230}
231
232let r1b = b2['1'];
233AssertType(r1b, "string");
234AssertType(b2['1'], "string");
235AssertType(b2, "{ [number]: string; 1: string; "2": string; }");
236AssertType('1', "string");
237
238let r2b = b2['2'];
239AssertType(r2b, "string");
240AssertType(b2['2'], "string");
241AssertType(b2, "{ [number]: string; 1: string; "2": string; }");
242AssertType('2', "string");
243
244let r3 = b2['3'];
245AssertType(r3, "string");
246AssertType(b2['3'], "string");
247AssertType(b2, "{ [number]: string; 1: string; "2": string; }");
248AssertType('3', "string");
249
250let r4 = b2[1];
251AssertType(r4, "string");
252AssertType(b2[1], "string");
253AssertType(b2, "{ [number]: string; 1: string; "2": string; }");
254AssertType(1, "int");
255
256let r5 = b2[2];
257AssertType(r5, "string");
258AssertType(b2[2], "string");
259AssertType(b2, "{ [number]: string; 1: string; "2": string; }");
260AssertType(2, "int");
261
262let r6 = b2[3];
263AssertType(r6, "string");
264AssertType(b2[3], "string");
265AssertType(b2, "{ [number]: string; 1: string; "2": string; }");
266AssertType(3, "int");
267
268
269