Note: I may be a little biased towards Typescript. There is no project where I prefer JS over Typescript.
What is typescript?
Then why do we use Typescript at all?
How does Typescript do it?
With Typescript, however, we can limit this behavior by explicitly declaring a type for a variable. If we try to assign a string to a variable of type “number”, an error is generated.
VS code warning in the event of incorrect type assignment
Typescript easily solves this problem by defining the type of the variable during the declaration so that it cannot be assigned a value of any other type.
If another developer has access to this variable in your program, they can now rest assured that its value is a number that exactly matches its name.
Here, too, Typescript has the solution to this problem.
This code may not make sense to you at this time. However, note how it ensures that the type of variable passed is of type Person, which is defined at the beginning.
Using Typescript will remove hundreds of negligent coding errors from your program and you won’t have to work your hair out every time you run into the silliest mistakes. In addition, your code will be better self-documented and maintainability improved.
Using types with typescript
Typescript has a number of basic types that are predefined. Number, string, boolean, and array are some examples.
The full list of basic types can be found in the Typescript documentation.
Here are some examples:
Similarly, try to avoid using a union of types. However, if this is unavoidable, limit the number of types allowed in the union as much as possible.
Declare custom types
Do you remember how I used a type called Person in a previous code example? Person is not a basic data type in Typescript. I created the person type as per my requirements to be used as a parameter type accepted by the specified function.
We use interfaces to define the basic structure of a new type that we introduce into the application.
Now when we create a new type object it should contain the field name and age. If not, Typescript throws an error.
VS Code warning of missing properties in custom types
You can also define optional fields within an interface.
You can then use a custom type as the field type when defining a different type.
In Typescript, you can inherit the properties of another type by extending its interface.
For example, suppose your application requires two different types: Person and Employee. Since an employee is also a person, it makes sense to inherit the properties of the person type when creating the employee interface. It prevents code repetition.
You can do this quickly by expanding the person interface.
Function parameter types and return types
Similar to variable types, you can define types for function parameters and return values. While the parameter type is declared next to the parameter name, the return type is declared immediately before the curly braces.
With the defined parameter type and return value, we can guarantee that you or anyone else using this function will not inadvertently pass an object that does not have the properties of the vehicle type.
Similar to variables, you can define the return and parameter types as a union of several types.
If you declare the accepted parameter or return type, objects of types that extend the interface of the initial type are also accepted as an argument or return value.
Use generic drugs
With Typescript you can define generic variables just as easily as before. If you define a generic function, you can use it to process data that is of one of the built-in or user-defined types.
What if you use the “any” type instead of generic?
Of course, you can modify the above function to accept all kinds of arguments of type ‘any’.
However, this method does not retain the data type passed to the function. Instead, it records any argument passed as belonging to any type. Also, you should avoid the use of any.
With generics, however, you can keep what type of data is passed to the function. If you want to change the functional logic according to the type of data being passed, then using generics is better than accepting data of any type.
Use of type aliases
If a particular field that you want to use in your application can be one of several types, you can define its type as a union of these separate types.
Now you don’t have to use a long union of types. In the future, if you want to change the return type of the function, all you have to do now is change one line of code.
When one type is defined by extending the interface of another, the relationship generated between the two gives us permission to convert objects defined in one of them to another.
Take the previously defined Car and ImportedCar types. First I create an object of type ImportedCar and see how the conversion works on it.
This code will compile without errors. It makes sense that this conversion works, as the ImportedCar type already has all the fields defined in the Car type.
If we try to access the manufacturer field defined in the object before conversion, an error is generated because the converted object is of type Auto.
These items was originally published on Live code stream by Juan Cruz Martinez (Twitter: @bajcmartinez), Founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker and maker of things.
Live code stream is also available as a free weekly newsletter. Sign up for updates on everything related to programming, AI and computer science in general.