简体   繁体   English

如何在打字稿中支持顶级等待?

[英]How to support top-level awaits in typescript?

I'm using version 4.3.5 of typescript with Ionic and have ts(1378) error.我正在使用带有 Ionic 的 4.3.5 版打字稿并且出现 ts(1378) 错误。

Here are is the tsconfig:这是tsconfig:

{
  "compilerOptions": {
    "target": "es2017",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "sourceMap": true,
    "noImplicitAny": true,
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "ESNext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

And the code where I'm getting the error:以及我收到错误的代码:

import { Storage } from '@ionic/storage';

const store = new Storage();
await store.create();

export default store;

According to the error all I needed to change was the target to >=es2017 and the module to esnext or system, but I have that and it still shows me the error.根据错误,我需要更改的只是> = es2017的目标和esnext或系统的模块,但我有它,它仍然向我显示错误。

Don't know if it's relevant but you can see my folder structure here .不知道它是否相关,但您可以在此处查看我的文件夹结构。

Thanks in advance :)提前致谢 :)

The issue doesn't come from your tsconfig but from webpack.问题不是来自您的 tsconfig,而是来自 webpack。

Top level await is still an experimental feature in webpack.顶级 await 仍然是 webpack 中的一个实验性功能。 You would have to include experiments: { topLevelAwait: true } in your webpack.config to make it work.您必须在 webpack.config 中包含experiments: { topLevelAwait: true }才能使其工作。

https://webpack.js.org/configuration/experiments/ https://webpack.js.org/configuration/experiments/

However create-react-app manages the webpackconfig files for you and you would have to eject from create-react-app to access them.但是 create-react-app 为您管理 webpackconfig 文件,您必须从 create-react-app 中弹出才能访问它们。

Ejecting from create-react-app is usually not recommended, especially to add an experimental feature.通常不建议从 create-react-app 中弹出,尤其是添加实验性功能。 It could induce more issues than it resolves.它可能引发的问题多于解决的问题。

Here are the steps to follow if you still want to try:如果您仍想尝试,请按照以下步骤操作:

npm run eject

add experiments: { topLevelAwait: true } in the return statement of the newly created config/webpack.config.js在新创建的 config/webpack.config.js 的 return 语句中添加experiments: { topLevelAwait: true }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM