如何定义此打字稿界面类型

时间:2020-07-19 17:05:51

标签: typescript

Hello StackOverflow社区,

如果可能的话,我想问一下如何在Typescript中正确定义此类型。 我一直在想是否可以通过使用泛型来实现。 假设对象的键始终是不同的,并且不遵循规范化的模型。

{ status_id: { original: "A", new: "B" } },

其中status_id可以是任何字段。

export interface IParsedLog {
  changes?: any; // How to properly define this Type in Typescript?
  date: Date;
}

const mock:IParsedLog[] = [
  {
    changes: [
         { status_id: { original: "A", new: "B" } },
         { any_field_id: { original: "A", new: "B" } },
    ],
    date: '10-06-2020',
  },
  {
    changes: [
         { supervisor_id: { original: "Jhon", new: "Jhony" } },
    ],
    date: '10-06-2020',
  }];

1 个答案:

答案 0 :(得分:0)

export interface IChange {
  [key: string]: ({ original: string, new:string});
}

export interface IParsedLog {
  changes: IChange[], 
  date: Date;
}

const mock:IParsedLog[] = [
  {
    changes: [
         { status_id: { original: "A", new: "B" } },
         { any_field_id: { original: "A", new: "B" } },
    ],
    date: '10-06-2020',
  },
  {
    changes: [
         { supervisor_id: { original: "Jhon", new: "Jhony" } },
    ],
    date: '10-06-2020',
  }];