Skip to content

Can't extend mixin constructor that constructs a generic type #13807

Open
@DanielRosenwasser

Description

@DanielRosenwasser

This applies regardless of whether or not I intersect it with some bogus type, or have a reasonable constraint. So none of the following works.

Returning a plain T

export type Constructor<T> = new (...args: any[]) => T

export interface Timestamp {
    timestamp: Date;
}

// plain old T
export function Timestamp<T, CT extends Constructor<T>>(Base: CT): Constructor<Timestamp> & Constructor<T> {
    return class extends Base {
        timestamp = new Date();
    }
}

Constructor of T with a bogus object type ({})

export type Constructor<T> = new (...args: any[]) => T

export interface Timestamp {
    timestamp: Date;
}

// Constructor of T with a bogus object type
export function Timestamp<T, CT extends Constructor<T & {}>>(Base: CT): Constructor<Timestamp> & Constructor<T> {
    return class extends Base {
        timestamp = new Date();
    }
}

Constructor of T where T is constrained to {}

export type Constructor<T> = new (...args: any[]) => T

export interface Timestamp {
    timestamp: Date;
}

export function Timestamp<T extends {}, CT extends Constructor<T>>(Base: CT): Constructor<Timestamp> & Constructor<T> {
    return class extends Base {
        timestamp = new Date();
    }
}

Constructor of T where T is constrained to object (see also #13805)

export type Constructor<T> = new (...args: any[]) => T

export interface Timestamp {
    timestamp: Date;
}

export function Timestamp<T extends object, CT extends Constructor<T>>(Base: CT): Constructor<Timestamp> & Constructor<T> {
    return class extends Base {
        timestamp = new Date();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions