/* * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { foo, bar } from "./oh_modules/ohos_lib" function restSpread() { const arr = [1, 2, 3]; function test(a, ...t) { console.log(a); // 1 console.log(t[0]); // 2 console.log(t[1]); // 3 } test(1, ...arr); } class MyGenerator { public *getValues() { // you can put the return type Generator, but it is ot necessary as ts will infer let index = 1; while (true) { yield index; index = index + 1; if (index > 10) { break; } } } } function defaultTypeParam(i: t, j: tt) { const c = i; const s = j; } function arrowFunctionTest() { const empty = () => {}; // no return type const double = (x: number) => x * 2; // no return type const square = (x): number => x * x; // no param type const sqrt = (x) => Math.sqrt(x); // shortcut syntax const even = [1, 2, 3, 4, 5, 6].filter((x) => x % 2 === 0); // shortcut syntax const foo = (x: number, y): boolean => x == y; // types are partly omitted const generic = (t: T, e: E) => t; // Generic lambda } function fooThis(i: number): void { this.c = 10; } class C { c: number; m = fooThis; } function choose(x: T, y: T): T { return Math.random() < 0.5 ? x : y; } const choice1 = choose(10, 20); const choice2 = choose('apple', 'orange'); class Collection { items: T[] = []; constructor(...args: T[]) { if (!args) return; for (const arg of args) this.items.push(arg); } } const col = new Collection(1, 2, 3); const col2 = new Collection('a', 'b', 'c'); function f(a: string): number { return 42; } foo(f(null)); foo(null); foo(() => { f(null); }); bar(() => { f(null); }, null, f(null)); bar(() => { bar(() => { f(null); }, null, f(null)); }, null, foo(f(null))); type PropDecorator = () => void; let Builder: PropDecorator; // this test is useless until we use custom tsc @Builder function buildSwiper() { f(null) foo(null) { f(null) foo(null) { f(null) foo(() => { f(null) }) } .foo(null) } }