打字稿不定式界面推断自身

发布于 2025-02-10 00:54:51 字数 3270 浏览 3 评论 0原文

我正在寻找一种从此API响应中制作类型/接口的方法,嵌套注释属性对我来说是具有挑战性的。

"data": [
            {
                "id": 1,
                "comment": "This is the comment to another comment",
                "approved": true,
                "archived": 0,
                "pinned": 0,
                "reported": 0,
                "created_at": "2022-06-24T06:12:25.000000Z",
                "updated_at": "2022-06-24T06:12:25.000000Z",
                "comments": [
                    {
                        "id": 2,
                        "comment": "This is the comment to another comment",
                        "approved": true,
                        "archived": 0,
                        "pinned": 0,
                        "reported": 0,
                        "created_at": "2022-06-24T06:12:37.000000Z",
                        "updated_at": "2022-06-24T06:12:37.000000Z",
                        "comments": [
                            {
                                "id": 3,
                                "comment": "This is the comment to another comment",
                                "approved": true,
                                "archived": 0,
                                "pinned": 0,
                                "reported": 0,
                                "created_at": "2022-06-24T06:32:52.000000Z",
                                "updated_at": "2022-06-24T06:32:52.000000Z",
                                "comments": [],
                                "commented": {
                                    "id": 1,
                                    "first_name": "Test",
                                    "last_name": "Testi",
                                    "full_name": "Test Testi"
                                }
                            },
                            {
                                "id": 4,
                                "comment": "This is the comment to another comment",
                                "approved": true,
                                "archived": 0,
                                "pinned": 0,
                                "reported": 0,
                                "created_at": "2022-06-24T06:32:57.000000Z",
                                "updated_at": "2022-06-24T06:32:57.000000Z",
                                "comments": [],
                                "commented": {
                                    "id": 1,
                                    "first_name": "Test",
                                    "last_name": "Testi",
                                    "full_name": "Test Testi"
                                }
                            }
                        ],
                        "commented": {
                            "id": 1,
                            "first_name": "Test",
                            "last_name": "Testi",
                            "full_name": "Test Testi"
                        }
                    }
                ],
                "commented": {
                    "id": 1,
                    "first_name": "Test",
                    "last_name": "Testi",
                    "full_name": "Test Testi"
                }
            },
        ],

I'm looking for a way to make a type/interface from this API response, The nested comments property is challenging for me.

"data": [
            {
                "id": 1,
                "comment": "This is the comment to another comment",
                "approved": true,
                "archived": 0,
                "pinned": 0,
                "reported": 0,
                "created_at": "2022-06-24T06:12:25.000000Z",
                "updated_at": "2022-06-24T06:12:25.000000Z",
                "comments": [
                    {
                        "id": 2,
                        "comment": "This is the comment to another comment",
                        "approved": true,
                        "archived": 0,
                        "pinned": 0,
                        "reported": 0,
                        "created_at": "2022-06-24T06:12:37.000000Z",
                        "updated_at": "2022-06-24T06:12:37.000000Z",
                        "comments": [
                            {
                                "id": 3,
                                "comment": "This is the comment to another comment",
                                "approved": true,
                                "archived": 0,
                                "pinned": 0,
                                "reported": 0,
                                "created_at": "2022-06-24T06:32:52.000000Z",
                                "updated_at": "2022-06-24T06:32:52.000000Z",
                                "comments": [],
                                "commented": {
                                    "id": 1,
                                    "first_name": "Test",
                                    "last_name": "Testi",
                                    "full_name": "Test Testi"
                                }
                            },
                            {
                                "id": 4,
                                "comment": "This is the comment to another comment",
                                "approved": true,
                                "archived": 0,
                                "pinned": 0,
                                "reported": 0,
                                "created_at": "2022-06-24T06:32:57.000000Z",
                                "updated_at": "2022-06-24T06:32:57.000000Z",
                                "comments": [],
                                "commented": {
                                    "id": 1,
                                    "first_name": "Test",
                                    "last_name": "Testi",
                                    "full_name": "Test Testi"
                                }
                            }
                        ],
                        "commented": {
                            "id": 1,
                            "first_name": "Test",
                            "last_name": "Testi",
                            "full_name": "Test Testi"
                        }
                    }
                ],
                "commented": {
                    "id": 1,
                    "first_name": "Test",
                    "last_name": "Testi",
                    "full_name": "Test Testi"
                }
            },
        ],

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦妄 2025-02-17 00:54:51
export interface Comment {
  id: number;
  comment: string;
  approved: boolean;
  archived: boolean;
  pinned: boolean;
  reported: boolean;
  created_at: string;
  updated_at: string;
  comments: Comment[];
  commented: {
    id: number;
    first_name: string;
    last_name: string;
    full_name: string;
  };
}
export interface Comment {
  id: number;
  comment: string;
  approved: boolean;
  archived: boolean;
  pinned: boolean;
  reported: boolean;
  created_at: string;
  updated_at: string;
  comments: Comment[];
  commented: {
    id: number;
    first_name: string;
    last_name: string;
    full_name: string;
  };
}
江心雾 2025-02-17 00:54:51

Typescript Playground Link

Comment is part of lib.dom .d.ts,所以更好地使用commentdata而不是

interface CommentData {
  id: number;
  comment: string;
  approved: boolean;
  archived: number;
  pinned: number;
  reported: number;
  created_at: string;
  updated_at: string;
  comments: CommentData[];
  commented: User;
}

interface User {
  id: number;
  first_name: string;
  last_name: string;
  full_name: string;
}

interface Data {
  data: CommentData[];
}

const data: Data = {
  data: [
    {
      id: 1,
      comment: "This is the comment to another comment",
      approved: true,
      archived: 0,
      pinned: 0,
      reported: 0,
      created_at: "2022-06-24T06:12:25.000000Z",
      updated_at: "2022-06-24T06:12:25.000000Z",
      comments: [
        {
          id: 2,
          comment: "This is the comment to another comment",
          approved: true,
          archived: 0,
          pinned: 0,
          reported: 0,
          created_at: "2022-06-24T06:12:37.000000Z",
          updated_at: "2022-06-24T06:12:37.000000Z",
          comments: [
            {
              id: 3,
              comment: "This is the comment to another comment",
              approved: true,
              archived: 0,
              pinned: 0,
              reported: 0,
              created_at: "2022-06-24T06:32:52.000000Z",
              updated_at: "2022-06-24T06:32:52.000000Z",
              comments: [],
              commented: {
                id: 1,
                first_name: "Test",
                last_name: "Testi",
                full_name: "Test Testi",
              },
            },
            {
              id: 4,
              comment: "This is the comment to another comment",
              approved: true,
              archived: 0,
              pinned: 0,
              reported: 0,
              created_at: "2022-06-24T06:32:57.000000Z",
              updated_at: "2022-06-24T06:32:57.000000Z",
              comments: [],
              commented: {
                id: 1,
                first_name: "Test",
                last_name: "Testi",
                full_name: "Test Testi",
              },
            },
          ],
          commented: {
            id: 1,
            first_name: "Test",
            last_name: "Testi",
            full_name: "Test Testi",
          },
        },
      ],
      commented: {
        id: 1,
        first_name: "Test",
        last_name: "Testi",
        full_name: "Test Testi",
      },
    },
  ],
};

console.log(data);

Typescript Playground Link

Comment is part of lib.dom.d.ts, so better use CommentData instead

interface CommentData {
  id: number;
  comment: string;
  approved: boolean;
  archived: number;
  pinned: number;
  reported: number;
  created_at: string;
  updated_at: string;
  comments: CommentData[];
  commented: User;
}

interface User {
  id: number;
  first_name: string;
  last_name: string;
  full_name: string;
}

interface Data {
  data: CommentData[];
}

const data: Data = {
  data: [
    {
      id: 1,
      comment: "This is the comment to another comment",
      approved: true,
      archived: 0,
      pinned: 0,
      reported: 0,
      created_at: "2022-06-24T06:12:25.000000Z",
      updated_at: "2022-06-24T06:12:25.000000Z",
      comments: [
        {
          id: 2,
          comment: "This is the comment to another comment",
          approved: true,
          archived: 0,
          pinned: 0,
          reported: 0,
          created_at: "2022-06-24T06:12:37.000000Z",
          updated_at: "2022-06-24T06:12:37.000000Z",
          comments: [
            {
              id: 3,
              comment: "This is the comment to another comment",
              approved: true,
              archived: 0,
              pinned: 0,
              reported: 0,
              created_at: "2022-06-24T06:32:52.000000Z",
              updated_at: "2022-06-24T06:32:52.000000Z",
              comments: [],
              commented: {
                id: 1,
                first_name: "Test",
                last_name: "Testi",
                full_name: "Test Testi",
              },
            },
            {
              id: 4,
              comment: "This is the comment to another comment",
              approved: true,
              archived: 0,
              pinned: 0,
              reported: 0,
              created_at: "2022-06-24T06:32:57.000000Z",
              updated_at: "2022-06-24T06:32:57.000000Z",
              comments: [],
              commented: {
                id: 1,
                first_name: "Test",
                last_name: "Testi",
                full_name: "Test Testi",
              },
            },
          ],
          commented: {
            id: 1,
            first_name: "Test",
            last_name: "Testi",
            full_name: "Test Testi",
          },
        },
      ],
      commented: {
        id: 1,
        first_name: "Test",
        last_name: "Testi",
        full_name: "Test Testi",
      },
    },
  ],
};

console.log(data);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文