Coder Social home page Coder Social logo

Proposal: ThenArg about type-fest HOT 15 CLOSED

sindresorhus avatar sindresorhus commented on May 16, 2024 3
Proposal: ThenArg

from type-fest.

Comments (15)

streamich avatar streamich commented on May 16, 2024 3

@kainiedziela

What's the advantage of using UnwrapPromise<PromiseLike<T>> over T?

T might not be available. It is basically the same as using ReturnType<() => T> over T.

from type-fest.

resynth1943 avatar resynth1943 commented on May 16, 2024 2

This has been added as PromiseValue.
/cc @sindresorhus @5c077yP

from type-fest.

sindresorhus avatar sindresorhus commented on May 16, 2024 1

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.

streamich avatar streamich commented on May 16, 2024 1

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

image

or this

image

from type-fest.

kainiedziela avatar kainiedziela commented on May 16, 2024 1

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.

sindresorhus avatar sindresorhus commented on May 16, 2024

Just noticed #42, which is basically this.

from type-fest.

kainiedziela avatar kainiedziela commented on May 16, 2024

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.

WORMSS avatar WORMSS commented on May 16, 2024

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.

fabiospampinato avatar fabiospampinato commented on May 16, 2024

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.

fregante avatar fregante commented on May 16, 2024

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.

5c077yP avatar 5c077yP commented on May 16, 2024

@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.

5c077yP avatar 5c077yP commented on May 16, 2024

@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.

Schniz avatar Schniz commented on May 16, 2024

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.

resynth1943 avatar resynth1943 commented on May 16, 2024
type UnwrapPromiseRecursive<TPromise> = TPromise extends Promise<infer TValue> ? UnwrapPromiseRecursive<TValue> : TPromise;

That can do recursive promise unwrapping.

from type-fest.

sindresorhus avatar sindresorhus commented on May 16, 2024

Fixed by #75.

from type-fest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.