typescript 类型 'Promise<void>' 上不存在属性 'finally'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52079031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:37:21  来源:igfitidea点击:

Property 'finally' does not exist on type 'Promise<void>'

angulartypescriptionic-framework

提问by JJ Pell

I am trying to use the finally method on a promise but I keep getting this error.

我正在尝试对承诺使用 finally 方法,但我不断收到此错误。

Property 'finally' does not exist on type 'Promise<void>'.

I have looked up similar questions to this which have suggested adding "es2018.promise" to the "lib" array in tsconfig.js. This then produces the following error:

我查过类似的问题,建议在 tsconfig.js 的“lib”数组中添加“es2018.promise”。这会产生以下错误:

typescript error
        Argument for '--lib' option must be: 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'esnext', 'dom',
        'dom.iterable', 'webworker', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator',
        'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol',
        'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string',
        'es2017.intl', 'esnext.asynciterable'.

Any help resolving this is greatly appreciated!

非常感谢任何帮助解决这个问题!

回答by slideshowp2

For TypeScript 3.3.3, just add es2018.promiseto your tsconfig.json- compilerOptions.libconfig like this:

对于TypeScript 3.3.3,只需像这样添加es2018.promise到您的tsconfig.json-compilerOptions.lib配置中:

"lib": ["es2015", "es2016", "dom", "es2018.promise"]

"lib": ["es2015", "es2016", "dom", "es2018.promise"]

If you use vscode, maybe you should reload windowafter adding this option to your tsconfig.jsonfile

如果您使用vscode,也许您应该reload window在将此选项添加到您的tsconfig.json文件后

回答by JBC

ES2018 support was not added until TypeScript 2.7. Upgrading to that version or later resolves this error.

直到TypeScript 2.7才添加 ES2018 支持。升级到该版本或更高版本可解决此错误。

回答by rod howard

While TypeScript 2.7 might support it you need to target: "ESNEXT" in your tsconfig.json.

虽然 TypeScript 2.7 可能支持它,但您需要在 tsconfig.json 中定位:“ESNEXT”。

Which means that your code will probably not run in older browsers or node.js versions.

这意味着您的代码可能无法在较旧的浏览器或 node.js 版本中运行。

I'm using: "@types/promise.prototype.finally": "^2.0.2", import { shim as finallyShim } from 'promise.prototype.finally'; finallyShim();

我正在使用:"@types/promise.prototype.finally": "^2.0.2", import { shim as finallyShim } from 'promise.prototype.finally'; finallyShim();

回答by JJ Pell

I fixed this by using this https://www.npmjs.com/package/promise.prototype.finallyas it seems to be an issue with ionic not typescript. I imagine it will be fixed in the near future.

我通过使用这个https://www.npmjs.com/package/promise.prototype.finally解决了这个问题,因为它似乎是 ionic 而非 typescript 的问题。我想它会在不久的将来得到修复。

Thanks to everyone who helped me with this.

感谢所有帮助过我的人。

回答by Yao Li

Make sure you yarn/npm install first, the error disappears after installation if you already have the es2018.promisein tsconfig.json.

确保你纱/ NPM安装第一,错误安装后,如果你已经有了消失es2018.promisetsconfig.json