The power of TypeScript
TypeScript, or TS for short, is a programming language that builds upon JavaScript by adding strong typing and advanced features commonly found in object-oriented languages such as C++, Java, and C#. One of the key advantages of using TypeScript is the ability to utilize interfaces and generics, which can greatly enhance the development experience and improve the overall quality and maintainability of your code. In this article, we will take a closer look at what TypeScript is, its relationship to JavaScript, and the benefits of using interfaces and generics in your code.
Types
One of the most straightforward concepts in TypeScript is the idea of "types." As mentioned earlier, TypeScript is a superset of JavaScript, meaning that it includes all the features of JavaScript, but also adds additional functionality. Therefore, any code written in JavaScript will also work in TypeScript, but TypeScript provides the added benefit of being able to explicitly define the types of variables, function parameters, and return values. This can improve the readability and maintainability of your code, as it becomes more clear what kind of data is expected in each location. The main advantage of TypeScript reveals itself when working in a team with different classes and different ways of thinking (Which is important). When you, as a developer, try to use a class that has been constructed by another member of your team how can you be sure you are using the right data type for the parameters? What is the data type of the returned value? All these kinds of questions are answered in TypeScript using types.
Here are two images that show the main difference in such a simple feature:
Also as you can see in the first image I have the "void" return type for the method. In this way, I can inform future developers that this method isn't going to return anything but change the object values.
Of course that this was a very brief introduction and there are way more things only for types. For example, you can create types of yourself, or use enums of types but I will let you search how to do that. The main concept is that everything needs to be in a concrete type at the end.
Interfaces
In my perspective, the concept of interfaces is just like types but for objects and it's a bit more "scalable" over the whole architecture of the software because you can reuse them. Also, you can see interfaces as shared models for the front-end and back-end of your application. No more "What is the back-end looking for in user?" Just use the correct interface and it describes itself.
Here is a real-life example of how TypeScript helps two developers one from the back end and one from the front end to communicate the types of objects only from an interface. In opposition to JavaScript where they need to actively look for what the types are.
Side note: there is a concept of DTO classes that are stronger validation types than interfaces but this is not relevant for now.
领英推荐
When you use the TypeScript approach you will encounter errors when trying to use a different type than what is needed. TypeScript is here to help you to develop easy-to-maintain applications.
Generics
Generics in TypeScript in a simple explanation are "Types to be declared later". That's all! The convention for a generic is using the capital T letter as a generic type for any type that will come into the function or class. But if this can be any, why don't just use "any"? Here is a visual example:
Now let's see an example of two new users that are coming from different social networks. One is from Google where the id of the user is of type "number", and the other in this case is from Twitter and where the id is of type "string". How can we be sure that we update the user ID in the correct way with the current class? This can be literally "any" type! Now let's see how we can leverage the power of a generic interface that will get the types from our concrete Google & Twitter types.
As you can see in the image above, we get a squiggly line under our input from the method because the new Google user is expecting a number, and the new Twitter user is expecting a string.
But as you can see this code is not that easy to read, there are a lot of abstractions and things that aren't really about developing a feature but developing a structure for the development of the features. Generics are most used in big libraries where they want the tools to be strongly typed but also very dynamic, or in some other cases those generic types can help us to work as a team if we have multiple services that need to communicate with each other, this kind of tool help us and other developers to communicate our original intention to what this class should get, and what this class should return and in which type.
Conclusions
I hope this brief introduction will give you the hunger to dive deep into the world of TypeScript and create very scalable classes and services.
Types, interfaces, and Generics are great tools to insure we develop our software in a reliable way. With the help of such features, we can move faster, and safer with our program. I genuinely suggest you move from the old loved JS to TypeScript and not because "It's harder so people will be impressed" but because it will actually help you to be a better developer.
See you next time!
CEO @ Immigrant Women In Business | Social Impact Innovator | Global Advocate for Women's Empowerment
5 个月???? ??? ?? ?? ???????? ??? ?????? ???? ?????? ???: ?????? ????? ??? ????? ?????? ?????? ??????. ?????? ?????? ?????? ?????,??????? ??????? ???????: https://chat.whatsapp.com/BubG8iFDe2bHHWkNYiboeU
WordPress Expert & Mentor | Empowering Web Success
7 个月???? ??? ?? ??????! ??? ????? ???? ?????? ?????? ????? ?????? ????? ??? ????? ??????? ?????? ?????? ?????? ??????: https://chat.whatsapp.com/BubG8iFDe2bHHWkNYiboeU
Oracle & SQL server DBA
2 年???? ??? ?? ?????? ???!
Fullstack Developer | Angular | HTML | SCSS | TypeScript | Node.js | .Net | Oracle SQL
2 年?? ?? ????!
Software Engineer
2 年https://www.youtube.com/watch?v=xk_PbxR7G8A&t=12s