• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16// Untyped object literals
17let method = {
18  m() { console.log(1); } // Error, fixable
19};
20
21let getMethod = {
22  get property() { return 2; } // Error, fixable
23};
24
25let setMethod = {
26  set property(value: number) { // Error, fixable
27    console.log(value);
28  }
29};
30
31let x = 1, y = '2', z = true;
32
33let shorthand = {
34  x, // Error, fixable
35  y, // Error, fixable
36  z // Error, fixable
37};
38
39let spread = {
40  ...shorthand // Error, not fixable
41};
42
43let mixed = { // Fixable
44  a: "foo",
45  b: 42,
46  c: [1, 2, 3],
47  x, // Error
48  y, // Error
49
50  method() { // Error
51    console.log(42)
52  },
53
54  get property() { // Error
55    return 0;
56  },
57
58  set property(value: number) { // Error
59    if (value < 0) {
60      throw new Error('Bad value');
61    }
62  }
63};
64
65let x2 = 1, y2 = 2, z2 = 3;
66let mixedBad = { // Not fixable
67  a: 1,
68  b: 2,
69  x2, // Error, fixable
70  y2, // Error, fixable
71  z2, // Error, fixable
72  m() {},
73  ...shorthand // Error, not fixable
74}
75
76// Typed object literals
77interface I {
78  m(): void;
79}
80let i: I = {
81  m() { // Fixable
82    console.log(100);
83  }
84};
85
86class C {
87  m(): void {
88    console.log(200);
89  }
90}
91let c: C = {
92  m(): void { // Fixable
93    console.log(300);
94  }
95};
96
97function foo(c: C) {}
98foo({
99  m() { console.log(300); } // Fixable
100});
101
102class C2 {
103  x2 = 10;
104  y2 = 20;
105  z2 = 30;
106
107  m() {}
108}
109let c2: C2 = {
110  x2, // Fixable
111  y2, // Fixable
112  z2, // Fixable
113  m() { console.log(1); } // Fixable
114};
115
116let c22: C2 = {
117  x2, // Fixable
118  y2, // Fixable
119  z2, // Fixable
120  m() { console.log(1); }, // Not fixable, object has spread property
121  ...shorthand // Not fixable
122};
123
124class C3 {
125  x2 = 10;
126  y2 = 20;
127  z2 = 30;
128
129  m() {}
130
131  constructor(a: number) {}
132}
133let c3: C3 = {
134  x2, // Fixable
135  y2, // Fixable
136  z2, // Fixable
137  m() { console.log(1); } // Not fixable, class type has constructor with parameters
138};
139
140function capturesFromLocalScope() {
141  let a = 1, b = 2;
142  let captureLocalVal = {
143    m() { // Not fixable, captures local values 'a' and 'b'
144      console.log(a, b);
145    }
146  };
147
148  let captureLocalVal2: C = {
149    m(): void { // Not fixable, captures local values 'a' and 'b'
150      console.log(a, b);
151    }
152  };
153
154  type LocalType = {a: number, b: string};
155  let localTypeVar: LocalType = { a: 1, b: '2' };
156  let captureLocalType = {
157    m() { // Not fixable, captures value of type `LocalType` declared in local scope
158      console.log(localTypeVar);
159    }
160  };
161  let captureLocalType2 = {
162    m(x: LocalType) { // Not fixable, `x` references type `LocalType` declared in local scope
163      console.log(x);
164    }
165  };
166
167  class LocalClass { x: number = 1 };
168  let captureLocalType3 = {
169    m() { // Not fixable, references type `LocalClass` declared in local scope
170      console.log(new LocalClass());
171    }
172  };
173}
174
175// Method overriding field
176class C4 {
177  a: number = 0;
178  b() {};
179}
180let c4: C4 = { // Not fixable, overrides class method with property of functional type
181  a: 1,
182  b: () => {}
183};
184
185class C5 {
186  a: number = 0;
187  b: () => void;
188}
189let c5: C5 = { // Not fixable, overrides class property with method
190  a: 1,
191  b() {}
192};
193
194interface I2 {
195  a: number;
196  b(): void;
197}
198let i2: I2 = { // Not fixable, implements method as functional-type property
199  a: 1,
200  b: () => {}
201};
202
203interface I3 {
204  a: number;
205  b: () => void;
206}
207let ii: I3 = { // Not fixable, implements functional-type property as a method
208  a: 1,
209  b() {}
210};
211
212// Inheritance
213class Base {
214  constructor() {}
215}
216class Derived extends Base {
217  m() {}
218}
219let b: Derived = { // Fixable
220  m() { console.log(2); }
221};
222
223class Base2 {
224  constructor(a: number) {}
225}
226class Derived2 extends Base2 {
227  m() {}
228}
229let b2: Derived2 = { // Not fixable, derived class inherits a constructor with parameters from base class
230  m() { console.log(2); }
231};
232
233class Base3 {
234  constructor(a: number) {}
235}
236class Derived3 extends Base3 {
237  m() {}
238
239  constructor() {
240    super(1);
241  }
242}
243let b3: Derived3 = { // Fixable
244  m() { console.log(2); }
245};
246
247interface I4 {
248  map: Map<string, string>;
249}
250let map:Map<string,string> = new Map<string,string>();
251let i4: I4 = {map};
252
253class C6 {
254  map1: Map<string, string> = new Map<string,string>();
255}
256
257let map1:Map<string,string> = new Map<string,string>();
258let c6: C6 = {map1};
259
260// Namespace typed object literals
261namespace X {
262  export class C {
263    m() {
264      console.log("C - 1");
265    }
266  }
267
268  export interface I {
269    m(a: number, b: string): void;
270  }
271}
272
273function test() {
274  let c: X.C = {
275    m() {
276      console.log("C - 2");
277    }
278  }
279
280  let i: X.I = {
281    m(): void {
282      console.log("I");
283    }
284  }
285}
286
287class FooBarBaz {
288    foo?: Map<string, Object>
289    bar?: string
290}
291
292function baz(fooBar: Map<string, Object>) {
293    baz2({fooBar});
294}
295
296function baz2(fooBarBaz: FooBarBaz) {
297}
298