简体   繁体   中英

Why TypeScript compiler ignores 'noImplicitThis' option in my example?

Consider the code:

class x {
    public variable: number = 0;
    public x() {
        const c = [];
        c.push({
            xxx () {
                console.log(this.variable);
            }
        });
    }
}

The anonymous function xxx doesn't have access to 'this' of class 'x'. Normally this can only compile if 'this' implicitly has type any. But it does compile .

If I extract my literal object in a separate value, I actually do get a compilation error that I expect.

Is it a compiler bug?

Note: you need to manually enable 'noImplicitThis' in typescript playground because apparently options are not sharable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM