如何在Typescript项目中实现顶层等待?

编程入门 行业动态 更新时间:2024-10-07 22:22:45

如何在Typescript<a href=https://www.elefans.com/category/jswz/34/1771421.html style=项目中实现顶层等待?"/>

如何在Typescript项目中实现顶层等待?

我正在尝试在我的项目中实现顶级等待,我在测试模块中尝试了顶级等待。我已在tsconfig.json中将目标更改为es2017,将模块更改为esnext,但这样做时出现错误,指出顶级'await'表达式仅在'module'选项设置为'esnext'或'system' ,并且“目标”选项设置为“ es2017”或更高。。我无法解决此错误。我使用的节点版本为13.14.0。有人可以帮我弄这个吗?

sample_validation_db.ts

import 'reflect-metadata';
import { ProductBrandSchema } from '../../../../api/modules/m3/schemas/m3_product_brand_schema';
import { ProductBrandModel } from '../../../../api/modules/m3/models/m3_product_brand_model';
import { TestHelper } from '../../../../tests/tests_core/utils/test_helper';
// Test suite for Product Brand Route
const productBrandSchema = new ProductBrandSchema();
const testModel = new ProductBrandModel(productBrandSchema);
const result = await TestHelper.retriveDataFromDb(testModel);
console.log(result);
describe('Product Brand Route', () => {
  it('validate collection', () => {
    expect(result).toEqual([]);
  }, 200000);
});

test_helper.ts

export class TestHelper {
  static async retriveDataFromDb(model: any) {
    const mongooseModel = model.getModel();
    const result = await mongooseModel.find({});
    return result;
  }
}

测试用例模块通过package.json中的脚本运行

"scripts": {
"validate-db": "jest --config=jest_validation_db.config.js --forceExit --detectOpenHandles --passWithNoTests --verbose false",
}

tsconfig.json

  "compilerOptions": {
    "target": "es2017",                       
    "module": "esnext", 
}
回答如下:

您需要在tsconfig.json中添加几个额外的属性,>

 "compilerOptions": {
   "target": "es2019",                       
   "module": "commonjs",
   "importHelpers": true,
   "downlevelIteration": true
   "lib": ["DOM", "ES2016", "ES2017", "ES2019"] 
}

为了使这个东西“起作用”,我希望我们需要npm i install tslib typescript

https://github/EnetoJara/agenda/blob/master/rest-api/src/controllers/lists.controller.ts

您拥有哪个版本的节点,打字稿和tslib?我粘贴了回购的链接,您可以看一下它

更多推荐

如何在Typescript项目中实现顶层等待?

本文发布于:2024-05-07 11:52:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目   如何在   Typescript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!