[promise.allsettled] Add new type declarations (#36314)

* 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
This commit is contained in:
Martin Jurča 2019-06-21 09:48:49 +02:00 committed by Daniel Rosenwasser
parent 362f0f4373
commit ffebba08e1
10 changed files with 116 additions and 0 deletions

5
types/promise.allsettled/auto.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
// This file exists only to reflect the existence of the auto.js file in the source package, which has a side-effect
// when imported.
declare const exports: {};
export = exports;

View File

@ -0,0 +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>};
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[]>;
export = allSettled;

33
types/promise.allsettled/index.d.ts vendored Normal file
View File

@ -0,0 +1,33 @@
// Type definitions for promise.allsettled 1.0
// Project: https://github.com/ljharb/promise.allsettled#readme
// Definitions by: Martin Jurča <https://github.com/jurca>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.1
import implementation = require('./implementation');
import getPolyfill = require('./polyfill');
import shim = require('./shim');
import {
PromiseRejection as PromiseRejectionType,
PromiseResolution as PromiseResolutionType,
PromiseResult as PromiseResultType,
PromiseResultTuple as PromiseResultTupleType,
} from './types';
type ExportedImplementationType = typeof implementation & {
getPolyfill: typeof getPolyfill,
implementation: typeof implementation,
shim: typeof shim,
};
declare const exportedImplementation: ExportedImplementationType;
export = exportedImplementation;
// This seems to be the only way to export these types here without colliding with the "export =" syntax.
declare namespace exportedImplementation {
type PromiseRejection<E> = PromiseRejectionType<E>;
type PromiseResolution<T> = PromiseResolutionType<T>;
type PromiseResult<T, E> = PromiseResultType<T, E>;
type PromiseResultTuple<T extends [unknown, ...unknown[]]> = PromiseResultTupleType<T>;
}

View File

@ -0,0 +1,5 @@
import implementation = require('./implementation');
declare function getPolyfill(): typeof implementation;
export = getPolyfill;

View File

@ -0,0 +1,11 @@
import allSettled = require("promise.allsettled");
type Result<T extends [unknown, ...unknown[]]> = Promise<allSettled.PromiseResultTuple<T>>;
allSettled(); // $ExpectType Promise<[]>
// the $ExpectType comment does not work with the following constraints unfortunately
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);

View File

@ -0,0 +1,3 @@
declare function requirePromise(): void;
export = requirePromise;

5
types/promise.allsettled/shim.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
import implementation = require('./implementation');
declare function shimAllSettled(): typeof implementation;
export = shimAllSettled;

View File

@ -0,0 +1,29 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"auto.d.ts",
"implementation.d.ts",
"index.d.ts",
"polyfill.d.ts",
"promise.allsettled-tests.ts",
"requirePromise.d.ts",
"shim.d.ts",
"types.d.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

14
types/promise.allsettled/types.d.ts vendored Normal file
View File

@ -0,0 +1,14 @@
export interface PromiseResolution<T> {
status: 'fulfilled';
value: T;
}
export interface PromiseRejection<E> {
status: 'rejected';
reason: E;
}
export type PromiseResult<T, E> = 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>};