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

Most upvoted comments

On the other hand, we’ve gotten the request to type this inside 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) or defineClass(POJO) which wants a different behavior?

In my JavaScript experience, the set of cases where this inside an object literal is actually referring to the object itself (as opposed to a transplant object or with a this provided by an API like jQuery.ajax) is relatively small.