模拟组件:类型模拟没有索引签名

时间:2018-08-16 14:17:34

标签: angular karma-jasmine

由于某种原因,我的MockComponent抛出了掉毛错误:

ERROR in src/app/helpers/mock-component.ts(17,13): error TS7017: Element implicitly has an 'any' type because type 'Mock' has no index signature.

代码如下:

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

export function MockComponent(options: Component): Component {

    const metadata: Component = {
        selector: options.selector,
        template: options.template || '',
        inputs: options.inputs,
        outputs: options.outputs || [],
        exportAs: options.exportAs || ''
    };

    class Mock { }

    if (metadata.outputs) {
        metadata.outputs.forEach((method: any) => {
            Mock.prototype[method] = new EventEmitter<any>();
        });
    }

    return Component(metadata)(Mock as any);
}

有人知道我该如何解决这个错误?

1 个答案:

答案 0 :(得分:0)

我发现了原因:我只需要在以下行中将Mock转换为任意一个

(Mock as any).prototype[method] = new EventEmitter<any>();