Skip to content

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

Closed
@henry-bishop

Description

@henry-bishop

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions