You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在TypeScript 4.3.5(Ionic环境)中支持顶层await并解决ts(1378)错误?

Fixing TS1378 (Top-level await not allowed) in TypeScript 4.3.5 + Ionic

Let's break down why you're hitting this TS1378 error even after setting the recommended target and module options, and walk through fixes that should resolve it.

First, a quick recap: TS1378 triggers when you use top-level await (outside an async function) in a context where TypeScript doesn't recognize the file as an ES module that supports this feature. Your tsconfig looks mostly correct, but there are a few gaps or conflicts that might be causing the issue.

Step 1: Fix the tsconfig.json syntax error

First off, your provided tsconfig is missing a closing } at the end. That might be a copy-paste mistake, but fixing it ensures TypeScript parses the config correctly:

{
  "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" ]
}

Step 2: Address top-level await limitations in Ionic's build pipeline

Even with the correct tsconfig, Ionic's underlying build tools (like Webpack or Vite) might not fully support top-level await in all contexts, especially with TypeScript 4.3.5 (which is a bit older). A reliable workaround is to wrap your storage initialization in an async function and export a Promise that resolves to the initialized store:

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

const store = new Storage();

// Wrap initialization in an async IIFE and export the promise
export default (async () => {
  await store.create();
  return store;
})();

Then, when you import the store elsewhere, you'll need to await it to get the initialized instance:

import storePromise from './path-to-your-store-file';

// Use in an async context
async function someFunction() {
  const store = await storePromise;
  await store.set('key', 'value');
}

Step 3: Verify module consistency

If you still want to use top-level await directly, double-check these:

  • Ensure your project's package.json includes "type": "module" (this tells Node.js and build tools to treat .ts files as ES modules). Note: Some Ionic templates might not set this by default, so adding it could resolve module mismatches.
  • Confirm that your isolatedModules: true setting isn't conflicting. While top-level await works with isolated modules, TypeScript requires strict ES module compliance here—make sure there are no CommonJS-style exports (like module.exports) in the same file.

Why your initial config didn't work

TypeScript 4.3.5 does support top-level await with module: ESNext and target: es2017, but the error often stems from build tooling overriding or ignoring your tsconfig settings. Ionic's pipeline might still be treating the file as a CommonJS module behind the scenes, which blocks top-level await. The async wrapper approach avoids this by moving the await into a function scope.

内容的提问来源于stack exchange,提问作者LoyalPotato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 21:52:46