TypeScript: Incorrect/overly strong `this` inference for POJOs
TypeScript Version:
nightly (1.9.0-dev.20160419)
Code
Ember.Object.extend({
init() {
this._super();
// ^~~~ error TS2339: Property '_super' does not exist on type '{ init(): void; }'.
...
}
});
Expected behavior:
this should be inferred as any
Actual behavior:
this is inferred to be { init(): void; }
This changed somewhere between typescript@1.9.0-dev.20160319 and typescript@1.9.0-dev.20160419. I believe it is unsafe to assume anything about the this on the “methods” on a POJO, as there are several common patterns in JavaScript that breaks this assumption (using POJOs as an “options” or “extensions” dictionary).
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 21 (14 by maintainers)
Commits related to this issue
- Lock typescript version for now pending https://github.com/Microsoft/TypeScript/issues/8191 — committed to glimmerjs/glimmer-vm by krisselden 8 years ago
- Unlock TypeScript dependency Since https://github.com/Microsoft/TypeScript/issues/8191 is now fixed — committed to glimmerjs/glimmer-vm by deleted user 8 years ago
On the other hand, we’ve gotten the request to type
thisinside object literal functions about eleventy million times already. 😕@RyanCavanaugh Are you sure that the eleventy million times are all cases where the object literal is the correct
this, vs. cases like.extend(POJO)ordefineClass(POJO)which wants a different behavior?In my JavaScript experience, the set of cases where
thisinside an object literal is actually referring to the object itself (as opposed to a transplant object or with athisprovided by an API likejQuery.ajax) is relatively small.