Skip to content
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

Static variables class variables of another class in the same module cannot be instantiated before the class has been declared #13121

Closed
Saulzi opened this issue Dec 22, 2016 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@Saulzi
Copy link
Contributor

Saulzi commented Dec 22, 2016

TypeScript Version: 2.1.4
Static variables class variables of another class in the same module cannot be instantiated before the class has been declared

for example

class Issue {
    static readonly X = new DoesntWork();
}

class DoesntWork {
    
}

this is because of where X is being set in the generated javascript

var Issue = (function () {
    function Issue() {
    }
    return Issue;
}());
Issue.X = new DoesntWork();
var DoesntWork = (function () {
    function DoesntWork() {
    }
    return DoesntWork;
}());

Issue.X should be set after DoesntWork is defined rather than before

If I swap the order of definition, it works, i.e.

class DoesntWork {

}
class Issue {
    static readonly X = new DoesntWork();
}

Generated JS

var DoesntWork = (function () {
    function DoesntWork() {
    }
    return DoesntWork;
}());
var Issue = (function () {
    function Issue() {
    }
    return Issue;
}());
Issue.X = new DoesntWork();

Swapping around kind of works but if you want class DoesntWork to inherit Issue Issue needs to be declared first

Expected behavior:
No error and able to reference Issue.X
Issue.X = new DoesntWork needs to be dumped at either the end of the js or after where DoesntWork is declared.

Actual behavior:
Error is raised,
Uncaught TypeError: DoesntWork is not a constructor (chrome)
Object doesn't support this action (edge)
etc etc

@mhegazy
Copy link
Contributor

mhegazy commented Dec 22, 2016

seems like a duplicate of #12673

@mhegazy mhegazy added the Duplicate An existing issue was already created label Dec 22, 2016
@Saulzi
Copy link
Contributor Author

Saulzi commented Jan 3, 2017

This is not a duplicate as #12673 as that is suggesting blocking using b before it is defined. What I am essentually trying to acheive is have a 'static readonly 'null instance'' the suggestion above will not allow exporting a hidden class which inherits from Issue and impements a 'null instance' of issue if that makes sense??

export class Issue {
    static readonly X = new DoesntWork();
    SomeMethod() { // Do something }
}

class DoesntWork extends Issue {
    SomeMethod() { // Do Nothing }
}

@mhegazy
Copy link
Contributor

mhegazy commented Jan 3, 2017

The compiler does not reorder code. #12673 tracks giving a warning at compile time that this is going to fail at runtime.

@Saulzi
Copy link
Contributor Author

Saulzi commented Jan 4, 2017

had worked around this with static get and backing field set after definition which is not exported. would be nice to have this with less code :P

@Saulzi Saulzi closed this as completed Jan 4, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants