Skip to content

Awaited return type of generic parameter not assignable to itself #50251

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

Closed
wbt opened this issue Aug 10, 2022 · 2 comments
Closed

Awaited return type of generic parameter not assignable to itself #50251

wbt opened this issue Aug 10, 2022 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@wbt
Copy link

wbt commented Aug 10, 2022

Bug Report

🔎 Search Terms

generic function parameter 2322 return type

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about functions and common bugs that aren't bugs
  • I was unable to test this on versions before 4.5 because the Awaited keyword didn't exist.

⏯ Playground Link

Playground link with relevant code

💻 Code

type FetcherFunction = (
    identifier: number
) => Promise<string>; //fixed 'string' not tied to parameter types is part of simplification for example
type demoFine1 = ReturnType<FetcherFunction>; // Promise<string>
type demoFine2 = Awaited<ReturnType<FetcherFunction>>; //string
async function getterDemo<
    F extends FetcherFunction = FetcherFunction
>(
    fetcherFn: F,
    identifier: number,
) : Promise<Awaited<ReturnType<F>>> {
    let demo3: F;
    let result = await fetcherFn(identifier); //result is a string
    return result; //ERROR TS2322: Type 'string' is not assignable to type 'Awaited<ReturnType<F>>'
    //In the motivating example, lots more is done with the result and the return type is a type derived from it,
    //but the simpler example seems like it shouldn't be throwing an error: whether Awaited<ReturnType<F> is a
    //simple string or something more complex, that is what the promise returned by this async function resolves to.
}

🙁 Actual behavior

Error as noted.

🙂 Expected behavior

No TS2322 error; the above code works fine. The awaited return type of fetcherFn should be assignable to the return (resolve) value, which is the awaited return type of that function type.

@MartinJohns
Copy link
Contributor

MartinJohns commented Aug 10, 2022

The issue is the deferred resolving of conditional types involving unbound type parameters. Plenty of issues about that.

Get rid of ReturnType<> and it will work fine.

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Aug 19, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants