我正在尝试扩展我的接口(interface)之一,但不断出现错误“{dcs:Relationships}类型中缺少索引签名,并带有以下代码段:

interface Relationship {
    data: {
        id: string;
        type: string;
    }
}

interface T {
    relationships: {[key: string]: Relationship};
}

interface Test extends T {
    relationships: {
        dcs: Relationship;
    };
}

目标是使T中的Relationships属性成为具有所有Relationship类型的任意数量的键的对象。测试应该是类型T的特定实现。

不确定解决此问题的正确方法是什么。提前致谢!

最佳答案

像这样声明Test:

interface Test extends T {
  relationships: {
    [key: string]: Relationship;
    dcs: Relationship;
  };
}

但是,正如您已经注意到的那样,当您实际实现接口(interface)时,最终不得不再次声明索引器。您可以通过执行以下操作来保存一些输入内容:
interface T {
  relationships: {
    [key: string]: Relationship;
  };
}

interface TestRelationships {
   [key: string]: Relationship;
   dcs: Relationship;
}

interface Test extends T {
  relationships: TestRelationships;
}

class T1 implements Test {
  relationships: TestRelationships;
}

10-06 00:10