Skip to content

Error "TS2335" false positive #60669

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
AFatNiBBa opened this issue Dec 3, 2024 · 5 comments
Closed

Error "TS2335" false positive #60669

AFatNiBBa opened this issue Dec 3, 2024 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@AFatNiBBa
Copy link

🔎 Search Terms

  • ts2335
  • static
  • hasInstance
  • 'super' can only be referenced in a derived class

🕗 Version & Regression Information

  • This has NOT changed
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "TS2335"
  • I tested this on "Nightly" and "3.3.3" on the playground

⏯ Playground Link

https://www.typescriptlang.org/play/?target=99&ts=5.8.0-dev.20241203#code/FAYwNghgzlAEByB7ATgWwmAwpGsDewsRsALogMonICWAdgOYAUAlPocR8gKYkCuytWAEZYAalhReABy7IAdGUo0GLANzsiAX1gB6HRwNFAiYSwA5JJnJTsEBEGJaYAJ6wARl1jcAZrK60QXAAmsHSwELCBstQAbkE2OFBywJrAoAmw5DIg1BjY0HAEAJBQJBAk1CCwANrkTqiuiGByABbQAJK0JXYBALqMiK4AVgBcsLy0ANa0iADutKxFhdx8ArADg7AAvNukzdSJUsiIZCROMrAAPhcS0rI1dQ1NrVAdXf5cfevM6oXaeoYAwFA4gmcy3Kw2OxrRwudyeLg+bjvYKhcKRGixYLgfJJFKgBxQRpcORgRBMAgcdajPDaOhvAKILwZLI5LAJAA0wK5AP+XgwUC4GhCnVGtC4M2ZXGyuQSLGF9K4jMl0rZ+U5XP+VF4go4h2OiFGmSlrLyMDkepOZw8dNK7yVRpVpqg6o4muQ2qFFsQAH1pmhRkg0DKcV7Tucbd1FUzA+hVTAXbz+TriCVkKLxQgULGnXLxAAiPPcouA-55oRVdZSkiwADyQyrPTzyW+QA

💻 Code

class NormalClass {
  toString() {
    return 1 + super.toString();
  } //         ↑ 'super' can only be referenced in a derived class.
}

class SpecialClass {
  static [Symbol.hasInstance](obj: unknown) {
    return obj === this.prototype || super[Symbol.hasInstance](obj);
  } //                               ↑ 'super' can only be referenced in a derived class.
}

console.log({
  obj: {} instanceof SpecialClass,                            // false
  ins: new SpecialClass() instanceof SpecialClass,            // true
  proto: SpecialClass.prototype instanceof SpecialClass,      // true
  proto_norm: NormalClass.prototype instanceof NormalClass,   // false
  str: new NormalClass() + ""                                 // "1[object Object]"
});

🙁 Actual behavior

The error "TS2335" is shown for the two marked lines of code

🙂 Expected behavior

The code works as a charm when executed, so I think the error shouldn't come up in those cases, since is probably a false positive

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

MartinJohns commented Dec 3, 2024

If this is intentional you can write extends Object. Otherwise the inheritance chain with Object is hidden.

Duplicate of #49224.

@AFatNiBBa
Copy link
Author

AFatNiBBa commented Dec 3, 2024

I don't think it's fully a duplicate because it's not only a problem with instance members (Object.prototype.toString()), but also static ones (Function.prototype[Symbol.hasInstance]()).

Moreover, extends Object has real runtime effects, for instance it adds the static members of Object to the class.

The prototype of base classes is Function.prototype, if you explicitly extended Object, it would be the prototype instead

console.log((class { }).__proto__ === Function.prototype)     // true
console.log((class extends Object { }).__proto__ === Object)  // true
console.log((class { }).create)                               // undefined
console.log((class extends Object { }).create)                // function create() { [native code] }

@RyanCavanaugh
Copy link
Member

It's still intentional to not allow any super access without an explicit extends clause. You can write class Foo extends Function if that's important in your code.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 3, 2024
@AFatNiBBa
Copy link
Author

AFatNiBBa commented Dec 5, 2024

As I said, it's completely different:

console.log((class { }).__proto__ === Function.prototype)     // true
console.log((class { }).__proto__ === Function)               // false

Notice the prototype, the thing you said would make the INSTANCE of the class extend function.
That error makes never sense in these situations, since the base members (On Object.prototype for instance members and on Function.prototype for static ones) are always accessible

@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 Dec 8, 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