It’s been a long time since I wrote an article, but a bad experience on Stack Overflow this weekend has made me think. I have read about the subject and I see that I am not alone in the matter that I expose in the title.
As I say, I have read about it, I have suffered it more times, I have seen it as a simple spectator, and I will give my humble opinion of a beginner developer but with a certain age to have drawn certain conclusions from my social interactions, and my short experience as a programmer.
Computing world is, like any other where humans are involved, full of different people. Many times I found talented people willing to help, but many others I found those who give programmers the fame of being individuals who are not able to communicate and have healthy human relationships (and thus, they are much more bad programmers or engineers than they pretend to be).
I am not a very experienced developer but I always try to remember who I am, and who I was. For me, a developer is a perpetual learner and no one is dumb because he/ she doesn´t know how to difference an array from an object. I remember the days when I struggled with that. Also I remember the days I thought I was better than others just because I had a certain ease for something or I just had more experience. Fortunately I was young and life taught me to be humble.
A lot has been said algo about the (supposed) big ego of developers, and well, that is for me one of the biggest problems in the computing world, the big egos of some people with maybe advanced technical skills but a total lack of social and soft skills. And I am not only talking from the perspective of a new developer: Usually, the most rude and condescending people don’t know how to talk to a customer, they don´t know how to extract requirements the clients need, they don’t know how to work in a team. That is why many times their bosses are people, maybe not so tech-skilled but with many important skills they don´t have. I would definitely don’t want them in a team if I had to hire someone, or if I had to work with them.
For me, rude answers usually come from people who usually try to cover a lot of self esteem issues and should look for help, because trying to subjugate people in one field that you are good at, tells more from you than from that person you answer to, and is not good.
I see it simple: if you are in a community but don’t want to help, simply don´t answer. You are no one to tell anyone he/she is asking a dumb question. Even if he/she does, maybe is only a newbie, has a bad day, or yeah, maybe is a lazy one (this of course annoys most of us). But there are tons of ways to point things, and maybe he/she realizes by him/herself it was a dumb question with your kind help. Or maybe is a dumb question for you now as experienced user and you forgot the dumb questions you did at the beginning of your career.
Rude answers are not a way to make a community like Stack Overflow (or an office, or an start-up, or any place) more efficient, just make it more toxic; because when you do such, you are not trying to help, you are just trying to convince yourself that you are better than you internally think you are. Is like an answer in front of a mirror.
To conclude, I would recommend humility, because when you need help, they may treat you as you treat others.
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:
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.
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
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
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
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
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:
As it is an array, we can initialize inside the constructor it simply as:
Now we must change the HTTP response code. So you can replace this:
Apart from the type change we have done for the property, we can see we changed two things inside the HTTP request:
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.