typescript 我试图声明一些类型,并不断得到TS1131错误,数据来自API

vc6uscn9  于 2023-05-19  发布在  TypeScript
关注(0)|答案(1)|浏览(188)

所以我理解这个问题,知道对象需要被标记。但是API返回的数据没有任何特定的标签。数组中的一个对象。
现在,当我只为第一个对象编写时,一切都很好。但我也需要第二个物体的数据。因此,我需要声明类型。
这是我的代码。
这是来自API的数据
这些对象是两个不同的对象,需要分别访问。

{

label_1: {
        "arr_1": [{
            "key": "value",
        }],
        "arr_2": [{
            "key": "value"
            "arr_3": [{
                "key": "value"
            }],
            "arr_3": [{
                "key": "value"
                    "label_2": {
                        "key": "value"
                        "arr_4": [{
                            "key": "value"
                            "arr_5": [{
                                "key": "value"
                            }]
                        }],
                        "arr_6": [{
                            "key": "value"
                            "label_3": {
                                "key": "value"
                                "label_4": {
                                    "key": "value"
                                    }
                            }
                        }]
                    }
                }
            , **This is the Huge issue** **How can I fix this** {
                "key_x": "value_x"
                "label_x_1": {
                    "label_x_2": {
                        "key_x": "value_x"
                        "arr_x_1": [{
                            "key_x": "value_x"
                            "label_x": {
                                "key_x": "value_x"
                            }
                        }]
                    }
                }
            }] 
        }]

}

}

这是类型声明。我不能声明arr_3,因为它有两个不同的对象。我喜欢单独访问这些对象。如果我只声明一个对象,一切都很好。并且TS使用数组的标签作为对象的名称。
我不能对同一个数组中的两个对象这样做。

Data: {


label_1: {
        "arr_1": {
            "key": "string"
        }[];
        "arr_2": {
            "key": "string";
            "arr_2_1": {
                "key": "string"
            }[];
            
            
            **This is the issue**
            
            "arr_3": [{
                "key": "string";
                    "label_2": {
                        "key": "string";
                        "arr_4": {
                            "key": "string";
                            "arr_5": {
                                "key": "string"
                            }[];
                        }[];
                        "arr_6": {
                            "key": "string";
                            "label_3": {
                                "key": "string";
                                "label_4": {
                                    "key": "string"
                                    };
                            };
                        }[];
                    };
                };
            ,
**This is the Huge issue** **How can I fix this** 

 {
                "key_x": "string";
                "label_x_1": {
                    "label_x_2": {
                        "key_x": "string";
                        "arr_x_1": {
                            "key_x": "string";
                            "label_x": {
                                "key_x": "string"
                            };
                        }[];
                    };
                };
            };
            
          
          **end of array with issue**
            ] 
            
            
            
        }[];

};

}

我以为不会有问题。我尝试用数组的名称命名对象,但我得到此错误Duplicate identifier 'arr_2'.ts(2300)。有人有这个问题吗?我该怎么解决这个问题?

nx7onnlm

nx7onnlm1#

我猜到了刚刚意识到对象大括号{}并不重要,所有内容都应该放在同一个{}中。
就像这样

Data: {


label_1: {
        "arr_1": {
            "key": "string"
        }[];
        "arr_2": {
            "key": "string";
            "arr_2_1": {
                "key": "string"
            }[];
            
            
            **This is the issue**
            
            "arr_3": {

                    "label_x_2": {
                        "key_x": "string";
                        "arr_x_1": {
                            "key_x": "string";
                            "label_x": {
                                "key_x": "string"
                            };
                        }[];
                    };

                "key": "string";
                    "label_2": {
                        "key": "string";
                        "arr_4": {
                            "key": "string";
                            "arr_5": {
                                "key": "string"
                            }[];
                        }[];
                        "arr_6": {
                            "key": "string";
                            "label_3": {
                                "key": "string";
                                "label_4": {
                                    "key": "string"
                                    };
                            };
                        }[];
                    };
                }[];

            
            
            
            
        }[];

};

}

相关问题