2
Vote

Error while creating Instance for Generic

description

consider the following class
module DesignPtn{
    export interface IBaseBO {
        GetById(id: number): IBaseBO
    }

    export class UserBO implements IBaseBO {
        Id: number;
        constructor(id: number) {
            this.Id = id;
        }
        GetById(id: number) {
            return new UserBO(id);
        }
    }

    export class AddressBO implements IBaseBO {
        UserId: number;
        constructor(userId: number) {
            this.UserId = userId;
        }
        GetById(id: number) {
            return new AddressBO(id);
        }
    }

    interface Instance<T> {
        new (): T;
    }

    export class Factory {
        static GetBO<T extends IBaseBO>(instance: Instance<T>): T {
            return new instance();
        }
    }

    export class Applicatioin {
        constructor() { }
        GetUserById(id: number) {
            var userBo: UserBO = Factory.GetBO(UserBO);
            //var myUserBo: AddressBO = Factory.GetBO(AddressBO);
        }
    }
}
I am getting the following compile error.

Argument of type 'typeof UserBO' is not assignable to parameter of type 'Instance<UserBO>'.

Help me to resolve this issue.

comments