1/* 2 * Copyright (c) 2022-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 { fooOh, barOh } from './oh_modules/ohos_lib' 16type ESValue = any 17 18class A<T> {} 19 20let g1: ESValue 21let g2: ESValue[] 22let g3: A<ESValue> 23 24class B { 25 f1: ESValue 26 f2: ESValue[] 27 f3: A<ESValue> 28 29 constructor(p1: ESValue, p2: ESValue[], p3: A<ESValue>) { 30 this.f1 = p1 31 this.f2 = p2 32 this.f3 = p3 33 } 34 35 foo1(p1: ESValue, p2: ESValue[], p3: A<ESValue>): ESValue { 36 return p1 37 } 38 39 foo2(p1: ESValue, p2: ESValue[], p3: A<ESValue>): ESValue[] { 40 return p2 41 } 42 43 foo3(p1: ESValue, p2: ESValue[], p3: A<ESValue>): A<ESValue> { 44 return p3 45 } 46} 47 48function bar1(p1: ESValue, p2: ESValue[], p3: A<ESValue>): ESValue { 49 return p1 50} 51 52function bar2(p1: ESValue, p2: ESValue[], p3: A<ESValue>): ESValue[] { 53 return p2 54} 55 56function bar3(p1: ESValue, p2: ESValue[], p3: A<ESValue>): A<ESValue> { 57 return p3 58} 59 60function ff(): {x: number} { 61 return {x: 10} 62} 63 64function baz(p1: ESValue, p2: ESValue[], p3: A<ESValue>): void { 65 const c1: ESValue = p1; 66 const c2: ESValue[] = p2 67 const c3: A<ESValue> = p3 68 69 let v1: ESValue = p1 70 let v2: ESValue[] = p2 71 let v3: A<ESValue> = p3 72 73 v1 = c1 74 v2 = c2 75 v3 = c3 76 77 v1.x = 10 78 v1.foo() 79 v1[10] = 20 80 v1(20) 81 82 v1 = {} 83 v1 = "abc" 84 v1 = ff() 85 v1 = [1, 2] 86 v1 = [p1, c1] 87 v1 = [p1, c1, "abc"] 88 v1 = new A<string>() 89 90 let v11: ESValue = {} 91 let v12: ESValue = "abc" 92 let v13: ESValue = ff() 93 let v14: ESValue = [1, 2] 94 let v15: ESValue = [p1, c1] 95 let v16: ESValue = [p1, c1, "abc"] 96 let v17: ESValue = new A<string>() 97 98 let n1: number = v1 99 n1 = v1 100 let n2: number = p1 as number 101} 102 103export let obj = new ESValue(); 104 105type t1 = ESValue 106type t2 = ESValue[] 107 108export type t3 = ESValue 109export type t4 = ESValue[] 110 111export type t5 = t3 112export type t6 = t4[] 113 114export function foo1(): any { 115 let a: ESValue = "STRING"; 116 return a 117} 118 119export function foo2(a: ESValue): ESValue { 120 return a; 121} 122 123export function foo3(a: t3): t3 { 124 return a; 125} 126 127foo2(5) 128foo3(5) 129foo2("asd") 130foo3("asd") 131foo2(null) 132foo3(null) 133foo2(undefined) 134foo3(undefined) 135 136export function foo4(a: ESValue[]): ESValue { 137 return a; 138} 139 140export function foo5(a: t3[]): t3 { 141 return a; 142} 143 144foo4([2, 3]) 145foo5([2, 3]) 146foo4(["str1", "str2"]) 147foo5(["str1", "str2"]) 148let n: ESValue 149n = null 150 151foo4(n) 152foo5(n) 153 154export function foo6<T extends ESValue>(a: ESValue[]): ESValue { 155 return a; 156} 157 158export function foo7<T extends t3>(a: t3[]): t3 { 159 return a; 160} 161 162export function foo8<T extends ESValue[]>(a: ESValue[]): ESValue { 163 return a; 164} 165 166export function foo9<T extends t3[]>(a: t3[]): t3 { 167 return a; 168} 169 170export class Cls<T extends ESValue> {} 171 172interface CL extends ESValue {} 173 174export interface CLS extends ESValue {} 175 176foo2({ k: 'k', h: {t: 1}}) // we can assign anything to the esobject, even untyped literal 177let q1: ESValue = 1; // CTE - ``ESValue`` typed variable can only be local 178let q2: ESValue = fooOh(); // CTE - ``ESValue`` typed variable can only be local 179let q3: ESValue = q2; // CTE - ``ESValue`` typed variable can only be local 180function f() { 181 let e1 = fooOh(); // CTE - type of e1 is `any` 182 let e2: ESValue = 1; // CTE - can't initialize ESValue with not dynamic values 183 let e3: ESValue = {}; // CTE - can't initialize ESValue with not dynamic values 184 let e4: ESValue = []; // CTE - can't initialize ESValue with not dynamic values 185 let e5: ESValue = ""; // CTE - can't initialize ESValue with not dynamic values 186 let e6: ESValue = fooOh(); // OK - explicitly annotaded as ESValue 187 let e7: ESValue = e6; // OK - initialize ESValue with ESValue 188 e6['prop'] // CTE - can't access dynamic properties of ESValue 189 e6[1] // CTE - can't access dynamic properties of ESValue 190 e6.prop // CTE - can't access dynamic properties of ESValue 191 barOh(e6) // OK - ESValue is passed to interop call 192 e6 = e7 // OK - ESValue is assigned to ESValue 193} 194