Skip to content

Opt-in flag for strictThisChecks #16615

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
EliSnow opened this issue Jun 19, 2017 · 1 comment
Closed

Opt-in flag for strictThisChecks #16615

EliSnow opened this issue Jun 19, 2017 · 1 comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@EliSnow
Copy link

EliSnow commented Jun 19, 2017

TypeScript Version: 2.3.4

Code
This is a repro of an actual bug that bit me which I was surprised the type checker did not catch.

class Foo {
    constructor(private name: string) { }
    public bar(s: string) {
        return this.name + s;
    }
}

const foo = new Foo("f");
["a", "b"].map(foo.bar);

Expected behavior:
The type checker should catch that bar will be called with a this of undefined (assuming strict mode). Methods should have an implicit this of the containing class (in this case this: Foo), or at least an interface with the properties accessed within the method (this: { name: string }).

Actual behavior:
No compile error is emitted and a runtime error is the result.

In #14936, @RyanCavanaugh mentions there being a performance cost to adding in implicit this types. At least in my case, since I was bit by this, I would be willing to pay the extra cost during compile to catch these kind of errors. Perhaps a compiler option would be worthwhile.

@DanielRosenwasser DanielRosenwasser added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Jun 19, 2017
@DanielRosenwasser DanielRosenwasser changed the title Type check error on this for methods Opt-in flag for strictThisChecks Jun 19, 2017
@RyanCavanaugh
Copy link
Member

Closing due to lack of additional feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants