如何在Visual Studio 2017 Office加载项TypeScript项目中实现Polyfill Promise [英] How to Polyfill Promise in Visual Studio 2017 Office Add-in TypeScript Project

查看:158
本文介绍了如何在Visual Studio 2017 Office加载项TypeScript项目中实现Polyfill Promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Office加载项中使用TypeScript,并且我想专门使用async/await函数.该项目无法使用"TS2468 TypeScript找不到全局值'Promise'"进行编译.

I am using TypeScript in an Office Add-in, and I want to use async / await functions specifically. The project fails to compile with "TS2468 TypeScript Cannot find global value 'Promise'."

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

I have read on here that I have to create a polyfill for Promise, but so far have not been able to figure out how to get a polyfill working in Visual Studio 2017. I am attempting to use core-js, and have installed it in to the project using npm install core-js. I can see core-js is installed under node_modules. npm also created a package.json file with the following:

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

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

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

Here is my tsconfig.json file: { "compilerOptions": { "skipLibCheck": true, "moduleResolution": "node" }, "exclude": [ "node_modules" ] }

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

I have require('core-js'); declared at the top of FunctionFile.ts, but the error persists.

我遵循了此问题中提供的指导: TypeScript版本的Office Addins文件不起作用

I followed the guidance provided in this question : Office Addins file in its TypeScript version doesn't work

使用我基于此链接创建的相同加载项: https://docs. microsoft.com/en-us/office/dev/add-ins/develop/convert-javascript-to-typescript

Using the same add-in I created based on this link: https://docs.microsoft.com/en-us/office/dev/add-ins/develop/convert-javascript-to-typescript

我在测试TypeScript文件中添加了以下内容:

I added the following to my test TypeScript file:

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

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

构建项目时,我仍然遇到相同的错误. "TS2468 TypeScript找不到全局值'Promise'."我也尝试过在顶部使用(window as any).Promise = OfficeExtension.Promise;.

I still get the same error when building the project. "TS2468 TypeScript Cannot find global value 'Promise'." I also tried it with (window as any).Promise = OfficeExtension.Promise; at the top.

推荐答案

通过将以下lib属性添加到 tsconfig.json 文件的compilerOptions对象中,可以解决此类问题:

Seems like this can be resolved by adding the following lib property to the compilerOptions object your tsconfig.json file:

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

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

In other words, update the contents of your ts.config file to look like this:

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

这篇关于如何在Visual Studio 2017 Office加载项TypeScript项目中实现Polyfill Promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆