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