Skip to content

Polymorphic this and Generics #6223

@kitsonk

Description

@kitsonk

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions