Polymorphic `this` and Generics
See original GitHub issueFirst, thank you team for polymorphic this
. It is really handy!
I think I ran into a case though that I am finding challenging, when I need to return a this
, but the generics might have changed. For example:
class A<T> {
private items: T[] = [];
map<U>(callback: (item: T, idx: number, a: this) => U): this {
// boring implementation details
});
}
Where I want to be able to change the generic type for the class with a function, but I will be contracting to return the “current” class, but I want to guard different generics. The following seems logical to me, but doesn’t appear to be currently supported:
class A<T> {
private items: T[] = [];
map<U>(callback: (item: T, idx: number, a: this) => U): this<U> {
// boring implementation details
});
}
Where if no generics arguments are supplied, it is inferred to be the current ones, where as if they are supplied they are substituted.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:23
- Comments:20 (8 by maintainers)
Top Results From Across the Web
Generics and polymorphism - Stack Overflow
Polymorphism is a property of classes, in that they implement a common interface, or are derived from a base class, implementing virtual methods...
Read more >Generics and Polymorphism - Whizlabs Blog
The polymorphism applies only to the 'base' type (type of the collection class) and NOT to the generics type. Generic Methods. To understand...
Read more >Polymorphism (generics) - Washington
Using generics. (supplying type arguments). List<AType> name = new ArrayList<AType>();. • The type that is passed (AType) is called the type parameter.
Read more >Polymorphism in Java Generics - Java Code Geeks - 2022
Generics are there to make the Lists TYPE SAFE and technically meaningful. In order to accept polymorphic sub/super classes we can enhance the ......
Read more >What's the difference between using polymorphism ... - Quora
Generics, or Parametric Polymorphism, refers to the mechanism that allows a method or a class to take on various forms, at compile-time, depending...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yeah, they fixed the inconsistency. The workaround is to use type intersection, but IMHO it’s not a great workaround given it prevents me from assuming
this extends T
(which is actually useful on occasion, believe it or not), and it’s forced me to recast types and parts of my data model at the type level more than once, in ways that were just boilerplatey hacks. I just wish they would allowextends T
andextends this
in an F-bounded fashion - it’d make certain type-heavy modeling cases so much easier.I agree that this is related with #6220 and I think TS should be able to express ES6 Promise subclassing behavior.