-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Argument inference change in 3.4.4 #31297
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
@ahejlsberg is this an effect of instantiating the |
Actually, |
This is working as intended. In the call to In half-way worked before because of a bug that was fixed in #30856. You can see the inconsistency with some quick info hovering. For example, inside |
If we expand on this example a bit, the behaviour still seems a little strange to me. If the function wrap<Self>(
creator: (self: Self) => Self
): Self {
const self = {} as Self;
Object.assign(self, creator(self));
return self;
} Then we get two different return types depending on if we reference const knownSelf = wrap(() => ({ val: 2 })); // type of knownSelf is { val: number }
const unknownSelf = wrap(self => ({ val: 2 })); // type of unknownSelf is unknown From your explanation I vaguely understand why this is the case, but is that behaviour desired? |
I was hoping for the following behaviour: const counter = // inferred
wrap(self => ({ // unknown
val: 0, // unknown
// unknown
increment(): number { // unknown
self.val += 1; // inferred
return self.val // inferred
}, // unknown
// unknown
decrement(): number { // unknown
self.val -= 1; // inferred
return self.val // inferred
}, // unknown
})); |
Did you consider this instead: const counter = {
val: 0,
increment() {
this.val += 1;
return this.val;
},
decrement() {
this.val -= 1;
return this.val;
},
}; Everything is inferred correctly and no need to double allocate the object. |
TypeScript Version: 3.4.4-dev.20190507
Search Terms: 3.4.4 generic argument inference
Code
Expected behavior:
Expected to compile without any errors, with the type of
self
inferred as{ val: number, func(): number }
. This works as expected from v3.1 -> 3.4.3Actual behavior:
As of 3.4.4 this no longer works. Looks like
self
is being inferred as{}
. TypeScript emits the following error atreturn self.val
:I tested this with [email protected] and it looks like
self
is being inferred asunknown
. It emits the following error:Playground Link:
works in older version of TypeScript
The text was updated successfully, but these errors were encountered: