Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PromiseValue type #75

Merged
merged 6 commits into from Feb 6, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 7 additions & 13 deletions source/promise-value.d.ts
Expand Up @@ -4,21 +4,15 @@ If the type is not a `Promise`, the type itself is returned.

@example
import {PromiseValue} from './promise-value';
import {asyncFunction} from 'api';

let data: PromiseValue<ReturnType<typeof asyncFunction>>;
type AsyncData = Promise<string>;
let asyncData: PromiseValue<AsyncData> = Promise.resolve('ABC');

async function setValue(): void {
data = await asyncFunction();
}
type Data = PromiseValue<AsyncData>;
let data: Data = await asyncData;

setValue();

// Here's an example of using this with non-Promise types.
function getNumber(): number {
return 2;
}

const number: PromiseValue<ReturnType<typeof getNumber>> = getNumber();
// Here's an example that shows how this type reacts to non-Promise types.
type SyncData = PromiseValue<string>;
let syncData: SyncData = getSyncData();
resynth1943 marked this conversation as resolved.
Show resolved Hide resolved
*/
export type PromiseValue<PromiseType, Otherwise = PromiseType> = PromiseType extends Promise<infer Value> ? Value : Otherwise;