Skip to content

code gen depends on types #20125

Closed
Closed
@dgoldstein0

Description

@dgoldstein0

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions