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
Hey there. I was experimenting with generics, and noticed that type inference didn't work as I expected for this particular case. I'm not entirely sure if this is an actual blind spot of the type inference mechanism, or if my declaration doesn't mean what I think it means. it's a bit similar to the “Curiously recurring template pattern” described on #5949, but isn't actually the CRTP, since there's no actual recursion involved. Thanks in advance for looking into this issue.
TypeScript Version:
1.8.10
Code
interfaceIFoo{foo: 'foo';// Could be anything}interfaceIWrapped<I>{getObject(): I;}functionunwrap<I,TextendsIWrapped<I>>(x: T): I{returnx.getObject();}letx: IWrapped<IFoo>/* = ... */;lety=unwrap<IFoo,IWrapped<IFoo>>(x);// Type is IFoo (correct)letz=unwrap(x);// Type is {} (incorrect?)
Expected behavior:
I expected the compiler to infer the type of z as being IFoo, like when using explicit generic parameters on y.
Actual behavior:
The compiler inferred the type of z as being {}.
The text was updated successfully, but these errors were encountered:
This is by design. Inference only infers from the arguments passed to the function to the declared parameters, it does not factor in constraints. Constrains are simply checks that are performed after inference completes. In your example there's really no reason to have two type parameters (and you should never have type parameters you don't need), but there are indeed situations where you want to infer multiple type parameters from a single argument. #7234 is about those scenarios.
Hey there. I was experimenting with generics, and noticed that type inference didn't work as I expected for this particular case. I'm not entirely sure if this is an actual blind spot of the type inference mechanism, or if my declaration doesn't mean what I think it means. it's a bit similar to the “Curiously recurring template pattern” described on #5949, but isn't actually the CRTP, since there's no actual recursion involved. Thanks in advance for looking into this issue.
TypeScript Version:
1.8.10
Code
Expected behavior:
I expected the compiler to infer the type of
z
as beingIFoo
, like when using explicit generic parameters ony
.Actual behavior:
The compiler inferred the type of
z
as being{}
.The text was updated successfully, but these errors were encountered: