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
exportfunctionfoo<T,K>(props: {seed: T;producer: (x: T)=>K;consumer: (data: K)=>void;}){//}foo({seed: "foo",producer: (seed)=>{// type of `seed` is stringreturnseed;},consumer: (data)=>{// type of `data` is unknown }})
Expected behavior: TypeScript infers K to a string, so the type of data is string.
Actual behavior:data variable is unknown.
The text was updated successfully, but these errors were encountered:
I believe this is also because TS cannot perform inference on the same object multiple times? #34865
It is why I use the builder pattern.
The builder pattern lets me break apart inference on one giant object into multiple inferences on multiple smaller objects/values #34907
@AnyhowStep is took me 2 months exactly to understand what you mean :))
I'm able to reproduce this behaviour by swapping from f({ a, b }) to f(a, b). The non-object version works correctly. However, I'm trying to type a TSX component which always receives an object.
TypeScript Version: 3.7-beta
Search Terms: inference, generic functions
Code
Expected behavior: TypeScript infers
K
to astring
, so the type ofdata
isstring
.Actual behavior:
data
variable isunknown
.The text was updated successfully, but these errors were encountered: