-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generic type arguments not inferred, default to {} #5884
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
Comments
TypeScript doesn't crave up type arguments from the context. Type parameters of The question remains why a function declaration defaults its parameters to implicit |
Yes, that's what I gathered, and why I asked if that could be changed so the context influences the type inference. This is what I'm used to from e.g. Haskell but of course typescript has many features that make this more complicated. |
Hindley-Milner type system as in Haskell doesn't work very well with subtyping and inheritance, also the union type is problematic to work with, so it seems it was a trade-off that TypeScript design team had to make http://stackoverflow.com/questions/7234095/why-is-scalas-type-inference-not-as-powerful-as-haskells |
Yes, that's what I was afraid of. I'm still hoping there is a way to do something local (but less local than now) to get cases like this working. |
I have that problem frequently with promises. If you write const mypromise: Promise<void> = Promise.resolve(); TypeScript errors with function resolve<R>(value?: R | Thenable<R>): Promise<R>; I don't pass anything to Promise.resolve(), therefore the type argument should be the type of const mypromise: Promise<void> = Promise.resolve(undefined); |
@felixfbecker That's how and why it works as it does in your case. const mypromise: Promise<void> = Promise.resolve<void>(); to manually specify the type argument, rather than relying on inference may be more along the lines of what you want. |
@weswigham thanks for your answer, but I think this is wrong behaviour... Why the empty object? If I don't pass an argument to a function, the value of that argument will in fact be |
@felixfbecker the behavior of inferring |
Discussion should either continue here or on #5254. |
closing this in favor of #5254. |
…e type Promise<string> itself and needs a little help. See the following for more discussion: microsoft/TypeScript#5884 microsoft/TypeScript#5254
I sometimes run into scenarios where it's clear to me what a generic type argument should be, but typescript fails to infer it. I think this is because inference only looks at the call site, and not the context. The example I just ran into was this:
In the last case it infers type parameter
A
to{}
, causing it to fail since{}
has no propertyfoo
. But from the context (comparing
should return a function(x : { foo : string }, y : { foo : string }) : number)
because it is used as an argument tosort
onx
) it should be clear thatA
should be{ foo : string }
.Is there any way this kind of inference could be added, or is this too complicated due to overloads/subtyping/etc.
P.S. There seem to be several issues about things like this, but they all seem slightly different. If they're not, my apologies. I'd like to have a place where I can track this issue, so I would appreciate any pointers to another ticket, a FAQ, etc.
The text was updated successfully, but these errors were encountered: