Generics Interface in typescript
“A major part of software engineering is building components that not only have well-defined and consistent APIs but are also reusable. ” This sentence is in the official documentation we would start with. There are languages that are strong in static typing & others that are weak in dynamic typing. At this point, Typescript has shown to have well-defined APIs with those languages that are weak in dynamic typing.
For every technology approach, we are about to learn we should know why this approach has been created and added, so think about it. We have a function that takes a number and do some operations finally returns a number, and we need a function that takes a string and do the same operations and returns a string, and this is solving the problem of the “any” type and able to capture the type.
Working with arrays:
You just need to add Type before brackets like this, I would take the array example to work, as the examples of interface would treat, in the next example we will create a function that takes an array with a certain type and return the array.
We can also add more than a generic type, I will create a function that takes two arguments with undefined types T & V and return their values
Generics With interface: First, what is the interface in typescript & what is the approach we follow?
Simply, we can say the interface is the schema for an object you create, it defines the properties and their types, and we can define an interface with the interface keyword here is the syntax in the next example we determine an interface and type in it its properties and the schema should the object follow.
And now we can define the generic type by adding <T> in the next example I will create an interface and make the object person applies its properties and passing type of the array
We can also specify What T type should contain a certain property by using extends keyword in the next example we define an interface that takes the T type that should have the id property, secondly, create an interface that has the id property and pass it as a type for the props interface & make customData object apply its properties
In the previous example, if the Object “customData” doesn’t have an “id”, it will be an error. So by the “extends” keyword “customData” object is forced to contain the “id” property, this technique is called “Constraints generics”.
We can also do this in the next example. Create an interface containing the id property then create a function that takes an array of type T that extends the props interface and return the array.
The advantage of generics?
The advantage starts to show in this question: Can we replace generics with any type? Technically no, we can’t, when we are using any as generic, we almost don’t know what are the type function args take and what it returns, we lose information about this function, and as official documentation says: by generics, we can capture the type of functions and variables, by generics we keep and increase type safety.
The disadvantage of generics in the interface?
We can say that you write more code and add more specifications to our code which make the code messy sometimes
Please Login to comment...