Skip to content

ES5 transpiled fat arrow async function looses the this #11031

Closed
@Ciantic

Description

@Ciantic

I'm converting babel project to TypeScript, one of the issues is how TS deals with this.

Edit I can also confirm that this works also with Chrome and target es2015. So the bug is in TypeScript's es5 transpiler thingie.

Edit Problem begins in here: https://gist.github.com/Ciantic/b3c7d68cd2fd77964492cdd8243fc870#file-asynctest-js-L50 it redefines _this as empty and since JS hoists the variable, it also passes empty variable right after!

This prints "hello world" with babel, but not with typescript transpiler, the this is not defined.

Cloneable gist contains also the javascript produced by TS.

class MyComponent {
    public text = "hello world";
    say = () => {
        console.log(this.text);
    }
    onChange = async () => {
        return new Promise(() => {
            this.say();
        });
    }
    render() {
        return { onChange : this.onChange }
    }
}
let a = new MyComponent();
a.render().onChange();

tsconfig.json

{
    "compilerOptions": {
        "moduleResolution": "node",
        "module": "commonjs",
        "target": "es5",
        "lib": [
            "es2015",
            "es6",
            "dom"
        ]
    }
}

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: TransformsRelates to the public transform APIFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions