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)
- [4.10.0][Regression] failure when type checking with exactOptionalPropertyTypes HOT 5
- IntRange Inclusive HOT 3
- Weird bug report - RangeError: Maximum call stack size exceeded when building HOT 1
- Update Merge type to accept N type arguments HOT 1
- Maintenance strategy: Considering splitting `type-fest` into smaller importable parts HOT 14
- OptionalExcept type HOT 2
- Why do we need to specify the `engines` field in package.json? HOT 1
- Require exactly one key of type `string` HOT 1
- Bug: Nextjs and type-fest camel-case.d.ts Type error: '?' expected.
- `JsonValue` can receive `type` but not `interface` HOT 1
- CamelCasedObjectDeep breaks tuple-types into array-types
- `UnionToUnorderedTuple` type HOT 2
- `NonEmptyObject` fails for objects with dynamic properties HOT 2
- Proposal: `ValueOfDeep<Type, 'key.deepKey'>` HOT 1
- Questions about the types located in the file `/source/basic.d.ts`
- ReadonlyDeep: add test for function or method returning an object? HOT 1
- `RepeatString` type
- `ArraySlice` should support spread array HOT 3
- TS 5.4.2 breaks `ConditionalKeys` and `SetParameterType` HOT 16
- `ReadonlyDeep` / `WritableDeep` not modifying arrays as of TS 5.3.3 HOT 3
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.