What are types in TS?
Types in TS helps us understand what methods & properties are associated with a given value/variable in a program that can help us analyze our code for existing errors and prevent further errors.
For example a value that is assigned a type of a string tells us that the value has access to string methods like startWith(), substr() , slice() etc. Similarly a value assigned a type of array gives us an idea that the corresponding value has access to array methods like push() , pop ().
Why are types important?
Since types allow us to understand what values are associated with variables , it gives context to our variables and we are more aware of the different moving parts inside our code and which removes ambiguity. Without types it would be very easy to call a string method on an array which happens all the time in Java script as it does not do the type checking for us to only wonder what went wrong when the error pops up during the run time. With Typescript’s typing system the error is thrown in the editor itself when we are writing the code.
What is any Type in TS and why should we avoid it?
Any type is a special type in TS that is used to tell Typescript that a variable can be of any type. A variable with the type
any means it can be a string , a number or anything .
The compiler treats
any as “please turn off type checking for this value” i.e now Typescript does not know the nature of the variable and would not throw errors if we called some arbitrary method on that variable which may not be associated with that variable. For example – calling pop() on a string.
Also , with type checking turned off we also lose access to handy auto complete and suggestions features built into the IDE / editors .
What are the alternatives?
Often when dealing with 3rd party libraries or Data returned from 3rd party API’s , developers tend to take a shortcut in order to save time and effort by using the any type which later on becomes a potential source of errors and issues. Instead, it is important to understand the type being returned and type the variables accordingly . Understanding the type being used will save us time and help us avoid bugs in the long run and will also ensure that we understand this code and ensure this solves the problem that is being worked upon.
To infer the type one can always go to the type definitions provided by the libraries themselves and perhaps import the types themselves from the library if the library happens to also export them . For 3rd party API’s one can always explore the docs if any else try to infer the types using 3rd party tools like https://quicktype.io/ .
Another alternative is to use unknown type . One can think of unknown as type safe counter part of any type. With
unknown we cannot assign a variable with the
unknown type to another variable with a type.
TS with its typing layer on top of java script helps us analyze our code for errors. Using any will turn off the type checking and essentially take away all of that from developers and it will be much more difficult to write type safe code . Using any type is an anti pattern and should be avoided i.e Don’t use