import { log } from "./utils.uts"; type AsyncOptions = { type: string; success: (res: string) => void; fail: (res: string) => void; complete: (res: string) => void; }; /** * 导出一个属性 */ export const MAX = 100; /** * 导出一个同步方法 * @returns */ export function testSync(msg: string) { console.log("log test"); log("log test1"); return { msg: `hello ${msg}`, }; } /** * 导出一个同步方法(触发了数组越界异常) */ export function testSyncError() { const arr: string[] = []; console.log(arr[1]); } /** * 导出一个带callback的同步方法 * @param opts */ export function testSyncWithCallback(opts: AsyncOptions) { if (opts.type == "success") { opts.success("success"); } else { opts.fail("fail"); } opts.complete("complete"); return { name: "testSyncWithCallback" }; } /** * 导出一个异步方法 * @returns */ export async function testAsync(opts: AsyncOptions) { if (opts.type == "success") { opts.success("success"); } else { opts.fail("fail"); } opts.complete("complete"); return { name: "testAsync" }; } type TestOptions = { name: string; callback: (res: string) => void; }; export class Test { id: number; name: string; static type: string = "Test"; constructor(id: number, options: TestOptions) { this.id = id; this.name = options.name; options.callback("Test.constructor"); } static testClassStaticSyncWithCallback(opts: AsyncOptions): UTSJSONObject { return testSyncWithCallback(opts); } static async testClassStaticAsync(opts: AsyncOptions): Promise { const res = await testAsync(opts); return res; } testClassSyncWithCallback(opts: AsyncOptions): UTSJSONObject { return testSyncWithCallback(opts); } async testClassAsync(opts: AsyncOptions): Promise { const res = await testAsync(opts); return res; } }