Skip to content

Commit

Permalink
Merge pull request #83 from andnp/PromiseOr
Browse files Browse the repository at this point in the history
Promise or
  • Loading branch information
andnp authored Feb 27, 2019
2 parents a66b789 + 29f8bc7 commit 1b1def6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ npm install --save-dev simplytyped

**[Utils](#utils)**

[NoInfer](#noinfer) - [Nominal](#nominal) - [Nullable](#nullable) - [Unknown](#unknown)
[NoInfer](#noinfer) - [Nominal](#nominal) - [Nullable](#nullable) - [PromiseOr](#promiseor) - [Unknown](#unknown)

**[Functions](#functions)**

Expand Down Expand Up @@ -583,6 +583,18 @@ test('Will make a type not nullable', t => {

```

### PromiseOr
Returns the given type or a Promise containing that type.
```ts
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);
});

```

### Unknown
A constant type that mimics an unknowable type.

Expand Down
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 1b1def6

Please sign in to comment.