Promise.all以角度9和打字稿3.8.3解决

时间:2020-06-03 07:32:48

标签: javascript node.js typescript promise

我正在尝试使用Promise.allSettled,但我一直保持TSLint的geeting错误

我使用:

  • 角度9
  • 打字稿3.8.3
  • 节点13.9.0

这是我的tsconfig:

  {
    "compileOnSave": false,
    "compilerOptions": {
        "baseUrl": "./",
        "outDir": "./dist/out-tsc",
        "sourceMap": true,
        "declaration": false,
        "downlevelIteration": true,
        "experimentalDecorators": true,
        "module": "esnext",
        "moduleResolution": "node",
        "importHelpers": true,
        "target": "es2015",
        "typeRoots": ["node_modules/@types"],
        "lib": ["es2015", "dom"],
        "paths": {
            "@ing/*": ["src/app/features/*"]
        }
    },
    "angularCompilerOptions": {
        "fullTemplateTypeCheck": true,
        "strictInjectionParameters": true
    }
}

这是TSlint标记错误的地方:

 Promise.allSettled(loadingPromises).then(results=>{
      results.forEach( (status, value) =>{
        console.log(status,value);
      })
    });

Te代码运行完美,T​​Slint引发的错误是:

TS2339:类型“ PromiseConstructor”上不存在属性“ allSettled”。

1 个答案:

答案 0 :(得分:2)

在这里回答问题: https://stackoverflow.com/a/60276174/11057988

建议的解决方法:

declare interface PromiseConstructor {
    allSettled(promises: Array<Promise<any>>): Promise<Array<{status: 'fulfilled' | 'rejected', value?: any, reason?: any}>>;
}