实现类后,组件不可分配

时间:2016-06-20 21:14:26

标签: typescript angular

有人能告诉我为什么TypeScript会说'typeof MyComponent' is not assignable to type 'Renderer'. Property 'input' is missing in type 'typeof MyComponent' 当MyComponent实现Rendered?

import { Component, Input } from '@angular/core';

interface Renderer {
    input: string;
}

@Component({
    selector: 'some-other-selector',
    template: 'hello-mum'
})
class MyComponent implements Renderer {
    @Input() input: string;
}


@Component({
    selector: 'dynamic-list-app',
    template: 'hello-world'
})
export class DynamicListAppComponent {
    renderer: Renderer;
    constructor() {
        this.renderer = MyComponent;
    }
}

1 个答案:

答案 0 :(得分:2)

renderer字段需要Renderer类型的实例,但您要分配类MyComponenttypeof MyComponent类似于java Class<MyComponent>,它显然不是Renderer的实例。

应为this.renderer = new MyComponent();