Skip to content

Generic function has function in props that does not detect return type when param added #48279

Closed

Description

Bug Report

🔎 Search Terms

Posted question to StackOverflow:
Generic React/Typescript function has function in props but does not detect return type when param added
but it occurs to me this may be a bug!?

🕗 Version & Regression Information

  • Tested in TypeScript 4.5.4

⏯ Playground Link

Playground link with relevant code

💻 Code

function simplified<T>(props: { generator: () => T, receiver: (t: T) => any }) {
}

function whatIWant<T>(props: { generator: (bob: any) => T, receiver: (t: T) => any }) {
}

function nonObject<T>(generator: (bob: any) => T, receiver: (t: T) => any) {
}

simplified({ generator: () => 123, receiver: (t) => console.log(t + 2) })
whatIWant({ generator: (bob) => bob ? 1 : 2, receiver: (t) => console.log(t + 2) })
nonObject((bob) => bob ? 1 : 2, (t) => console.log(t + 2))

🙁 Actual behavior

The whatIWant function is showing an error because it thinks t is unknown type.

The other two (simplified and nonObject are correctly seeing t as a number type.

image

🙂 Expected behavior

Above code (all three cases) should have no errors.

I'm particularly confused by the difference between simplified and whatIWant. Why is this correct behaviour, or is it a bug?

Activity

  1. added
    Duplicate
    on Mar 16, 2022
  2. RyanCavanaugh commented on Mar 16, 2022

    @RyanCavanaugh
    Member

    See #47599

  3. typescript-bot commented on Mar 19, 2022

    @typescript-bot
    Contributor

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

  4. added
    Fix Available
    on Apr 3, 2022
  5. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateFix Available

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions