TypeScript and Types: What Are They and How Do You Use Them?
Must know in TypeScript.
What is TypeScript?π
According to the Documentation of TypeScript, TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.
Well, do I understand that?π€ hmmm, Not really!. Let's break it down.
TypeScript is a programming language built on JavaScript to support additional features. It is strongly typed; meaning that the type of data supported by TypeScript is predefined in the compiler - Consider it a friend who is constantly looking for problems and errors in your code. How sweet is that?π
Now, What are types?
Types are a convenient way to refer to a value's many attributes and methods.
Why should we care about them?π€
- TypeScript compiler uses types to analyze our code for errorsπ―.
- Other developers can see what values are flowing across our program thanks to types.
- Efficient editor support is provided using types.π€
Consider the variable below. It points to an instance of a date object.
When you hover your mouse over the variable's name, The type of the value it contains is displayed by the editor. Okay, what's this saying? The variable name "today" is pointing at an object of type "Date".
This is how TypeScript determines what operations may be done on the object. Since the compiler is bundled with the definition of various types.
For example,
The whole point of type checking is to automatically make the built-in functions and properties associated with an object visible. Just like the picture above. If we try to access a property or method that is not recognized by TypeScript, we immediately get an error.
If we create a plain JavaScript Object and hover over it. Because the type was not specified - a short definition of what the person Object is. TypeScript only prints out the type of the methods and properties it contains as a default.π
Where do we use types?π€©
Well, Everywhere!ππ
Quick question.
- Think about what type is.
- What has a type in TypeScript.
- Why do we care about types.