简体   繁体   English

打字稿:在界面中格式化嵌套类型

[英]Typescript: formatting nested type in interface

I have interface CountersData that I would like to model off of my JSON data.我有interface CountersData我想根据我的 JSON 数据建模。 As seen below, I have a nested id (in example below 1830 and 1835 are ids ) property that is a array of a nested dictionaries, which I also want to be optional .如下所示,我有一个嵌套的id (在下面的示例中18301835ids )属性,它是一个嵌套字典的数组,我也希望它是可选的。

I have been unable to make it optional, so I am unable to fetch the proper data.我无法将其设为可选,因此无法获取正确的数据。 How should this data be formatted?这些数据应该如何格式化? See below my code and the JSON format.请参阅下面我的代码和 JSON 格式。

interface CountersData {
  //other properties

  //want this to be optional
  [id: number]: {
    count_targets: number;
    count_targets_excluded: number;
    count_targets_pending: number;
    count_targets_in_progress: number;
    count_targets_completed: number;
    count_targets_failed: number;
  }[];

  [key: string]: number | string | any | undefined;
}

在此处输入图像描述

您的示例非常好,结帐游乐场

You can make the property optional like this:您可以像这样使属性成为可选:

type CountersData = {
    [key in number]?: {
        count_targets: number;
        count_targets_excluded: number;
        count_targets_pending: number;
        count_targets_in_progress: number;
        count_targets_completed: number;
        count_targets_failed: number;
    }[];
} & { [key: string]: number | string | any | undefined };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM