Skip to content

Fix circularity error when extending a class in same JSContainer#24710

Merged
sandersn merged 1 commit into
masterfrom
js/fix-circularity-error-when-extending-class
Jun 6, 2018
Merged

Fix circularity error when extending a class in same JSContainer#24710
sandersn merged 1 commit into
masterfrom
js/fix-circularity-error-when-extending-class

Conversation

@sandersn
Copy link
Copy Markdown
Member

@sandersn sandersn commented Jun 5, 2018

Do this by not widening properties of an object literal that are

  1. JS initialisers
  2. and not an object literal

These properties have types that will never widen, so the compiler shouldn't ask for the types earlier than it strictly needs to. This avoids the bogus circularity error.

Fixes #24703

This reverses the chrome-devtools-frontend errors that #24707 introduces with its improved valueDeclaration location. Together, they provide a substantial improvement to the number of errors we get when compiling that project.

Do this by not widening properties of an object literal that are

1. JS initialisers
2. and not an object literal

These properties have types that will never widen, so the compiler
shouldn't ask for the types earlier than it strictly needs to.
@sandersn sandersn requested review from mhegazy and weswigham June 5, 2018 23:20
@sandersn
Copy link
Copy Markdown
Member Author

sandersn commented Jun 6, 2018

Hmmm. I'm going ahead with the merge of this, even though it doesn't completely fix the problem. Simple cases work fine (like the test I added), but most of the improvement I saw in chrome-devtools-frontend happens because we resolve properties from the base class as having type any. I'll investigate and file separately since this fix works for most cases.

@sandersn sandersn merged commit d6250c8 into master Jun 6, 2018
@mhegazy mhegazy deleted the js/fix-circularity-error-when-extending-class branch June 6, 2018 17:25
@sandersn
Copy link
Copy Markdown
Member Author

sandersn commented Jun 6, 2018

Here is the reduced repro for the part of the problem that's not fixed:

var UI = {}
UI.TreeElement = class {
    constructor() {
        this.treeOutline = 12
    }
};
UI.context = new UI.TreeElement();

class C extends UI.TreeElement {
    m() {
        // should error on these two lines, but doesn't
        this.doesNotExist
        this.treeOutline.doesntExistEither()
    }
};

This gives a noImplicitAny error for UI and UI.context (since they reference themselves, they need a type annotation), but no other errors. The error correctly points out that you can annotate UI or UI.context to avoid the problem; the simplest way is to put /** @type {*} */ on top of UI.context.

The reason for the error is that the fix in this PR still checks the type of any initializer that is not a JSContainer, such as UI.n = 1, in case that type is null or undefined or contains them. When the initializer references UI, as new UI.TreeElement() does, then the circularity is hit again and UI's type just becomes any. So C effectively extends from any.

@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants