What is TypeScript?
What can I do with TypeScript?
Static type safety — what it is?
Type safety is a programming language feature for preventing type errors. It involves ensuring that variables are of a certain type before being used. It ensures that we are using variables of the correct type.
Type safety means that TypeScript will throw errors if we attempt to use variables or functions that we’ve not declared. This means that we’re going to end up having fewer surprises and will end up writing code that is more maintainable. The code that’s produced by TypeScript is more readable too.
Type guards are similar to type safety. They allow us to easily perform type checks in the first place. They act as a way to make sure that we’re not using variables or functions that we don’t have declared. This means that we won’t get caught by surprise when using variables or functions.
For example, let’s say that we have a function that calculates a tax amount. We would like to make sure that we’re using the correct types of variables in order to correctly calculate this amount. We can check the types of our variables using a type guard.
Type inference allows us to write less code. This is because the compiler is able to infer the type of variables. It can often infer the type of the variables if we omit the type. In the following example, we can omit the type annotation for the first variable, and the compiler will infer the type of it.
Another thing that TypeScript allows is for us to omit type annotations for functions as long as we provide an argument with the type that is being used for the function.
Should you learn TypeScript?
Should you learn both?