Skip to content

Returned function with generic doesn't complain when provided type are incompatible #53610

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
cavalier38 opened this issue Mar 31, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@cavalier38
Copy link

Bug Report

With a generic function returning a function that has an argument base return type, which is later passed to another function.
It then is accepted by the later function even if the type used in the generic are incompatible.
For example a custom react hook which is passed down components.

When not the exact return type is used, but instead new type is written with the same structure.
Then the incomparibility in the type used as generic is detected.

🔎 Search Terms

returned
function
generic

🕗 Version & Regression Information

Tested on 5.0.2 (Playground)
The syntax was accepted down to 4.7.4, all versions respond the same.

  • This is the behavior in every version I tried (down to 4.7.4, below the syntax is not accepted, and I reviewed the FAQ for entries about generics

⏯ Playground Link

Playground link with relevant code
Playground link with working alternative

💻 Code

function useTest<GType>(init: GType) {
  const getValue = <GKey extends keyof GType>(key: GKey): GType[GKey] => {
    return init[key];
  };
  return getValue;
}

type TGetValue<GType> = ReturnType<typeof useTest<GType>>;

function pass(getValue: TGetValue<{ aString: string; needed: number }>) {}

function test() {
  const myType = {
    aNumber: 2,
    aString: 'hello',
  };
  const getValue = useTest(myType);
  pass(getValue);
}

🙁 Actual behavior

getValue is accepted as correct type for pass.

🙂 Expected behavior

getValue is missing 'needed' which is required by pass.

With alternative solution, changing TGetValue from ReturnType to a type on its own:

type TGetValue<GType> = <GKey extends keyof GType>(key: GKey) => GType[GKey];

This report 'needed' is missing.
With this alternative code, when using version 3.3.3 it also accepts getValue like it does when using ReturnType.

@RyanCavanaugh
Copy link
Member

Duplicate of #48070

@RyanCavanaugh RyanCavanaugh marked this as a duplicate of #48070 Mar 31, 2023
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 31, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' 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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants