Skip to content

Commit

Permalink
feat: add PromiseOr utility
Browse files Browse the repository at this point in the history
  • Loading branch information
andnp committed Feb 25, 2019
1 parent a66b789 commit 63b50b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ export declare class Tagged<N extends string> { private _nominal_: N; }
* @returns a type that is equal only to itself, but can be used like its contained type `T`
*/
export type Nominal<T, N extends string> = T & Tagged<N>;

/**
* Returns the given type or a Promise containing that type.
* @param T the inner type for the promise
* @returns a the type union with a promise containing the given type
*/
export type PromiseOr<T> = Promise<T> | T;
11 changes: 11 additions & 0 deletions test/utils/PromiseOr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from 'ava';
import { assert } from '../helpers/assert';

import { PromiseOr } from '../../src';

test('Will give back a promise containing given type union the type itself', t => {
type got = PromiseOr<string>;
type expected = Promise<string> | string;

assert<got, expected>(t);
});

0 comments on commit 63b50b8

Please sign in to comment.