如何在 Visual Studio 2017 Office 加载项 TypeScript 项目中填充 Promise

How to Polyfill Promise in Visual Studio 2017 Office Add-in TypeScript Project

我在 Office 加载项中使用 TypeScript,我想专门使用 async / await 函数。该项目无法编译 "TS2468 TypeScript Cannot find global value 'Promise'."

我在这里读到我必须为 Promise 创建一个 polyfill,但到目前为止还没有弄清楚如何让 polyfill 在 Visual Studio 2017 中工作。我正在尝试使用 core -js,并使用 npm install core-js 将其安装到项目中。我可以看到 core-js 安装在 node_modules 下。 npm 还创建了一个 package.json 文件,其中包含以下内容:

{ "requires": true, "lockfileVersion": 1, "dependencies": { "core-js": "^2.5.3" } }

这是我的 tsconfig.json 文件: { "compilerOptions": { "skipLibCheck": true, "moduleResolution": "node" }, "exclude": [ "node_modules" ] }

我在 FunctionFile.ts 的顶部声明了 require('core-js');,但错误仍然存​​在。

我遵循了这个问题中提供的指导:

使用我基于此创建的相同插件 link: https://docs.microsoft.com/en-us/office/dev/add-ins/develop/convert-javascript-to-typescript

我将以下内容添加到我的测试 TypeScript 文件中:

(function () {
    Office.initialize = function (reason) {
        (window as any).Promise = OfficeExtension.Promise;
    };
})();

async function test() {
    return 'hello';
}

构建项目时仍然出现同样的错误。 "TS2468 TypeScript Cannot find global value 'Promise'." 我也在顶部尝试了 (window as any).Promise = OfficeExtension.Promise;

似乎可以通过将以下 lib 属性 添加到 compilerOptions 对象 tsconfig.json 文件来解决这个问题:

"lib": [ "es5", "dom", "es2015.promise" ]

换句话说,将 ts.config 文件的内容更新为如下所示:

{
  "compilerOptions": {
    "skipLibCheck": true,
    "moduleResolution": "node",
    "lib": [ "es5", "dom", "es2015.promise" ]
  },
  "exclude": [
    "node_modules"
  ]
}