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

TS报错Cannot find global type 'Promise',tsconfig.json配置问题排查

动态import编译报错:找不到Promise类型的问题

问题场景

我在index.ts中尝试通过动态import导入模块,代码如下:

const functions = await import("./avs-directory/index").then(
    (m) => {
        return m;
    }
);

执行tsc -p tsconfig.json编译时出现以下错误:

tsc -p tsconfig.json
error TS2318: Cannot find global type 'Promise'.

index.ts:51:25 - error TS2711: A dynamic import call returns a 'Promise'. Make sure you have a declaration for 'Promise' or include 'ES2015' in your '--lib' option.

51 const functions = await import("./avs-directory/index").then(
                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 2 errors in the same file, starting at: index.ts:51

我的tsconfig.json配置如下:

{
  "extends": "assemblyscript/std/portable.json",
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig to read more about this file */

    /* Projects */

    /* Language and Environment */
    "target": "es2022",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "lib": ["ES2015", "ES2015.Promise"],                                        /* Specify a set of bundled library declaration files that describe the target runtime environment. */

    /* Modules */
    "module": "es2022",                                /* Specify what module code is generated. */

    /* JavaScript Support */
    "allowJs": true,                                  /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
    "outDir": "./dist",                                   /* Specify an output folder for all emitted files. */

    "allowSyntheticDefaultImports": true,             /* Allow 'import x from y' when a module doesn't have a default export. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */

    /* Completeness */
    // "skipDefaultLibCheck": true,                      /* Skip type checking .d.ts files that are included with TypeScript. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  },
  "ts-node": {
    "compilerOptions": {
      "module": "ES2015"
    }
  },
  "include": ["./**/*.ts", "index.ts", "runner.ts", "env.d.ts"],
  "exclude": ["node_modules", "dist"]
}

我已根据错误提示修改了tsconfig.json,但仍报错,请问我的tsconfig.json存在什么问题?


问题原因

核心问题在于你继承了assemblyscript/std/portable.json配置文件。AssemblyScript的portable配置是专门为编译到WebAssembly设计的,它会覆盖TypeScript默认的环境库声明,即便你手动在lib数组中添加了ES2015和ES2015.Promise,也会被这个继承的配置覆盖,导致Promise类型无法被识别。

解决方案

  1. 移除AssemblyScript配置继承:如果你的项目不是要编译为WebAssembly,直接删除"extends": "assemblyscript/std/portable.json"这一行。此时TypeScript会使用默认环境配置,配合你设置的lib数组就能正确识别Promise类型。
  2. 简化动态import代码:你的代码中同时混用了await和.then(),可以简化为更清晰的写法:
const functions = await import("./avs-directory/index");
  1. 保留AssemblyScript配置的替代方案:如果确实需要保留该继承配置,你需要手动补充Promise的类型声明,或者调整lib数组为更完整的环境库集合,但这种方式复杂度较高,仅推荐在WebAssembly编译场景下使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 14:03:14