-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Array inheritance in ES6 declaration file (lib.es6.d.ts) #10886
Comments
This is going to be difficult, because TypeScript does not currently allow re-slotting of generics on polymorphic The problem becomes if in a subclass you introduce a different generic slot, how does polymorphic There are more examples in the issue mentioned above as well of where polymorphic this breaks down when combined with generics. |
The issue with 'map' is indeed the same as the one discussed in "Polymorphic 'this' and Generics", but all the other problematic Array methods could work correctly by simply returning 'this'. And the Array.from and Array.of methods produce a separate problem, that is also impossible to represent with current TypeScript syntax as far as I know, because the ArrayConstructor interface would have to be able to refer to the 'this' of the Array interface. |
the static functions can be solved by making for the rest, adding |
ES6 supports Array inheritance, but this is not reflected in the TypeScript declaration file (lib.es6.d.ts).
Array functions like 'filter' and 'slice' return an array of the subclass, so the following should compile in TypeScript:
The functions 'reverse', 'concat', 'slice', 'splice', 'filter' should return with the type 'this'.
The 'map' function returns an instance of the subtype too, but it I don't know how that could be represented in TypeScript, as it would require a generic parameter in the return type, something like:
map<U>(callback: (value: T, index: number, array: this) => U): this<U>
Another issue is the static constructors of the Array class, this should compile too:
There are also some additional complications added by the @@species Symbol, which can be used to change the type signature of the subclass to not use 'this' as the return value for functions like 'filter' and 'slice'. So for the following example, the subclass would work how the return types are defined in the declaration file currently:
Chrome and Node.js both already implement the array inheritance features shown here.
The text was updated successfully, but these errors were encountered: