实现类字典类

时间:2017-02-03 07:20:15

标签: typescript

这不可能吗?

interface IDictionary {
    [index: string]: string;
}

class Dictionary implements IDictionary {
    public foo = "bar";
}

相反,我得到了错误:

TS2420:Class 'Dictionary' incorrectly implements 'IDictionary'. Index signature is missing in type 'Dictionary'.

1 个答案:

答案 0 :(得分:0)

因为你实现了一个接口

interface IDictionary {
    [index: string]: string;
}

class Dictionary implements IDictionary {
    public foo = "bar";
}

您需要提供所有要求

interface IDictionary {
    [index: string]: string;
}

class Dictionary implements IDictionary {
    public foo = "bar";
    [index: string]: string;
}