Inconsistent compiler behavior when using a lambda expression with multiple parameters
I have two examples, which should behave the same, but doesnt:
// Example 1
var callback = (param1, param2) => {
// Do someting
};
getDeferred().then(callback);
// Example 2
getDeferred().then((param1, param2) => {
// Do someting
});
Example 2 generates a compile error, while Example 1 doesn't. Should they not behave the same way? If wrapping the lambda function in paranthesis, Example 2 now compiles as well.
This seems really inconsistent, and Example 2 is really hard to debug, when you're not aware of the paranthesis requirement. Why does the compiler behave like this?
Can you post a self-contained example that demonstrates the problem you're describing? This doesn't give me an error on either line:
declare function getDeferred(): { then(cb: (arg1, arg2) => void): void; };
// Example 1
var callback = (param1, param2) => {
// Do someting
};
getDeferred().then(callback);
// Example 2
getDeferred().then((param1, param2) => {
// Do someting
});
Its hard to create a self-contained example, as I see now, it has something to do with the use of jQuery. I'll try to post my example:
// ... Within a class
private loadData () {
// load() returns a JQueryPromise<T>
// where T is a Backbone.Model or a Backbone.Collection<Backbone.Model>
var voucher = this.requiredData.requiredVoucherData.load();
var types = this.requiredData.purchaseTypes.load();
return $.when<Backbone.Model | Backbone.Collection<Backbone.Model>>(voucher, types);
}
// Now compiler thinks that voucher is of type <Backbone.Model | Backbone.Collection<Backbone.Model>>
// and types is of type <any>
// Wrapping the anonynomous function in paranthesis solves the problem
this.loadData().then((voucher, types) => {
// ...
});
Let me know if you need anymore information, I'll be happy to help where I can.
This is because then() contextually types voucher and types.
This is usually desirable because we can infer the types of your parameters given the types then() expects, meaning your code is better checked, but you can stop it by adding a type assertion for your arrow function: <any>((voucher, types) => { /* ... */ }).
So we can better understand your use case, is there any specific reason why you find this undesirable?
That sounds perfectly reasonable and desirable the way you put it. What I don't understand, however, is the need for the paranthesis. Three examples below:
// Same as above; compile error (with or without <any> type assertion)
this.loadData().then((voucher, types) => {
// ...
});
// Works (with or without <any> type assertion)
this.loadData().then(((voucher, types) => {
// ...
}));
// Works
var callback = (voucher, types) => {
// ...
};
this.loadData().then(callback);
You might have answered my question already, but to answer yours, what I find undesirable is the need for the paranthesis in the first example.
I find it inconsistent that I cannot simply copy the content of my callback variable, and paste it into the then function and get the same result. Compiler behavior aside, as a programmer, wouldn't you expect this to work?
Are you using an old version of the compiler? Parentheses use to "block" contextual typing, but no longer do. That's the discrepancy between the first two examples.
In the third example, in an ideal world of infinite computing resources the compiler would emulate the exact flow of all data through your program and infer all kinds of types that you might have wanted. That would be slow, and wouldn't be possible to reason about as a human -- if you thought this example was hard to reduce, imagine if we had to see literally your entire codebase to be able to explain any particular behavior. That's the trade-off.
This is indeed correct. I'm using version 1.4, and the contextural typing paranthesis block was removed in 1.5.
I'll update to 1.5 and see how that goes.
Same issue for me in 1.5. I'll close it for now, and add another, more specific, issue, when I've found the exact reason for this behavior. Thanks!