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
Proposal: ThenArg #38
Comments
Can't say I've ever personally needed this, but I'll leave it open for feedback and votes. If we were to accept it, I think it should be called @fabiospampinato @kainiedziela @WORMSS Thoughts? |
Just noticed #42, which is basically this. |
A promise is usually typed, so wrapping that in another type seems redudant to me. In the above example using the |
I would think it would be most likely be useful in a declaration file but I've had a long day and struggling to think of a real world example currently. |
There might be some use cases for this, like crafting some other complicated type maybe, but the use case provided by @5c077yP is a bit strange IMHO: /** @type {ThenArg<ReturnType<loadData>>} */
let data; // <-- data is untyped if not defining the @type above
data = await loadData(); Here the return type of If instead the function to Definitely Maybe this type should check if the passed type is a |
This doesn't seem specific to Promises. It can work for any generic like: GenericArg<Partial<Options>> === Options
GenericArg<Array<Function>> === Function
GenericArg<NodeListOf<TextNode>> === TextNode Or Unwrap<Promise<Data>> === Data Edit: this doesn't seem to be possible at all now; an |
@sindresorhus sorry for the maybe not awesome example i gave. I fully agree with @streamich , infering the type of an async function is an often use case, where the result of this function is not publicly exported/available to use directly. The case I tried to show is basically the same. The result of the async function Any further comments on this? Could I somehow help getting this forward? |
Okay, I can see this being useful, but doesn't it promote bad habits? Why would the typeof a promise's result not be available to use directly? Shouldn't you type it and then the What's the advantage of using |
@kainiedziela might be true that this isn't a good habit. I'm using jsdoc in js extensively , so we're not using typescript directly and there not all type information is always available, e.g. you have to I guess this also applies to transitional phases, where types are just getting added step by step. |
|
I have implemented it in a couple of projects. Some times it was recursive ( |
type UnwrapPromiseRecursive<TPromise> = TPromise extends Promise<infer TValue> ? UnwrapPromiseRecursive<TValue> : TPromise; That can do recursive promise unwrapping. |
This has been added as |
Fixed by #75. |
Hey there,
would be nice to include the helper type to unwrap a PromiseLike:
e.g.
my main motivation is when using jsdoc in js I often declare a
let
without any initialisation and assign a value in a later closure (mostly when running mocha tests), e.g.:The text was updated successfully, but these errors were encountered: