-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Cannot subclass class-like types with accessors. #40733
Comments
This could also potentially be a feature request, ideally I'd like to be able to somehow declare getters/setters in a mapped type but I haven't been able to figure out a way to do it. For now, I'm trying to type a JavaScript function which defines "properties" on a class (in JS it uses defineProperty with getters/setters) and then have other JavaScript classes subclass these types. In previous versions, this worked but with the universal change to error in this case it doesn't. I think it makes sense to error, but there is now no way (as far as I can tell) to use getters/setters with mapped types. |
One almost-workaround is to use interface merging: type BaseInterface = typeof Base;
interface MyClass extends BaseInterface { }
class MyClass {
} But you can't call super anymore, and |
Hi @sandersn! Unfortunately I'm trying to type a JS function which is used both in checkJS code and TypeScript code. Also, in my more complicated version Specifically, we're working on bringing type checking to GNOME Shell (the desktop environment for Ubuntu, Fedora, etc.) and I'm writing a type definition for This is the actual work which is appended to this file. Ideally there would be someway to describe getters/setters in the type syntax (I assume internally there must be some sort of flag for how a property is represented)... But for now I really wish there was a way to disable this new behavior when classes extend from newable-types (and not class definitions). |
This is (in some ways) the opposite problem of #40220. |
Looking again at this, there isn't a way, as far as I can tell, for object types to contain accessors. We should probably allow this on grounds that (1) accessors are not allowed, so the compiler can't know whether an accessor was intended (2) inheriting from object types is rare, happening mostly with mixins where we have real trouble representing the truth already. Edit: I missed that this was coming from an object literal, not an object literal type. These do allow accessors, but the flag doesn't survive through the mapped type. It probably should. However, this wouldn't solve the original problem, because the properties (like |
TypeScript Version: 4.1.0-beta
Search Terms: accessor property subclass mapped types
Code
Expected behavior:
I can create subclasses of mapped types when the mapped type defines a property and the subclass defines an accessor.
Actual behavior:
TypeScript gives an error saying you can't override a property with an accessor.
Playground Link: Link
Related Issues: I know this is related to the change in behavior in 4.0 to universally disallow accessors overriding properties.
The text was updated successfully, but these errors were encountered: