Skip to content

Generic inference from property return type fails only if parameters exist and JSX attribute syntax is used #52786

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
henry-bishop opened this issue Feb 15, 2023 · 2 comments · Fixed by #52837
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@henry-bishop
Copy link

henry-bishop commented Feb 15, 2023

Bug Report

🔎 Search Terms

  • Generics
  • JSX
  • Inconsistent
  • Inference

🕗 Version & Regression Information

This is the behavior in every version I've tested including 4.7.4 up to 5.0.0-dev.20230215
(Before 4.7.4, the issue occurred regardless of whether JSX syntax was used)

⏯ Playground Link

Playground link with relevant code

💻 Code

interface Props<T> {
  a: (x: string) => T;
  b: (arg: T) => void;
}

function Foo<T>(props: Props<T>) {
  return <div />;
}

// Works fine, T is inferred as 'number' through the return value of 'a'
<Foo
  a={() => 10}
  b={(arg) => { arg.toString(); }}
/>;

// If 'a' takes any parameters, inference stops working and T becomes 'unknown'
<Foo
  a={(x) => 10}
  b={(arg) => { arg.toString(); }} // ERROR: 'arg' is of type 'unknown'
/>;

// (since 4.7.4) Works fine if the object literal syntax is used instead
<Foo {...{
  a: (x) => 10,
  b: (arg) => { arg.toString(); },
}} />;

🙁 Actual behavior

Foo<T> fails to infer T from the return value of a if the function provided for a accepts any parameters, and the JSX attribute syntax is used.

🙂 Expected behavior

Foo<T> should infer T from the return value of a regardless of whether its signature has parameters, and regardless of whether the JSX attribute syntax is used or not.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Feb 21, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Feb 21, 2023
@RyanCavanaugh
Copy link
Member

Work item here is to port some of the logic added to address #47599 to also apply to JSX

@Andarist
Copy link
Contributor

This is already fixed by #52837 and awaits review 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants