简体   繁体   English

如何在打字稿中键入对象

[英]How to to type objects in typescript

A fairly common case I'm trying to figure out how to handle, is type annotation in json objects returned by some API.我试图弄清楚如何处理的一个相当常见的情况是某些 API 返回的 json 对象中的类型注释。

The spec says: 规范说:

Apart from primitives, the most common sort of type you'll encounter is an object type.除了基元,您将遇到的最常见的类型是对象类型。 This refers to any JavaScript value with properties, which is almost all of them!这指的是任何带有属性的 JavaScript 值,几乎是所有属性! To define an object type, we simply list its properties and their types.要定义对象类型,我们只需列出其属性及其类型。 For example, here's a function that takes a point-like object:例如,这是一个接受点状对象的函数:

// The parameter's type annotation is an object type
function printCoord(pt: { x: number; y: number }) {
  console.log("The coordinate's x value is " + pt.x);
  console.log("The coordinate's y value is " + pt.y);
}
printCoord({ x: 3, y: 7 });

So we should type every parameter and nested objects?那么我们应该键入每个参数和嵌套对象吗? What if I don't know in advance the response body?如果我事先不知道响应主体怎么办? What if the body is very long and rich in nested objects and arrays of objects?如果主体很长并且包含大量嵌套对象和对象数组怎么办?

Which would be the best practice, or the sensible alternatives in the latter case?在后一种情况下,哪种是最佳做法,或明智的选择?

There is not much to do in the face of this problem.面对这个问题,没有什么可做的。

Some libraries and services have cared to package the client to their app (like twilio) so that it is type.一些库和服务关心将客户端打包到他们的应用程序(如 twilio)中,以便它是类型的。 If that's not the case and you are programming against an API (HTTP most of the time), your first alternative is to program very defensively.如果情况并非如此,并且您正在针对 API(大部分时间是 HTTP)进行编程,那么您的第一个选择是非常防御性地编程。 That is, not taking for granted that a given property will be present, as if you were using plain Javascript.也就是说,不要理所当然地认为给定的属性会存在,就像您使用普通的 Javascript 一样。

Another alternative is to do your best to try and type the responses you get from external services based on their documentation.另一种选择是尽最大努力尝试根据外部服务的文档键入从外部服务获得的响应。 You can do it by hand, or (and this is what I tend to do to avoid typos, you can use external tools that are able to transform a JSON response into its corresponding type.您可以手动完成,或者(这是我为避免拼写错误而倾向于做的事情,您可以使用能够将 JSON 响应转换为其相应类型的外部工具。

As an example, Axios allows you to specify the response type when making the call.例如,Axios 允许您在调用时指定响应类型。 This is through the use of typescript generics .这是通过使用打字稿泛型 You can see an example here .您可以在此处查看示例。

You can quickly generate Typescript interfaces by pasting your API response into a tool like https://app.quicktype.io/?l=ts it converts your JSON object into typescript interfaces.您可以通过将 API 响应粘贴到类似https://app.quicktype.io/?l=ts的工具中来快速生成 Typescript 接口,它将您的 JSON 对象转换为 typescript 接口。

Note that if your API responses change, you'll need to ensure that your Interfaces handle all the variations.请注意,如果您的 API 响应发生变化,您需要确保您的接口处理所有变化。

Here is another example of specifying return types using the native 'fetch' method: How to use fetch in TypeScript这是使用本机 'fetch' 方法指定返回类型的另一个示例: How to use fetch in TypeScript

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

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