Property 'allSettled' does not exist on type 'PromiseConstructor'.ts(2339)

后端 未结 3 1269
慢半拍i
慢半拍i 2020-12-16 11:14

I\'m trying to use the Promise.allSettled API with TypeScript. Code here:

server.test.ts:

it(\'shou         


        
相关标签:
3条回答
  • 2020-12-16 11:50

    It's ES2020 and at Stage 4, so not available everywhere without a polyfill. It got typed and merged into TS. Try installing the latest @types/node package and see if that pulls it in.

    Update: Looks like it will be adding es2020.promise to the libs, when it does land.

    Update: npm i typescript@3.8.0-beta woot woot!

    0 讨论(0)
  • 2020-12-16 11:54

    The types for Promise.allSettled() were only merged in January, and will apparently be released in TypeScript 3.8.

    As an interim solution, you can declare a mock-ish type for the function yourself:

    declare interface PromiseConstructor {
        allSettled(promises: Array<Promise<any>>): Promise<Array<{status: 'fulfilled' | 'rejected', value?: any, reason?: any}>>;
    }
    
    0 讨论(0)
  • 2020-12-16 12:06

    To get this running on Linux, I needed the latest typescript version:

    sudo npm install -g typescript@latest
    

    Then in your tsconfig you currently need the ES2020.Promise lib. My full tsconfig:

    {
      "compilerOptions": {
        "sourceMap": true,
        "module": "commonjs",
        "target": "es5",
        "jsx": "react",
        "esModuleInterop": true,
        "allowJs": true,
        "outDir": "./dist",
        "lib": [
          "ES2020.Promise",
        ]
      },
      "include": [
        "./src"
      ],
      "exclude": [
        "./node_modules",
        "./build"
      ],
      "compileOnSave": true,
      "parserOptions": {
        "ecmaFeatures": {
          "jsx": true
        }
      }
    }
    

    Usage: const results = await Promise.allSettled(BatchOfPromises);

    0 讨论(0)
提交回复
热议问题