You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Like in React.useCallbackdeclarefunctionuseCallback<TextendsFunction>(fn: T): T;declarefunctionex1(callback: (x: number)=>void): void;declarefunctionex2(callback?: (x: number)=>void): void;declarefunctionex3(callback: ((x: number)=>void)|5): void;ex1(x=>{})// OK. x: numberex1(useCallback(x=>{}));// OK. x: numberex2(x=>{})// OK. x: numberex2(useCallback(x=>{}));// 7006 (no implicit any)ex3(x=>{})// OK. x: numberex3(useCallback(x=>{}));// 7006 (no implicit any)
🙁 Actual behavior
The type of x in the code example above is not inferred when callback is a union or optional.
🙂 Expected behavior
x should be inferred as number.
Edit: simplified example
The text was updated successfully, but these errors were encountered:
In the first case you "luck out" because the parameter type of ex1 is a function type. That is compatible with the constraint of the return type of useCallback, so useCallback can use it as a valid inference candidate. When x => {} needs to be contextually typed, it has an inference candidate from the returned contextual type.
In the second and third cases with ex2 and ex3, we look at the contextual type of useCallback() and find union types - undefined | ((x: number) => void) and 5 | ((x: number) => void) respectively. Neither is compatible with the constraint Function.
I do wonder if it would make sense to try to infer the intersection of the contextual type with the constraint itself.
In the meantime, one alternative way to write useCallback might be the following:
I attempted to filter the contextual type by applicable constraints and I also attempted to create an intersection out of those two in this PR. Even taking aside the correctness - the results were incredibly slow. Perhaps my method wasn't correct - this was quite a quick hack, just to check if I would get the correct results (and I did, for this case at hand - but not always for all of the tests).
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The type of
x
in the code example above is not inferred whencallback
is a union or optional.🙂 Expected behavior
x
should be inferred asnumber
.Edit: simplified example
The text was updated successfully, but these errors were encountered: