From ffebba08e197810e99be8f5a862e3ec7c8362aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Jur=C4=8Da?= Date: Fri, 21 Jun 2019 09:48:49 +0200 Subject: [PATCH] [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 --- types/promise.allsettled/auto.d.ts | 5 +++ types/promise.allsettled/implementation.d.ts | 10 ++++++ types/promise.allsettled/index.d.ts | 33 +++++++++++++++++++ types/promise.allsettled/polyfill.d.ts | 5 +++ .../promise.allsettled-tests.ts | 11 +++++++ types/promise.allsettled/requirePromise.d.ts | 3 ++ types/promise.allsettled/shim.d.ts | 5 +++ types/promise.allsettled/tsconfig.json | 29 ++++++++++++++++ types/promise.allsettled/tslint.json | 1 + types/promise.allsettled/types.d.ts | 14 ++++++++ 10 files changed, 116 insertions(+) create mode 100644 types/promise.allsettled/auto.d.ts create mode 100644 types/promise.allsettled/implementation.d.ts create mode 100644 types/promise.allsettled/index.d.ts create mode 100644 types/promise.allsettled/polyfill.d.ts create mode 100644 types/promise.allsettled/promise.allsettled-tests.ts create mode 100644 types/promise.allsettled/requirePromise.d.ts create mode 100644 types/promise.allsettled/shim.d.ts create mode 100644 types/promise.allsettled/tsconfig.json create mode 100644 types/promise.allsettled/tslint.json create mode 100644 types/promise.allsettled/types.d.ts diff --git a/types/promise.allsettled/auto.d.ts b/types/promise.allsettled/auto.d.ts new file mode 100644 index 0000000000..331bed541d --- /dev/null +++ b/types/promise.allsettled/auto.d.ts @@ -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; diff --git a/types/promise.allsettled/implementation.d.ts b/types/promise.allsettled/implementation.d.ts new file mode 100644 index 0000000000..7d00ef2b16 --- /dev/null +++ b/types/promise.allsettled/implementation.d.ts @@ -0,0 +1,10 @@ +import { PromiseRejection, PromiseResolution, PromiseResult } from './types'; + +type PromiseTuple = {[P in keyof T]: Promise}; +type PromiseResultTuple = {[P in keyof T]: PromiseResult}; + +declare function allSettled(): Promise<[]>; +declare function allSettled(iterable: PromiseTuple): Promise>; +declare function allSettled(iterable: Iterable): Promise; + +export = allSettled; diff --git a/types/promise.allsettled/index.d.ts b/types/promise.allsettled/index.d.ts new file mode 100644 index 0000000000..a9338e59da --- /dev/null +++ b/types/promise.allsettled/index.d.ts @@ -0,0 +1,33 @@ +// Type definitions for promise.allsettled 1.0 +// Project: https://github.com/ljharb/promise.allsettled#readme +// Definitions by: Martin JurĨa +// 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 = PromiseRejectionType; + type PromiseResolution = PromiseResolutionType; + type PromiseResult = PromiseResultType; + type PromiseResultTuple = PromiseResultTupleType; +} diff --git a/types/promise.allsettled/polyfill.d.ts b/types/promise.allsettled/polyfill.d.ts new file mode 100644 index 0000000000..d32bf138e5 --- /dev/null +++ b/types/promise.allsettled/polyfill.d.ts @@ -0,0 +1,5 @@ +import implementation = require('./implementation'); + +declare function getPolyfill(): typeof implementation; + +export = getPolyfill; diff --git a/types/promise.allsettled/promise.allsettled-tests.ts b/types/promise.allsettled/promise.allsettled-tests.ts new file mode 100644 index 0000000000..b154040d38 --- /dev/null +++ b/types/promise.allsettled/promise.allsettled-tests.ts @@ -0,0 +1,11 @@ +import allSettled = require("promise.allsettled"); + +type Result = Promise>; + +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(null), Promise.resolve(true)]); // tslint:disable-line use-default-type-parameter +const input = [0, 1, 2, 3, 4]; +const r3: Promise = allSettled(input); diff --git a/types/promise.allsettled/requirePromise.d.ts b/types/promise.allsettled/requirePromise.d.ts new file mode 100644 index 0000000000..1f47680eac --- /dev/null +++ b/types/promise.allsettled/requirePromise.d.ts @@ -0,0 +1,3 @@ +declare function requirePromise(): void; + +export = requirePromise; diff --git a/types/promise.allsettled/shim.d.ts b/types/promise.allsettled/shim.d.ts new file mode 100644 index 0000000000..532151ecf8 --- /dev/null +++ b/types/promise.allsettled/shim.d.ts @@ -0,0 +1,5 @@ +import implementation = require('./implementation'); + +declare function shimAllSettled(): typeof implementation; + +export = shimAllSettled; diff --git a/types/promise.allsettled/tsconfig.json b/types/promise.allsettled/tsconfig.json new file mode 100644 index 0000000000..03c7d52c08 --- /dev/null +++ b/types/promise.allsettled/tsconfig.json @@ -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" + ] +} diff --git a/types/promise.allsettled/tslint.json b/types/promise.allsettled/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/promise.allsettled/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/promise.allsettled/types.d.ts b/types/promise.allsettled/types.d.ts new file mode 100644 index 0000000000..715ee292f6 --- /dev/null +++ b/types/promise.allsettled/types.d.ts @@ -0,0 +1,14 @@ +export interface PromiseResolution { + status: 'fulfilled'; + value: T; +} + +export interface PromiseRejection { + status: 'rejected'; + reason: E; +} + +export type PromiseResult = PromiseResolution | PromiseRejection; + +export type PromiseTuple = {[P in keyof T]: Promise}; +export type PromiseResultTuple = {[P in keyof T]: PromiseResult};