Skip to content
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

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

Closed
700software opened this issue Mar 15, 2022 · 2 comments · Fixed by #48538
Closed
Labels
Duplicate An existing issue was already created Fix Available A PR has been opened for this issue

Comments

@700software
Copy link

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?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 16, 2022
@RyanCavanaugh
Copy link
Member

See #47599

@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 Fix Available A PR has been opened for this issue
Projects
None yet
3 participants