简体   繁体   English

如何在 API 响应数据上使用 TypeScript 类型

[英]How to use TypeScript types on API response data

So I make a call to the api and return data form it.所以我打电话给 api 并从它返回数据。 It contains way more data than I need, so I map trough my response and return only values that I need.它包含的数据比我需要的多得多,所以我 map 通过我的响应并只返回我需要的值。 The problem is that I do not understand how to define my response data in TS.问题是我不明白如何在 TS 中定义我的响应数据。 Now it looks like this but I know that using any is a bad option .现在看起来像这样,但我知道使用 any 是一个不好的选择

data = data.results.map((item: any) => {
                        return {
                            id: item.id,
                            src: item.urls.small,
                            description: item.alt_description,
                            name: item.user.name,
                            favorited: false
                        }
                    })

How should I transform response data to format that I need using TS.我应该如何使用 TS 将响应数据转换为我需要的格式。 I think I need some additional step so I could use my interface on item.我想我需要一些额外的步骤,以便我可以在项目上使用我的界面。

interface PhotoModel {
    id: string
    src: string
    description: string
    name: string
    favorited: boolean
    }

You need to create some interface or type that will describe the data you're going to process.您需要创建一些接口或类型来描述您要处理的数据。 For example:例如:


interface ResultItem {
  id: string;
  urls: {
    small: string;
  };
  alt_description: string;
  user: {
    name: string;
  };
}

interface PhotoModel {
  id: string
  src: string
  description: string
  name: string
  favorited: boolean
}

data.results.map((item: ResultItem): PhotoModel => {
    return {
        id: item.id,
        src: item.urls.small,
        description: item.alt_description,
        name: item.user.name,
        favorited: false
    }
})

However (especially if you don't control the shape of the API you're requesting), in runtime you might not get what you expect to get.但是(特别是如果您不控制您请求的 API 的形状),在运行时您可能无法获得预期的结果。 So it would be beneficial to validate the data returned from the API first (for example, using some tool like io-ts or a similar one).因此,首先验证从 API 返回的数据将是有益的(例如,使用诸如io-ts或类似工具之类的工具)。

You can just return data.results as PhotoModel array您可以将 data.results 作为 PhotoModel 数组返回

return data.results as PhotoModel[]

The best way would be to just cast the return to your desired type.最好的方法是将 return 转换为您想要的类型。 Typescript will not let you use any of the extra fields: and you can get away without any processing: Typescript 不会让您使用任何额外的字段:并且您无需任何处理即可逃脱:

const data: PhotoModel[] = data.results;

If you really want to mutate your data for some reason and this is insufficient, you can do this:如果您出于某种原因真的想改变您的数据并且这还不够,您可以这样做:

data = data.results.map((item: any): PhotoModel => {
                    return {
                        id: item.id,
                        src: item.urls.small,
                        description: item.alt_description,
                        name: item.user.name,
                        favorited: false
                    }
                })

Using any is not bad here as even if you define an interface, it has no value.在这里使用any也不错,因为即使您定义了一个接口,它也没有任何价值。

let photos: PhotoModel[] = data.results.map((item: any) => {
                        return {
                            id: item.id,
                            src: item.urls.small,
                            description: item.alt_description,
                            name: item.user.name,
                            favorited: false
                        }
                    })

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

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