Skip to content

typeof this stops working after for..in or for..of: "Cannot find name 'this'." #46072

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
Yonom opened this issue Sep 26, 2021 · 6 comments · Fixed by #46181
Closed

typeof this stops working after for..in or for..of: "Cannot find name 'this'." #46072

Yonom opened this issue Sep 26, 2021 · 6 comments · Fixed by #46181
Labels
Bug A bug in TypeScript Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this
Milestone

Comments

@Yonom
Copy link

Yonom commented Sep 26, 2021

Bug Report

After a for..in or for..of statement, I can no longer access typeof this in class methods. A compile time error appears saying "Cannot find name 'this'."

🔎 Search Terms

Cannot find name 'this'. typeof this class for in for of

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about typeof, classes, typeof this

⏯ Playground Link

Playground link with relevant code

💻 Code

class Tests {
    test1() { // OK
        type Test = typeof this;
    }

    test2() { // OK
        for (;;) {}
        type Test = typeof this;
    }

    test3() { // NOK: expected no compile errors
        for (const dummy in []) {}
        type Test = typeof this; // Error 2304: Cannot find name 'this'.
    }

    test4() { // NOK: expected no compile errors
        for (const dummy of []) {}
        type Test = typeof this;  // Error 2304: Cannot find name 'this'.
    }
}

🙁 Actual behavior

The test cases test3 and test4 show compile errors.

🙂 Expected behavior

All test cases should pass without error.

@andrewbranch
Copy link
Member

Congratulations, this is officially the weirdest bug I’ve seen today

image

Uh, also, all of these were errors until 4.4?!

@andrewbranch andrewbranch added Bug A bug in TypeScript Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this labels Sep 30, 2021
@andrewbranch andrewbranch added this to the Backlog milestone Sep 30, 2021
@fatcerberus
Copy link

In the meantime, for the record: typeof this and this are the same type.

@paranoiacblack
Copy link
Contributor

Something interesting to note here is that it compiles fine if there are any statements within the for..in or for..of loop.

@Zzzen
Copy link
Contributor

Zzzen commented Oct 3, 2021

test2 is an infinite loop, so anything could happen.

@Zzzen
Copy link
Contributor

Zzzen commented Oct 3, 2021

In the meantime, for the record: typeof this and this are the same type.

Actually, they are slightly different: this always refers to the current scope of it while typeof this will use its flow type.

class Base {
    f() {
        if (this instanceof D) {
            this.g();
            (this as this).g();  // Error: Property 'g' does not exist on type 'Base'.(2339)
            (this as typeof this).g();
        }
    }
}

class D {
    g() {}
}

@ahejlsberg
Copy link
Member

Looks to be an effect of #43898.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants