microsoft / microsoft/TypeScript

Function expressions in a property assignment of a prototype object should be methods, not nested classes

Open
#37,720 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Binder
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

/** @class */
module.exports.C = function() {
  this.x = 1
}
module.exports.C.prototype = {
  m1() {
    this.a = 1
  },
  m2: function() {
    this.b = 2
  }
}
var c = new module.exports.C()
c.a
c.b

Expected behavior:
c.a and c.b both work.

Actual behavior:
Only c.a works; c.b says that 'd' does not exist on type 'C'. There's also an error on this.b = 2 when noImplicitThis: true.

bindThisPropertyAssignment needs to understand that function expressions might be part of a property assignment in an object literal. The code to handle this will probably look like this:

                    // For `{ x: function() }` should modify the object literal's members, not behave like a fresh class (as long as it doesn't have @class on it)
                    if (isPropertyAssignment(thisContainer.parent)) {
                        // wow I hope those parent pointers are set!
                        const containingClass = thisContainer.parent.parent;
                        const symbolTable = containingClass.symbol.members!;
                        if (hasDynamicName(node)) {
                            bindDynamicallyNamedThisPropertyAssignment(node, containingClass.symbol);
                        }
                        else {
                            declareSymbol(symbolTable, containingClass.symbol, node, SymbolFlags.Property | SymbolFlags.Assignment, SymbolFlags.None, /*isReplaceableByMethod*/ true);
                        }
                        break;
                    }

I expect this to break a lot of tests, but it didn't. However, I haven't tried it on the user tests yet.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction in the issue and inspect bindThisPropertyAssignment, especially how function expressions inside prototype object property assignments are handled. Verify the behavior with the shown example under noImplicitThis, including whether both c.a and c.b work and the reported diagnostic is gone.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.