属性 'allSettled' 在类型 'PromiseConstructor' 上不存在 'PromiseConstructor'.ts(2339)

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

我正在尝试将 Promise.allSettled API 与 TypeScript 一起使用。代码在这里:

server.test.ts:

it('should partial success if QPS > 50', async () => {
  const requests: any[] = [];
  for (let i = 0; i < 51; i++) {
    requests.push(rp('http://localhost:3000/place'));
  }
  await Promise.allSettled(requests);
  // ...
});

但是 TSC 抛出一个错误:

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

我已经将这些值添加到 tsconfig.json 中的 lib 选项中:

tsconfig.json:

{
  "compilerOptions": {
    /* Basic Options */
    "target": "ES2015" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
    "lib": [
      "ES2015",
      "ES2016",
      "ES2017",
      "ES2018",
      "ES2019",
      "ES2020",
      "ESNext"
    ] 
   // ...
}

TypeScript 版本:"typescript": "^3.7.3"

那么,我该如何解决呢?我知道我可以使用替代模块,但我对原生使用 TypeScript 很好奇。

The types for Promise.allSettled() 仅在 1 月份合并,显然将在 TypeScript 3.8 中发布。

作为临时解决方案,您可以自己为函数声明一个模拟类型:

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

它是 ES2020 并且处于第 4 阶段,因此如果没有 polyfill 就无法在任何地方使用。它被输入并合并到 TS。尝试安装最新的@types/node 包,看看是否能正常运行。

更新:看起来它将 es2020.promise 添加到库中,当它着陆时。

更新:npm i typescript@3.8.0-beta呜呜呜!

要在 Linux 上获得此 运行,我需要最新的打字稿版本:

npm install -g typescript@latest

然后在您的 tsconfig 中,您当前需要 ES2020.Promise 库。我的完整 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
    }
  }
}

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

将它用于旧版 Typescript 的解决方法

await (Promise as any).allSettled(promises);

有一个 shim 库可以提供相同的功能 (https://www.npmjs.com/package/promise.allsettled),但请注意它需要显式导入 (var allSettled = require('promise.allsettled');)。

如果您使用的是 typescript,等效的类型化包是 https://www.npmjs.com/package/@types/promise.allsettled,但请注意,类型名称与官方 ES2020 实现不匹配,因此如果您想要完全直接替换,您将需要添加一个新的 TS 文件来对类型进行别名并使其在全局命名空间中可用:

import { PromiseRejection, PromiseResolution, PromiseResult } from 'promise.allsettled';

// 
export {}

// TechDebt: Remove once project is migrated to ES2020
// Global type aliases are required because the promise.allsettled shim doesn't match the types of the actual 
// ES2020 implementation
declare global {
    export type PromiseFulfilledResult<T> = PromiseResolution<T>;
    
    export type PromiseRejectedResult = PromiseRejection<any>;
    
    export type PromiseSettledResult<T> = PromiseResult<T, any>;

    export class PromiseConstructor {
        /**
         * Creates a Promise that is resolved with an array of results when all
         * of the provided Promises resolve or reject.
         * @param values An array of Promises.
         * @returns A new Promise.
         */
         allSettled<T extends readonly unknown[] | readonly [unknown]>(values: T):
             Promise<{ -readonly [P in keyof T]: PromiseSettledResult<T[P] extends PromiseLike<infer U> ? U : T[P]> }>;

        /**
         * Creates a Promise that is resolved with an array of results when all
         * of the provided Promises resolve or reject.
         * @param values An array of Promises.
         * @returns A new Promise.
         */
        allSettled<T>(values: Iterable<T>): Promise<PromiseSettledResult<T extends PromiseLike<infer U> ? U : T>[]>;
    }
}

对我有用的是简单地将“es2020”添加到 tsconfig.json 文件的“lib”属性中

@types/node 需要更新

工作版本在这里:

npm i @types/node@17.0.33

您可以查看来自 here

的版本