Skip to content
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

In an anonymous class, accessing static class property in instance method via this.constructor is reported as an error (is not an error) [Javascript, but TypeScript checking it] #60449

Closed
gima opened this issue Nov 7, 2024 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@gima
Copy link

gima commented Nov 7, 2024

🔎 Search Terms

"class" AND "private" AND ("field" OR "property") AND ("must be declared in an enclosing class" OR "does not exist on type Function") AND "static" AND "this.constructor" AND "private" AND -label:Suggestion

Relevant bugs:

🕗 Version & Regression Information

  • The problem exists on the oldest (3.3.3333) and the newest (Nightly, v5.8.0-dev.20241107) of the Playground.
  • I reviewed the FAQ and couldn't find anything related to it.

⏯ Playground Link

https://www.typescriptlang.org/play/?filetype=js#code/MYewdgzgLgBA5gUygYQDYEMIRgXhgCgEpcA+AKBkpmAyxgHoAqGAQTHAE8BbEAV2xqZsjejADeFKlOjooAS2AwAxIKwAFAE5yAbrIS4YARgBMAZgDcZSVMoz5ixCloRNOvUXHWbUjUl4awGCgACzkIADoVZ1ddKARLb0oAXytE+CQ0IRi9AFUIOTA4AElIKHQwYAQAWSRgkAATDwk0nz8AoNCI0FKNXmAoEA1I1RctWPivKRSpq27oGGQAGRYAZRWDR0ysIksyOZBUBHDUEDh8GABycM3osfdiHBwLgBoF5bXrjNu3OI9CS32h2OpwIVxuWTucTyBWKpXKlRqIQaHkeLwIYAQAHc3qsVkRCJ8nBCfghoYUSjIKtVasjiP8yEA

💻 Code

const getClass = () =>
    class /* Anonymous class */ {
        static #classPrivate = 123;

        static getClassPrivate() {
            return this.#classPrivate;
        }

        getClassPrivateUsingInstanceMethod() {
            return this.constructor.#classPrivate;
        }
    }

const CLASS = getClass();

console.log( '.getClassPrivate() ==', CLASS.getClassPrivate() );
console.log ('.getClassPrivateUsingInstanceMethod() ==', (new CLASS()).getClassPrivateUsingInstanceMethod() );

Running the above code works. Logs:

[LOG]: ".getClassPrivate() ==",  123 
[LOG]: ".getClassPrivateUsingInstanceMethod() ==",  123 

🙁 Actual behavior

TypeScript checker says there are errors in the code.

Errors in code

    Property '#classPrivate' does not exist on type 'Function'.

🙂 Expected behavior

No errors from TypeScript checker.

Additional information about the issue

The problem goes away when adding // @ts-ignore above the erroring line.

Except

when using VSCode¹ locally, the ts-ignore directive does nothing. If I add // @ts-check at the top of the file, the ts-ignore directive works as expected.

¹ (built-in TypeScript version 5.6.3, Restricted Mode)

@jcalz
Copy link
Contributor

jcalz commented Nov 7, 2024

Isn't this a duplicate of #32452, which you linked above? How does this differ? I'd expect both class expressions and class declarations to have the same behavior with regards to the constructor property.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 8, 2024
@gima
Copy link
Author

gima commented Nov 8, 2024

Uuuh 😳
❝ Yes, but.

Mine is

  • about JavaScript (not TypeScript) code being checked automatically inside VSCode without even configuring anything. Just editing .js files.
  • about anonymous classes which Cannot be accessed via the literal class name instead, as suggested (as no such exists).
  • way clearer! 😫

And. The following Additional information about the issue might be yet another bug:

The problem goes away when adding // @ts-ignore above the erroring line.
Except when using VSCode¹ locally, the ts-ignore directive does nothing. If I add // @ts-check at the top of the file, the ts-ignore directive works as expected.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants