以 es5 为目标时,如何将 Promises 与 TypeScript 结合使用?
How do I use Promises with TypeScript when targeting es5?
Promise.all<any, any>(ajaxRequests).then(()=> {
console.log("done");
});
以上代码给出了以下编译错误:
TS2585: 'Promise' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
我不熟悉这个编译器 lib
选项是什么,如果我要更改它会有什么影响。
我正在尝试针对较旧的浏览器,我相信需要支持 es5
。我假设这可以通过 transpiling/polyfilling 来完成?我的打字稿配置是:
{
"compilerOptions": {
"sourceMap": true,
"target": "es5",
"declaration": true,
"removeComments": false,
"module" : "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
},
"include": [
"src/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
将以下内容添加到您的编译器选项中:
"lib": [
"dom",
"es5",
"es2015.promise"
]
lib
选项有更详细的描述 here。
Here's target
和 lib
区别的解释。
话虽如此,如果您可以接受使用 es6
,那么我认为您可以将 target
设置为 "es6"
而不是乱用 lib
。
Promise.all<any, any>(ajaxRequests).then(()=> {
console.log("done");
});
以上代码给出了以下编译错误:
TS2585: 'Promise' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
我不熟悉这个编译器 lib
选项是什么,如果我要更改它会有什么影响。
我正在尝试针对较旧的浏览器,我相信需要支持 es5
。我假设这可以通过 transpiling/polyfilling 来完成?我的打字稿配置是:
{
"compilerOptions": {
"sourceMap": true,
"target": "es5",
"declaration": true,
"removeComments": false,
"module" : "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
},
"include": [
"src/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
将以下内容添加到您的编译器选项中:
"lib": [
"dom",
"es5",
"es2015.promise"
]
lib
选项有更详细的描述 here。
Here's target
和 lib
区别的解释。
话虽如此,如果您可以接受使用 es6
,那么我认为您可以将 target
设置为 "es6"
而不是乱用 lib
。