Angular 如何使用类构造函数为类实例定义接口

Angular 如何使用类构造函数为类实例定义接口,angular,typescript,prototype,Angular,Typescript,Prototype,我正在构建一个应用程序,部分代码允许开发人员指定要呈现特定部分的组件。我想让用户知道他们需要实现一个接口,但我不知道如何正确地编写输入 export interface ICustomComponent { templateObject: any; } export class MyComponent implements ICustomComponent { } export class MyLib { constructor( private yourCo

我正在构建一个应用程序,部分代码允许开发人员指定要呈现特定部分的组件。我想让用户知道他们需要实现一个接口,但我不知道如何正确地编写输入

export interface ICustomComponent {
    templateObject: any;
}

export class MyComponent implements ICustomComponent {
}

export class MyLib {
    constructor(
        private yourComponent: ICustomComponent
    ) {}
}

new MyLib(MyComponent); <== Fails
导出接口ICustomComponent{
templateObject:任意;
}
导出类MyComponent实现ICustomComponent{
}
导出类MyLib{
建造师(
私有yourComponent:ICustomComponent
) {}
}

新的MyLib(MyComponent) 由于
MyLib
需要类构造函数,而不是类实例,因此需要为类构造函数定义一个接口,并指定它使用
ICustomComponent
接口返回实例:

interface ICustomComponent {
  templateObject: any;
}

interface ICustomComponentConstructor {
  new (...deps: any[]): ICustomComponent;
}
然后你可以这样使用它:

export class MyComponent implements ICustomComponent {
  templateObject: any;
}

export class MyLib {
  constructor(private yourComponent: ICustomComponentConstructor) {
  }
}

new MyLib(MyComponent);

您可以阅读关于类构造函数和实例的接口。

您想在这里传递什么
newmylib(…)?实例,如
newmylib(newmycomponent())或类的引用?该示例仅用于说明问题,以便将
MyComponent
传递给componentFactoryResolver.resolveComponentFactory(MyComponent);`?是的,但是它像@Config({useComponent:MyComponent})一样被传入decorator