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

问题描述 投票:0回答:1

由于某种原因,我的MockComponent会抛出一个linting错误:

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);
}

有谁知道我怎么能解决这个错误?

angular karma-jasmine
1个回答
0
投票

我找到了原因:我只需要将Mock转换为以下行中的任何一个:

(Mock as any).prototype[method] = new EventEmitter<any>();
© www.soinside.com 2019 - 2024. All rights reserved.