Skip to content

Commit

Permalink
feat(types): Added meta functions PromiseOf<T> and PromiseElement<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
grantila committed May 8, 2019
1 parent 948a14f commit c24d865
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ The functions are standalone and depends on no particular Promise implementation

This library is written in TypeScript but is exposed as ES7 (if imported as `already`) and ES5 (if imported as `already/es5`). Typings are provided too, so any TypeScript project using this library will automatically get full type safety of all the functions.

# Types
* [PromiseOf\<P\>](#PromiseOf)
* [PromiseElement\<P\>](#PromiseElement)

# Functions

* [delay](#delay)
Expand All @@ -33,6 +37,27 @@ This library is written in TypeScript but is exposed as ES7 (if imported as `alr
* [wrapFunction](#wrapfunction)
* [funnel](#funnel)

---

# Types

## PromiseOf

`PromiseOf< P >` returns the Promise wrapped value of `P`, unless it's already a promise, where the promise itself is returned instead.

* For `P` (being `Promise< E >`), it returns `P`
* For non-promise `P`, it returns `Promise< P >`


## PromiseElement

`PromiseElement< P >` returns the element type of a promise, or the type itself if it isn't wrapped in a promise.

* For `P` (being `Promise< E >`), it returns `E`
* For non-promise `P`, it returns `P`


# Functions

## delay

Expand Down
19 changes: 19 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ export default {
};


/**
* Returns the Promise wrapped value of P, unless it's already a promise, where
* the promise itself is returned instead.
*
* For P being Promise<E>, it returns P
* For non-promise P, it returns Promise<P>
*/
export type PromiseOf< P > = P extends Promise< infer U > ? P : Promise< P >;

/**
* Returns the element type of a promise, or the type itself if it isn't
* wrapped in a promise.
*
* For P being Promise<E>, it returns E
* For non-promise P, it returns P
*/
export type PromiseElement< P > = P extends Promise< infer U > ? U : P;


function toReadonlyArray< T >( arr: ConcatArray< T > ): ReadonlyArray< T >
{
/* istanbul ignore else */
Expand Down

0 comments on commit c24d865

Please sign in to comment.