I am sure you, as a web developer, have faced a lot of times the problem of having to deal with complex Objects in your Typescript or Angular projects. One way to do it is creating our custom Typescript interfaces or classes, but it could give us a lot of work when, as said, the objects are too complex . We will see in this article how to create our Typescript interfaces using a powerful tool called Quicktype.io . This tool can be accessed through the website but also as a Visual Studio extension.
Probably many of you are experienced developers who know the benefits of Typescript interfaces and custom types much better than me, but the point here is, among everything, how to use quicktype.io to deal with big Objects, and to make an interface for those big Objects in less than a minute even if they have dozens of properties.
First of all, we will see a piece of code which shows us one typical response returned by an HTTP request in Angular, from the Giphy API. I am currently developing an app which uses Giphy API to show images, among other functionalities, and I will use some pieces of code to show you how to use it and the great advantages of creating your custom Typescript interfaces.
This time, as it is a long code, will just link it and not embed, for readability reasons:
https://gist.github.com/Franweb79/58671911ea12b583adc7712efb82d369
As you can see, it is an Object with a lot of properties, nested ones… it is easy to have errors trying to access those properties, and is very difficult to trace a path to one nested property, you could have mistyping errors…
Advantages creating custom Typescript interfaces
With Typescript interfaces, we will be able to avoid the errors mentioned above more easily, taking advantage of the help that Typescript can give us to access an Object and its properties.
Not only that: as mentioned above, with Quicktype.io we will be able to create our Typescript interface for such complex Objects fast and easily.
Let´s imagine we will request 10 items from Giphy with our HTTP request. The limit of items can be as a query parameter, as we will see later. Please read Giphy documentation to know more about it.
The result will be a very big Object with thousands of lines. But it doesn´t matter. Let´s see the code. First we will intentionally use a typical bad practice to request data, using the “any” types.
Bad way of requesting data: using “any” type
This would be our HTTP request. The ApiData property is where we will store our data to be shown in the template.
As you can see we have declared a variable called wholeQueryString where we can set the limit of items we want to request. To see more info about the other parameters I recommend you again to visit Giphy for Developers documentation.
let wholeQueryString:string=`${URL}${Key}&limit=10&q=${searchQueryParameter}`;
Also we have set the property and the parameter for the HTTP request as “any”. Sometimes developers use this to avoid Typescript throwing errors. For example, when we do
this.APIdata=response.data;
If we don´t declare the response as “any”, Typescript will say something like “ey, this is a generic Object type, I can´t see a data property inside because it is not part of a generic Object type”.
We can solve it by setting the response as “any”, it is like telling Typescript: “Is OK, I know what I am doing, just let me do”. Then it won´t complain:
But this way, we must know our response Object structure; we must check the properties and know our response has a data property. That can get worse if we must access something like
response.data[0].url
As mentioned above, it is easy that you have errors trying to follow complex, nested property paths.
It is a solution, our software will work; but is not the best way to do it.
Using quicktype.io to create our Typescript interfaces
Let´s create then our custom Typescript interface, see how it helps us, and how to create it with quicktype.io
If you know something about Typescript interfaces, you can figure out how hard it would be to build our interface for an Object as we get on this Giphy response, isn´t it? Imagine you have to check more than 20 properties, declare types… and same for nested Objects.
Fortunately we will use quicktype.io to do that now.
First we run our HTTP code.
As we have a
console.log (response);
It will show us the complete Object that Giphy returns.
We just copy the entire Object and paste it inside the left panel of quicktype.io. It will transform our JSON Object into typesafe code in any language, including Typescript.
Remember to copy the entire Object, just right-click over it and select “copy object”, works the same for Firefox and Google Chrome.
You must write a name for your Typescript interfaces, in this case we will use GiphyResponse. Then, in the options button you can select the language you want the JSON to be parsed. In our case it is Typescript.
And that’s it! We have a complete interface of our long response in less than one minute!
Using the quicktype.io generated data for our projects
Now that we have the code, we just can copy and paste it to create our own Typescript interfaces inside our Angular project.
First we will create a file called
giphy-response-interfaces.ts
Preferably do it inside an “interfaces” folder. Just copy the code generated by quicktype.io
Now we are ready to use our custom Typescript interfaces wherever we need inside our project. And now, we will use it to handle Objects easily with the help that Typescript can give us.
We will change the “any” types we have written before, with our new custom type, GiphyResponse, or derived ones.
We can see our interface has now The relevant data we need, as its own name says, will be inside the “data” property.
Remember we will store our data inside a property. As we see, the data is an array of Datum type, so we change:
public APIdata:any;
to
public APIdata:Datum[];
As it is an array, we can initialize inside the constructor it simply as:
this.APIdata=[];
Now we must change the HTTP response code. So you can replace this:
for this:
Apart from the type change we have done for the property, we can see we changed two things inside the HTTP request:
- We used our new custom type and Typescript generics (the code inside <> ).
this._http.get<GiphyResponse>
- Now the response parameter has also our custom type. Please note that it would now work even just leaving the response without type:
next: (response)=>{ this.APIdata=response.data; console.log (response); }
But I humbly think it is a better idea to specify this response with our custom type
next: (response:GiphyResponse)=>{ this.APIdata=response.data; console.log (response); }
Well, and where is the advantage of all of this?
Look what happens now:
Yes! Now Typescript knows the structure and property of the Object, thanks to our custom Typescript interfaces. We have told Typescript something more specific and well described than a vague “any” type so it can help us.
The best advantage is that, if we have to access now to any nested property inside the object, we can do it much more easily, with Typescript safely guiding us through the structure of the Object.
Look at this. Remember data property is an array, so we could do, for example, for first element of the array:
You can see all nested properties, objects… much easier than exploring the browser console and following the paths by yourself, right?
I hope this article was useful for you. Remember to check other Angular or Typescript related articles we have written.
Image by James Osborne found in Pixabay