• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { foo, bar, TestClassJ, TestClassK } from "./oh_modules/ohos_lib"
16import { foo as foo_ets, bar as bar_ets, TestClassJ as TestClassJ_ets, TestClassK as TestClassK_ets } from "./oh_modules/ohos_lib_ets"
17
18function restSpread() {
19  const arr = [1, 2, 3];
20  function test(a, ...t) {
21    console.log(a); // 1
22    console.log(t[0]); // 2
23    console.log(t[1]); // 3
24  }
25  test(1, ...arr);
26}
27
28class MyGenerator {
29  public *getValues() {
30    // you can put the return type Generator<number>, but it is ot necessary as ts will infer
31    let index = 1;
32    while (true) {
33      yield index;
34      index = index + 1;
35
36      if (index > 10) {
37        break;
38      }
39    }
40  }
41}
42
43function defaultTypeParam<t, tt = string>(i: t, j: tt) {
44  const c = i;
45  const s = j;
46}
47
48function arrowFunctionTest() {
49  const empty = () => {}; // no return type
50
51  const double = (x: number) => x * 2; // no return type
52
53  const square = (x): number => x * x; // no param type
54
55  const sqrt = (x) => Math.sqrt(x); // shortcut syntax
56  const even = [1, 2, 3, 4, 5, 6].filter((x) => x % 2 === 0); // shortcut syntax
57
58  const foo = (x: number, y): boolean => x == y; // types are partly omitted
59
60  const generic = <T, E>(t: T, e: E) => t; // Generic lambda
61}
62
63function fooThis(i: number): void {
64  this.c = 10;
65}
66class C {
67  c: number;
68  m = fooThis;
69}
70
71function choose<T>(x: T, y: T): T {
72  return Math.random() < 0.5 ? x : y;
73}
74const choice1 = choose(10, 20);
75const choice2 = choose<string>('apple', 'orange');
76
77class Collection<T> {
78  items: T[] = [];
79
80  constructor(...args: T[]) {
81    if (!args) return;
82
83    for (const arg of args) this.items.push(arg);
84  }
85}
86const col = new Collection<number>(1, 2, 3);
87const col2 = new Collection('a', 'b', 'c');
88
89function f(a: string): number {
90  return 42;
91}
92
93foo(f(null));
94foo(null);
95
96foo(() => {
97  f(null);
98});
99
100bar(() => {
101  f(null);
102}, null, f(null));
103
104bar(() => {
105  bar(() => {
106    f(null);
107  }, null, f(null));
108}, null, foo(f(null)));
109
110type PropDecorator = () => void;
111let Builder: PropDecorator;
112
113// this test is useless until we use custom tsc
114@Builder
115function buildSwiper() {
116  f(null)
117  foo(null) {
118    f(null)
119      foo(null) {
120        f(null)
121        foo(() => {
122        f(null)
123      })
124    }
125    .foo(null)
126  }
127}
128
129foo_ets(f(null));
130foo_ets(null);
131foo_ets(() => {
132  f(null);
133});
134
135new TestClassJ(f(null));
136new TestClassJ(null);
137new TestClassJ(() => {
138  f(null);
139});
140
141new TestClassJ_ets(f(null));
142new TestClassJ_ets(null);
143new TestClassJ_ets(() => {
144  f(null);
145});
146
147bar_ets(() => {
148  f(null);
149}, null, f(null));
150bar_ets(() => {
151  bar_ets(() => {
152    f(null);
153  }, null, f(null));
154}, null, foo_ets(f(null)));
155
156new TestClassK(() => {
157  f(null);
158}, null, f(null));
159new TestClassK(() => {
160  new TestClassK(() => {
161    f(null);
162  }, null, f(null));
163}, null, foo(f(null)));
164
165new TestClassK_ets(() => {
166  f(null);
167}, null, f(null));
168new TestClassK_ets(() => {
169  new TestClassK_ets(() => {
170    f(null);
171  }, null, f(null));
172}, null, foo_ets(f(null)));
173
174
175