Comments (15)
What's the advantage of using
UnwrapPromise<PromiseLike<T>>
overT
?
T
might not be available. It is basically the same as using ReturnType<() => T>
over T
.
from type-fest.
This has been added as PromiseValue
.
/cc @sindresorhus @5c077yP
from type-fest.
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 UnwrapPromise
.
@fabiospampinato @kainiedziela @WORMSS Thoughts?
from type-fest.
An often use case is to use in in conjunction with ReturnType
when inferring return type of an async function. For example, it could be a library that exports a type of an async function, and you want to get the return type of it.
UnwrapPromise<ReturnType<Fn>>
like this
or this
from type-fest.
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 UnwrapPromise
type would be obsolete?
What's the advantage of using UnwrapPromise<PromiseLike<T>>
over T
?
from type-fest.
Just noticed #42, which is basically this.
from type-fest.
A promise is usually typed, so wrapping that in another type seems redudant to me. In the above example using the loadData
type would be sufficent, correct?
from type-fest.
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.
from type-fest.
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 loadData
never changes, so you might as well just write that in the TSDoc comment (ie. if it's Promise<object>
just write object
, if it's Promise<MyData>
just write MyData
).
If instead the function to await
is being passed dynamically we would still have to constrain the type of the allowed function, otherwise we would just get any
out, and I think just writing the expected return type instead of ThenArg<ReturnType<TypeOfAllowedFunctions>>
will probably be cleaner 🤔
Definitely UnwrapPromise
is a much better name.
Maybe this type should check if the passed type is a PromiseLike
rather than a Promise
, just to have a more general implementation.
from type-fest.
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 Unwrap
type has to extend
something.
from type-fest.
@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 loadData
is not available but the variable declaration is split from the initialisation , which is why the typescript compiler is not able (at least for me) to infer the type correctly.
Any further comments on this? Could I somehow help getting this forward?
from type-fest.
@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 export
things which should be available, but that's not always the case.
I guess this also applies to transitional phases, where types are just getting added step by step.
from type-fest.
I have implemented it in a couple of projects. Some times it was recursive (UnwrapPromise<Promise<Promise<T>>>
) to work like await
from type-fest.
type UnwrapPromiseRecursive<TPromise> = TPromise extends Promise<infer TValue> ? UnwrapPromiseRecursive<TValue> : TPromise;
That can do recursive promise unwrapping.
from type-fest.
Fixed by #75.
from type-fest.
Related Issues (20)
- IfAny circular constraint in TS 5.4+ HOT 2
- Make the second argument to RequireAllOrNone optional HOT 1
- Use `NoInfer`?
- Type for serializable objects? HOT 3
- Proposal: SetKeyRequired with key required, but allow undefined value HOT 2
- OverrideProperties changes undefined as optional HOT 2
- Exclude / Pick type in Paths / only add leaf nodes HOT 2
- `Proposal`: add option with exclude/keep specified type on deep type HOT 5
- Unify `Get` and `Paths` / `PickDeep` etc HOT 1
- `Paths` is unable to escape dots in property names
- DistributedOmit may not need to constraint the second type argument HOT 6
- A type util to produce an alternation of function overloads. HOT 1
- PositiveIntegerString type HOT 4
- Type-fest based prelude HOT 1
- ArrayExceptLastElement type HOT 7
- Zero is not exported by ... HOT 1
- `And` and `Or` is not exported at `index.d.ts`
- ConditionalKeys does not correctly filter out `never` type HOT 2
- Paths<T> should handle recursive types
- `PickDeep` strips `null` HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from type-fest.