question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Polymorphic `this` and Generics

See original GitHub issue

First, 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:open
  • Created 8 years ago
  • Reactions:23
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
dead-claudiacommented, Apr 29, 2018

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 allow extends T and extends this in an F-bounded fashion - it’d make certain type-heavy modeling cases so much easier.

1reaction
saschanazcommented, Dec 23, 2015

I agree that this is related with #6220 and I think TS should be able to express ES6 Promise subclassing behavior.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found