Skip to content

Allow this typing and type parameters on static methods #7627

Closed
@tinganho

Description

@tinganho

Since constructor visibility #2341 got merged. I haven't been able to utilise it fully because the classes I can benefit from requires type parameters and this typings on static methods.

abstract class Model<P> {
    static create(props: P): this {
        let model = window.store[this.name + props.id];
        if (model) {
            return model;
        }
        return window.store[this.name + props.id] = new this(props)
    }

    protected constructor(props: P) {
    }
}

The Model class is a base class. A this typing would be very good to refer to the subclass instead of the base class. Since many classes has type arguments in their constructors, it makes sense to allow type arguments in static factory methods as well, otherwise you cannot create type safe factory methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions