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.
🙂 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?