Skip to content

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

Closed
@700software

Description

@700software

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions