-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inheritance does not work for Array<T> #11919
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
Comments
I believe the problem has to do with a change that was made to capture the values of |
Yes I also think so. But I can change the method to a function variable. Then the code works. What do you think, is this the right way to extend an Array?
|
I don't think there's a good way to extend Using a property instead of a method is fine if you don't expect your type to be subclassed and don't have extreme performance requirements. That's not the problem. The problem is that in ES5 the class will behave like a factory when called with A deeper problem is that since ES2015 classes are not callable at all your subclass won't have the intrinsic array function behavior of serving as a factory when not called with |
This is basically the same domain as #12013. In ES5 Arrays are not sub-classable and have some "magic" properties that cause issues/challenges. Traditionally, attempting to modify the behaviour of the array requires one of three approaches. 1) modify As @aluanhaddad noted and discussed in the other issue, there really isn't a clean way to do it that works both with ES2015+ and ES5/ES3. |
Thanks. I will remove the inheritance from the array and instead of that I will create a "normal" class with the array as a public property. |
TypeScript Version: [email protected]
Code
Expected behavior:
with [email protected] the output is correct:
Actual behavior:
with [email protected] the following exception is thrown:
The text was updated successfully, but these errors were encountered: