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

Private Members with Default Value on Class Extension Compiles with Member declarations before Super call if function is called before super #29453

Open
jamelcole-ah opened this issue Jan 17, 2019 · 1 comment
Labels
Bug A bug in TypeScript
Milestone

Comments

@jamelcole-ah
Copy link

TypeScript Version: 3.3.0-dev.201xxxxx

Search Terms:

  • "Private member compile"
  • "Private Properties before super"
  • "compile class extension"
  • "constructor order at compile time"
  • "functions before super call"
    Code
function example ( number: number ) {
    return number;
} 
class ExtString extends String {
    private x: number;
    private y: number = 10;
    z: number;
    constructor(...args){
        example(args[0]);
        super(...args);
        this.z = 19;
    }
}

Expected behavior:
I would expect that when the above code compiles, that the private members get initialized with values after the super call like below:

function example(number) {
    return number;
}
class ExtString extends String {
    constructor(...args) {
        example(args[0]);
        super(...args);
        this.y = 10;
        this.z = 19;
    }
}

Actual behavior:
the private members which have values gets assigned to before the super call. This behaviour only happens when I call a function before a super call, even if said function does not alter this in the code (like below).

function example(number) {
    return number;
}
class ExtString extends String {
    constructor(...args) {
        this.y = 10;
        example(args[0]);
        super(...args);
        this.z = 19;
    }
}

This results in a compile time error.

Related Issues:
I found a similar bug that is referenced here #8277

@weswigham weswigham added Bug A bug in TypeScript Needs Investigation This issue needs a team member to investigate its status. labels Jan 17, 2019
@jamelcole-ah
Copy link
Author

Given this issue has yet to be resolved, I wanted to share a current work around with anyone who may be experiencing this as well:

When creating private memebers, if you don't provide them with default values in the head of your class, but instead initialize them in the constructor explicitely, it will compile correctly.

i.e.

function example ( number: number ) {
    return number;
} 
class ExtString extends String {
    private x: number;
    private y: number;
    z: number;
    constructor(...args){
        example(args[0]);
        super(...args);
        this.z = 19;
        this.y = 10;
        this.x = 20;
    }
}

@RyanCavanaugh RyanCavanaugh removed the Needs Investigation This issue needs a team member to investigate its status. label Mar 7, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants