Skip to content

code gen depends on types #20125

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
dgoldstein0 opened this issue Nov 18, 2017 · 0 comments
Closed

code gen depends on types #20125

dgoldstein0 opened this issue Nov 18, 2017 · 0 comments
Assignees
Labels
Bug A bug in TypeScript

Comments

@dgoldstein0
Copy link

TypeScript Version: 2.6.1

Code

class Foo {
    bar(): void {
        console.log(this.x);
    }
}

class Bar extends Foo {
    x: Number;

    constructor() {
        super();
        this.x = 2;
    }

    bar() {
        super.bar();
        (super.bar as any)();
    }
} 

let b = new Bar();
b.bar()

Expected behavior:

I expect that the code generated for super.bar() and (super.bar as any)() behave the same - this code should console.log(2) twice.

Actual behavior:

The code logs 2, and then undefined.

The underlying issue is that super.bar() and (super.bar as any)() compile to different code. the former compiles to _super.prototype.bar.call(this);, but the as any changes the compilation to _super.prototype.bar();, and so super.bar is called with the wrong this when the cast is in place.

I was generally under the impression that code generation should be the same whether or not casts were present in the code, so I found this a bit surprising. that said, it was easy to change my code to avoid this pitfall.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants