Skip to content

extends dynamic Base class with generic throw an error #8853

@cevek

Description

@cevek

Throw error: Base constructor return type 'T' is not a class or interface type

interface BaseClass<T> {
    new (): T
}
function generateUserClass<T>(baseClass: BaseClass<T>){
    return class extends baseClass {
        name: string;
    }
}

Error: Type any is not constructor function type

function generateUserClass<T>(baseClass: any){
    return class extends baseClass {
        name: string;
    }
}

But if we specify generic it's work

function generateUserClass(baseClass: BaseClass<Foo>){
    return class extends baseClass {
        name: string;
    }
}

Throw error: Base constructor return type 'IUser & Base' is not a class or interface type

interface IUser {
    name: string;
}
interface IUserClass<T>{
    new (): IUser & T;
}
function genUserCls<T>(base: new ()=>T): IUserClass<T> {
    return null;
}
class Base{
    foo(){}
}
class User extends genUserCls(Base) {}

TS version: 1.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions