TypeScript and Types: What Are They and How Do You Use Them?

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.

Screenshot (5).png

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, Screenshot (7).png

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. Screenshot (9).png 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.
Β