Skip to content

Object literal methods in prototype assignments don't have correct this-type #26831

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

Closed
sandersn opened this issue Aug 31, 2018 · 2 comments · Fixed by #26925
Closed

Object literal methods in prototype assignments don't have correct this-type #26831

sandersn opened this issue Aug 31, 2018 · 2 comments · Fixed by #26925
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@sandersn
Copy link
Member

sandersn commented Aug 31, 2018

// @noImplicitAny: true
/**
 * @constructor
 * @template {string} K
 * @template V
 */
// function Multimap() {
var Multimap = function() {
    /** @type {Object<string, V>} TODO: Remove the prototype from the fresh object */
    this._map = {};
};

Multimap.prototype = {
  /**
   * @param {K} key
   * @param {V} value
   */
  set: function(key, value) {
    var set = this._map.get(key);
    if (!set) {
      set = new Set();
      this._map.set(key, set);
    }
    set.add(value);
  }
}

Expected behavior:

No errors on this._map references.

Actual behavior:

Property '_map' doesn't exist on type { set(key: K): V }.

@sandersn sandersn added Bug A bug in TypeScript Salsa labels Aug 31, 2018
@sandersn sandersn added this to the TypeScript 3.1 milestone Aug 31, 2018
@sandersn sandersn self-assigned this Aug 31, 2018
@sandersn
Copy link
Member Author

sandersn commented Sep 4, 2018

This is a regression in 3.1. Doesn't repro on 3.0.

@sandersn
Copy link
Member Author

sandersn commented Sep 5, 2018

The reason 3.0 doesn't repro is that all objects have an index signature, even with noImplicitAny. In 3.1, noImplicitAny doesn't put an index signature on object literals.

Outside references to Multimap instances have the correct type because they create an intersection. checkThisExpression should do the same thing.

sandersn added a commit that referenced this issue Sep 5, 2018
Some cases were missing from tryGetThisTypeAt.
Fixes #26831
@sandersn sandersn added the Fixed A PR has been merged for this issue label Sep 5, 2018
sandersn added a commit that referenced this issue Sep 12, 2018
* Fix this-type in prototype-assigned object literals

Some cases were missing from tryGetThisTypeAt.
Fixes #26831

* Lookup this in JS only for  @constructor+prototype assignments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant