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 Unpromise type #42

Closed
wants to merge 3 commits into from
Closed

Conversation

streamich
Copy link

@streamich streamich commented Jun 27, 2019

Adds Unpromise type.

type Str = Unpromise<Promise<string>>;
const str: Str = 'string...';

@sindresorhus
Copy link
Owner

What’s the real-world use-case for this?

@streamich
Copy link
Author

streamich commented Jun 27, 2019

@sindresorhus if you want to get the return type of an async function for example.

type AsyncRequest = (...args: any) => Promise<'foobar'>;
type FooBar = Unpromise<ReturnType<AsyncRequest>>; // 'foobar'

@streamich
Copy link
Author

@sindresorhus sindresorhus mentioned this pull request Jul 2, 2019
@sindresorhus
Copy link
Owner

@streamich See discussion in #38

@sindresorhus
Copy link
Owner

@streamich Are you still interested in finishing this?

@@ -0,0 +1,10 @@
/**
Unwraps `Promise` and returns lifted type.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write about some real-world use-cases where it can be useful. (It can be hard sometimes for users to see where they would use something) - https://github.com/sindresorhus/type-fest/blob/master/.github/contributing.md#submitting-a-new-type

@example
```
type A = Promise<string>;
type InnerType = Unpromise<A>; // string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example here could be more realistic:

Example code block with a realistic example. - https://github.com/sindresorhus/type-fest/blob/master/.github/contributing.md#submitting-a-new-type

@sindresorhus
Copy link
Owner

If there has been any discussion somewhere about this type, include a link to it. For example, a discussion on the TypeScript issue tracker. - https://github.com/sindresorhus/type-fest/blob/master/.github/contributing.md#submitting-a-new-type

@@ -0,0 +1,10 @@
/**
Unwraps `Promise` and returns lifted type.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description could be clearer and more verbose on what it does.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/**
 * Returns the type that is wrapped inside a `Promise` type.
 * 
 * @example
 * import { asyncFunction } from 'api';
 * import { UnwrapPromise } from 'type-fest';
 * 
 * let data: UnwrapPromise<ReturnType<typeof asyncFunction>>;
 * 
 * async function setValue () {
 *   data = await asyncFunction();
 * }
 * 
 * setValue();
 */

How's this, bud?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

UnwrapPromise => Unpromise

@@ -71,6 +71,7 @@ Click the type names for complete docs.
- [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given properties.
- [`ReadonlyDeep`](source/readonly-deep.d.ts) - Create a deeply immutable version of a `object`/`Map`/`Set`/`Array` type.
- [`LiteralUnion`](source/literal-union.d.ts) - Create a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. Workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729).
- [`Unpromise`](source/unpromise.d.ts) - Returns type wrapped in `Promise`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [`Unpromise`](source/unpromise.d.ts) - Returns type wrapped in `Promise`.
- [`Unpromise`](source/unpromise.d.ts) - Returns the type that is wrapped inside a `Promise` type.

import {expectType, expectError} from 'tsd';
import {Unpromise} from '..';

type Str = Unpromise<Promise<string>>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use acronyms.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh god, I love you for saying that. 😛

@resynth1943
Copy link
Contributor

I'd be interested in taking over this, if you're happy with that, @sindresorhus.

@sindresorhus
Copy link
Owner

@resynth1943 Sure, go ahead 👍

@resynth1943
Copy link
Contributor

Gotcha. I'll whip up a PR. ❤️

@resynth1943
Copy link
Contributor

resynth1943 commented Jan 31, 2020

There are a few possible names we could use for this too.

  • UnboxPromise
  • UnwrapPromise
  • Depromise
  • Unpromise
  • PromiseValue
  • AsyncValue

Or should we stick with Unpromise?

@sindresorhus
Copy link
Owner

I like PromiseValue.

@resynth1943
Copy link
Contributor

Let's do it. ❤️

@resynth1943 resynth1943 mentioned this pull request Jan 31, 2020
@resynth1943
Copy link
Contributor

@sindresorhus #75.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants