Typescript 实现类似字典的类

Typescript 实现类似字典的类,typescript,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 'Dict

难道这不可能吗

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'.

因为你实现了一个接口

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