1/*
2 * Copyright (c) 2023-2024 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 */
15import {ohFunction1, ohFunction2, OhosI} from './oh_modules/ohos_lib'
16interface I {
17  a: number;
18  b: string;
19}
20
21class C {
22  a: number;
23  b: string;
24}
25
26class C2 {
27  q: { x: number; y: string };
28  w: any;
29  e: I;
30  r: C;
31}
32
33function localVariable(): void {
34  // Declaration
35  let a1 = { a: 1, b: 'a' }; // NOT OK
36  let a2: any = { a: 2, b: 'b' }; // OK - ASSIGNMENT TO ANY
37  let a3: { a: number; b: string } = { a: 30, b: 'c' }; // NOT OK
38  let a4: I = { a: 4, b: 'd' }; // OK
39  let a5: C = { a: 5, b: 'e' }; // OK
40  let a6: C2 = {
41    // OK
42    q: { x: 6, y: 'f' }, // NOT OK
43    w: { a: 7, b: 'g' }, // OK - ASSIGNMENT TO ANY
44    e: { a: 8, b: 'h' }, // OK
45    r: { a: 9, b: 'i' }, // OK
46  };
47
48  // Assignment
49  a1 = { a: 11, b: 'a' }; // NOT OK
50  a2 = { a: 12, b: 'b' }; // OK - ASSIGNMENT TO ANY
51  a3 = { a: 13, b: 'c' }; // NOT OK
52  a4 = { a: 14, b: 'd' }; // OK
53  a5 = { a: 15, b: 'e' }; // OK
54  a6 = {
55    // OK
56    q: { x: 16, y: 'f' }, // NOT OK
57    w: { a: 17, b: 'g' }, // OK - ASSIGNMENT TO ANY
58    e: { a: 18, b: 'h' }, // OK
59    r: { a: 19, b: 'i' }, // OK
60  };
61}
62
63function defaultParamValue(): void {
64  function foo(x = { a: 21, b: 'a' }) {
65    console.log(x.a, x.b);
66  } // NOT OK
67  function foo2(x: any = { a: 22, b: 'b' }) {
68    console.log(x.a, x.b);
69  } // NOT OK
70  function foo3(x: { a: number; b: string } = { a: 23, b: 'c' }) {
71    console.log(x.a, x.b);
72  } // NOT OK
73  function foo4(x: I = { a: 24, b: 'd' }) {
74    console.log(x.a, x.b);
75  } // OK
76  function foo5(x: C = { a: 25, b: 'e' }) {
77    console.log(x.a, x.b);
78  } // OK
79
80  // Function call
81  foo({ a: 21, b: 'a' }); // NOT OK
82  foo2({ a: 22, b: 'b' }); // OK - ASSIGNMENT TO ANY
83  foo3({ a: 23, b: 'c' }); // NOT OK
84  foo4({ a: 24, b: 'd' }); // OK
85  foo5({ a: 25, b: 'e' }); // OK
86}
87
88function returnFromFunction(): void {
89  function bar() {
90    return { a: 31, b: 'a' };
91  } // NOT OK
92  function bar2(): any {
93    return { a: 32, b: 'b' };
94  } // OK - ASSIGNMENT TO ANY
95  function bar3(): { a: number; b: string } {
96    return { a: 33, b: 'c' };
97  } // NOT OK
98  function bar4(): I {
99    return { a: 34, b: 'd' };
100  } // OK
101  function bar5(): C {
102    return { a: 35, b: 'e' };
103  } // OK
104}
105
106function ternaryOperator(): void {
107  // In ternary operator
108  const condition = true;
109  const a1 = condition ? { a: 41, b: 'a' } : { a: 42, b: 'b' }; // NOT OK
110  const a2: any = condition ? { a: 43, b: 'c' } : { a: 44, b: 'd' }; // OK - ASSIGNMENT TO ANY
111  const a3: { a: number; b: string } = condition
112    ? { a: 45, b: 'e' }
113    : { a: 46, b: 'f' }; // NOT OK
114  const a4: I = condition ? { a: 47, b: 'g' } : { a: 48, b: 'h' }; // OK
115  const a5: C = condition ? { a: 49, b: 'i' } : { a: 50, b: 'j' }; // OK
116}
117
118function arrayLiteral(): void {
119  const arr1 = [
120    { a: 51, b: 'a' },
121    { a: 52, b: 'b' },
122  ]; // NOT OK
123  const arr2: any[] = [
124    { a: 53, b: 'c' },
125    { a: 54, b: 'd' },
126  ]; // OK - ASSIGNMENT TO ANY
127  const arr3: { a: number; b: string }[] = [
128    { a: 55, b: 'e' },
129    { a: 56, b: 'f' },
130  ]; // NOT OK
131  const arr4: I[] = [
132    { a: 57, b: 'g' },
133    { a: 58, b: 'h' },
134  ]; // OK
135  const arr5: C[] = [
136    { a: 59, b: 'i' },
137    { a: 60, b: 'j' },
138  ]; // OK
139}
140
141enum E {
142  OK,
143  NO_OK,
144}
145interface I1 {
146	v: E | number
147}
148
149interface I2 {
150	v: E
151}
152
153let i1: I1 = {v:E.OK}
154let i2: I2 = {v:E.NO_OK}
155
156function g1(a: E) {
157  let ii1: I1 = {v:a}
158  let ii2: I2 = {v:a}
159}
160
161function g(): boolean {
162  return true;
163}
164interface CondI {
165  a: number;
166}
167let a1: CondI = {
168  a: g() ? 0 : 1,
169};
170let b1: CondI = {
171  a: (g() ? 0 : 1) as number,
172};
173let c1 = g() ? 0 : 1;
174let d1: CondI = {
175  a: c1,
176};
177let e1: CondI = {
178a: 0|1|2|3
179}
180let f1: 0|1|2|3 = 3
181let ff : CondI = {
182    a: f1
183}
184
185let dict = new Map<string, string | number>();
186dict.set('1', 123)
187
188interface III {
189  param?: string | number | boolean
190}
191
192let test1: III = { param: dict.get('1') } as III
193let test2: III = { param: dict.get('1')! } as III
194let test3: III = { param: dict.get('1') as number } as III
195let test4: III = { param: dict.get('1') as (number | string) } as III
196export interface Style {
197}
198export class SwitchMenuStyle implements Style {
199}
200export class ProfileOneLineSwitchMenuStyle extends SwitchMenuStyle {
201}
202export class ProfileSwitchMenuStyle extends SwitchMenuStyle {
203}
204export let hfpProfileSwitchMenuStyle = new ProfileSwitchMenuStyle();
205export let hfpProfileOneLineSwitchMenuStyle = new ProfileOneLineSwitchMenuStyle();
206
207export interface SettingsBaseMenuData {
208  style?: Style;
209}
210
211function test(isDisConnected:boolean){
212  let a={style: isDisConnected ? hfpProfileOneLineSwitchMenuStyle: hfpProfileSwitchMenuStyle} as SettingsBaseMenuData
213}
214
215interface PPP {
216  x: number
217  y: number | undefined
218  z?: number
219}
220
221let p1: PPP = {x: 10, y: 10}
222let p2: PPP = {x: 10, y: undefined}
223let p3: PPP = {x: 10, y: undefined, z: undefined}
224let p4: PPP = {x: 10, y: undefined, z: 10}
225let p5: PPP = {x: 10, y: 10, z: 10}
226const cp1: PPP = {x: 10, y: 10}
227const cp2: PPP = {x: 10, y: undefined}
228const cp3: PPP = {x: 10, y: undefined, z: undefined}
229const cp4: PPP = {x: 10, y: undefined, z: 10}
230const cp5: PPP = {x: 10, y: 10, z: 10}
231
232const oi: OhosI = { f: 1 };
233
234ohFunction1({d: oi})
235ohFunction1({d: {f: 1}})
236ohFunction2({d: oi})
237ohFunction2({d: {f: 1}})
238