Note: I may be a bit biased towards typescript. There is no project that I start where I prefer JS to typescript.
What is typescript?
Then, why do we use TypeScript?
How does TypeScript?
But with TypeScript, we can restrict this behavior by explicitly declaring a type for a variable. If we try to specify the string in a variable of “number”, it generates an error.
aNumber Variable Representing an object. While this is not an error that will crash the program, it beats the code for the purpose of using variable names in self-documentation.
TypeScript easily resolves this issue by defining the type of the variable during declaration so that it cannot be assigned to another type of value.
If another developer in your program has access to this variable, they can now rely on its value, as the name suggests.
in this matter,
Again, TypeScript has a solution for this problem.
You may not understand this code at this time. But notice how this ensures that the type passed is the type of person, defined at the beginning.
Using TypeScript will cause hundreds of careless coding mistakes from your program and can prevent your hair from falling out every time you encounter the biggest problem of bugs. This will also better self-document your code and increase its stability.
Using types with TypeScript
TypeScript has several basic types that are pre-defined. Numbers, string, boolean and array are some examples of them.
You can find the complete list of basic types Typescript documentation.
Here are a few examples:
Likewise, try to avoid using a union of types, but if it is unavoidable, limit the number of types allowed in the union as much as possible.
Announcement of custom types
Remember how I used a type called Persona in the previous code example? The person in TypeScript is not a basic data type. I created the person type according to my requirements to use as the type of parameter accepted by the given function.
We use interfaces to define a new type of basic structure that we are familiarizing with the application.
Now, if we create a new type of object, it should have the field name and age within it. If not, TypeScript throws an error.
You can also define optional fields inside an interface.
You can then use the custom type as a field type when defining another type.
In TypeScript, you can get any other type of properties by extending its interface.
Suppose your application requires two different types, person and employee. Since an employee is also a person, it makes sense to derive person type attributes when creating an employee interface. This code prevents repetition.
You can achieve this by increasing the individual interface.
Function Parameter Type and Return Type
Similar to variable types, you can define types of function parameters and return values. While the parameter type is declared next to the parameter name, the return type is declared just before the curly braces.
With a defined parameter and type of return value, we can guarantee that you or anyone else using this function has not accidentally passed an object that does not have car type features.
You can also guarantee that the area
Similar to variables, you can define returns and parameter types as multiple types of associations.
When you declare an accepted parameter or return type, the objects of the type that extend the initialization type interface are also accepted as arguments or return values.
With TypeScript, you can easily define generic variables like the ones we’ve covered so far. If you are defining a common function, you can use it to process data that is of any built-in or custom type.
What happens if you use ‘someone’ type instead of generics?
Of course, you can change the above function to accept any type of arguments using ‘any’ type.
However, this method does not preserve the type of data provided on the function. Instead, it records each argument in some way. Also you should avoid its use
With generics, however, you can preserve what type of data is passed to the function. If you want to change function logic according to the type of data passed, then using a generic is better than accepting any type of data.
Using type aliases
When you want to use a particular field, it can be one of several types, you can define its type as a union of those different types.
Now, you do not have to use long association types. Also, if you want to change the return type of the function in the future, you now have to change only one line code.
When one type is defined by extending the interface to the other, the relationship generated between the two allows us to transform the objects defined in one of them to the other.
Type Car and Importedkar which I defined earlier. First, I’ll create an object of type ImportedCar and see how the conversion works on it.
This code compiles without an error. It makes sense that this conversion works because the ImportedCar type already has all the fields defined in the car type.
If we try to access the constructor field defined in the object before conversion, it generates an error because the changed object is of type Car.
this Article Was originally published Live code stream by Juan Cruz Martinez (Twitter: @bajcmartinez), Founder and publisher of live code streams, entrepreneurs, developers, authors, speakers and doers.
Live code stream Also available as a free weekly newspaper. Sign up for updates on everything related to programming, AI and computer science in general.