Property 'finally' does not exist on type

2019-06-16 17:01发布

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

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:

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!

4条回答
再贱就再见
2楼-- · 2019-06-16 17:29

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

Thanks to everyone who helped me with this.

查看更多
淡お忘
3楼-- · 2019-06-16 17:30

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

查看更多
小情绪 Triste *
4楼-- · 2019-06-16 17:47

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

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

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

查看更多
Luminary・发光体
5楼-- · 2019-06-16 17:54

For TypeScript 3.3.3, just add es2018.promise to your tsconfig.json - compilerOptions.lib config like this:

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

If you use vscode, maybe you should reload window after adding this option to your tsconfig.json file

查看更多
登录 后发表回答