[promise.allsettled] Fix type declarations for non-tuple inputs (#36815)

* added type definitions for promise.allsettled

* code style update

* code style and compatibility fixes

* removed the (hopefully not needed) tslint disable flag

* more code style fixes

* code style fix

* fixed the result type for iterable non-tuple inputs, made the types more practical

* fixed code style
This commit is contained in:
Martin Jurča 2019-07-12 00:54:04 +02:00 committed by Armando Aguirre
parent 42b73bb4d1
commit 57356feb55
4 changed files with 7 additions and 6 deletions

View File

@ -1,10 +1,10 @@
import { PromiseRejection, PromiseResolution, PromiseResult } from './types';
type PromiseTuple<T extends [unknown, ...unknown[]]> = {[P in keyof T]: Promise<T[P]>};
type PromiseResultTuple<T extends [unknown, ...unknown[]]> = {[P in keyof T]: PromiseResult<T[P], unknown>};
type PromiseResultTuple<T extends [unknown, ...unknown[]]> = {[P in keyof T]: PromiseResult<T[P]>};
declare function allSettled(): Promise<[]>;
declare function allSettled<T extends [unknown, ...unknown[]]>(iterable: PromiseTuple<T>): Promise<PromiseResultTuple<T>>;
declare function allSettled<T>(iterable: Iterable<T>): Promise<T[]>;
declare function allSettled<T>(iterable: Iterable<T>): Promise<Array<PromiseResult<T>>>;
export = allSettled;

View File

@ -28,6 +28,6 @@ export = exportedImplementation;
declare namespace exportedImplementation {
type PromiseRejection<E> = PromiseRejectionType<E>;
type PromiseResolution<T> = PromiseResolutionType<T>;
type PromiseResult<T, E> = PromiseResultType<T, E>;
type PromiseResult<T, E = unknown> = PromiseResultType<T, E>;
type PromiseResultTuple<T extends [unknown, ...unknown[]]> = PromiseResultTupleType<T>;
}

View File

@ -1,6 +1,7 @@
import allSettled = require("promise.allsettled");
type Result<T extends [unknown, ...unknown[]]> = Promise<allSettled.PromiseResultTuple<T>>;
type ArrayResult<T, E = unknown> = Promise<Array<allSettled.PromiseResult<T>>>;
allSettled(); // $ExpectType Promise<[]>
// the $ExpectType comment does not work with the following constraints unfortunately
@ -8,4 +9,4 @@ const r0: Result<[number]> = allSettled([Promise.resolve(0)]);
const r1: Result<[number, string]> = allSettled([Promise.resolve(1), Promise.resolve('a')]);
const r2: Result<[never, boolean]> = allSettled([Promise.reject<never>(null), Promise.resolve(true)]); // tslint:disable-line use-default-type-parameter
const input = [0, 1, 2, 3, 4];
const r3: Promise<number[]> = allSettled(input);
const r3: ArrayResult<number> = allSettled(input);

View File

@ -8,7 +8,7 @@ export interface PromiseRejection<E> {
reason: E;
}
export type PromiseResult<T, E> = PromiseResolution<T> | PromiseRejection<E>;
export type PromiseResult<T, E = unknown> = PromiseResolution<T> | PromiseRejection<E>;
export type PromiseTuple<T extends [unknown, ...unknown[]]> = {[P in keyof T]: Promise<T[P]>};
export type PromiseResultTuple<T extends [unknown, ...unknown[]]> = {[P in keyof T]: PromiseResult<T[P], unknown>};
export type PromiseResultTuple<T extends [unknown, ...unknown[]]> = {[P in keyof T]: PromiseResult<T[P]>};