Introduction.
TypeScript is called an advanced version of JavaScript because it adds static type options and object-oriented classes that are intended to make the language more extensible and reliable.
It is open source and has been maintained by Microsoft since they created it in 2012. However, TypeScript had its initial foray as the core programming language in Angular 2. Ever since, it has continued to evolve, as is the React, Vue, and NodeJs community.
In this article you and you will learn the basics of TypeScript with the help of practical examples.
I. Install TypeScript.
Before starting any coding, we need to install TypeScript on our computer. We just need to open a terminal and enter the following command:
npm install -g typescript
(note the computer has npm installed)
Finished installation to check if the installation is successful or not or version of ts, we run the command:
tsc -v
II. Writing some code
Let’s create our first TypeScript file and write the simplest lines of code inside it by.
Open your IDE or Favorite Text Editor and create a file with the name ** first.ts ** – For the TypeScript file we use the .ts extension.
For now, we’ll just write a few simple lines of old JavaScript, since all JavaScript code is also valid TypeScript code:
1 2 3 4 5 6 | let a = 5; let b = 5; let c = a + b; console.log(c); |
To run the .ts file, we compile it to pure JavaScript.
III. Compiling TypeScript.
To compile the ** .ts ** file is very simple, just run the command:
tsc first.ts
This command will help compile the corresponding .js file.
IV. Data types.
TypeScript – as the name implies – is the type of JavaScript version. This means that we can assign types to different variables at the time of declaration. They will always keep the same type of data in that range.
Typing is a very useful feature to ensure reliability and scalability. Type checking helps ensure our code is working as expected. In addition, it helps find errors and errors and records our code correctly.
The syntax for assigning a type to any variable is to write the name of the variable followed by: sign followed by the name of the type followed by = sign and the value of the variable.
There are three different types in TypeScript: any
type, Built-in
type and User-defined
type. Let’s take a look at each one.
1. Any type – any type of data.
Any
is a metadata type of all data types in TypeScript. Giving any variable of type any
is equivalent to not participating in the variable’s data type checking.
1 2 | let myVariable: any = 'This is a string' |
2. Built-in types – Predefined data types
These are the types that are built into TypeScript. These include number, string, boolean, void, null, and undefined.
1 2 3 4 | let num: number = 5; let name: string = 'Alex'; let isPresent: boolean = true; |
3. User-defined types – Data type defined by the user.
The User-defined
types include enum
, class
, interface
, array
, and tuple
. We’ll talk about some of the more common ones in this article.
4. Object-oriented programming – Object-oriented programming
TypeScript supports all features of object oriented programming, such as classes and interfaces. This ability is a huge boost to JavaScript – it has always had trouble with its OOP functionality, especially since developers started using it for large-scale applications.
5. Class.
In object oriented programming, classes are templates of objects. The class defines what an object will look like in terms of the features and functions of that object. The class also encapsulates data for the object.
TypeScript has built-in support for classes, not supported by ES5 and earlier versions. This means we can use the class
keyword to declare it easily.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <span class="token keyword">class</span> <span class="token class-name">Car</span> <span class="token punctuation">{</span> <span class="token comment">// fields </span> model <span class="token operator">:</span> String <span class="token punctuation">;</span> doors <span class="token operator">:</span> Number <span class="token punctuation">;</span> isElectric <span class="token operator">:</span> Boolean <span class="token punctuation">;</span> <span class="token function">constructor</span> <span class="token punctuation">(</span> <span class="token parameter">model <span class="token operator">:</span> String <span class="token punctuation">,</span> doors <span class="token operator">:</span> Number <span class="token punctuation">,</span> isElectric <span class="token operator">:</span> Boolean</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span> <span class="token punctuation">.</span> model <span class="token operator">=</span> model <span class="token punctuation">;</span> <span class="token keyword">this</span> <span class="token punctuation">.</span> doors <span class="token operator">=</span> doors <span class="token punctuation">;</span> <span class="token keyword">this</span> <span class="token punctuation">.</span> isElectric <span class="token operator">=</span> isElectric <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">displayMake</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token template-string"><span class="token template-punctuation string">`</span> <span class="token string">This car is </span> <span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> <span class="token keyword">this</span> <span class="token punctuation">.</span> model <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token template-punctuation string">`</span></span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
In the above example, we have declared a Car class, along with some of its properties, that we are initializing in the constructor. We also have a method that will display some messages using its properties.
Let’s see how we can create a new version of this class:.
1 2 3 | <span class="token keyword">const</span> Prius <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Car</span> <span class="token punctuation">(</span> <span class="token string">'Prius'</span> <span class="token punctuation">,</span> <span class="token number">4</span> <span class="token punctuation">,</span> <span class="token boolean">true</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> Prius <span class="token punctuation">.</span> <span class="token function">displayMake</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// This car is Prius</span> |
If you have done with object oriented programming, you are too familiar with the steps above.
We take the keyword of new
and call the class’s constructor and pass it properties.
Now this Prius
object has its own feature of model
, doors
and isElectric
. The object can also call displayMake
method, which will have access to Prius’s properties.
6. Interface.
The concept of interfaces is another powerful feature of TypeScript that allows you to define the structure of the variables. Why is that a paradigm includes specific features that an object must adhere to.
Interfaces are best described through an actual example. So let’s say we have an object of Car:
1 2 3 4 5 6 | <span class="token keyword">const</span> Car <span class="token operator">=</span> <span class="token punctuation">{</span> model <span class="token operator">:</span> <span class="token string">'Prius'</span> <span class="token punctuation">,</span> make <span class="token operator">:</span> <span class="token string">'Toyota'</span> <span class="token punctuation">,</span> <span class="token function">display</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">'hi'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
If we look at the object above and try to extract its signature, it would be:
1 2 3 4 5 6 | <span class="token punctuation">{</span> model <span class="token operator">:</span> String <span class="token punctuation">,</span> make <span class="token operator">:</span> String <span class="token punctuation">,</span> <span class="token function">display</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">}</span> |
If we want to use this signature again, we can declare it as an interface. To create the interface, we use the interface
keyword.
1 2 3 4 5 6 7 8 9 10 11 12 | <span class="token keyword">interface</span> <span class="token class-name">ICar</span> <span class="token punctuation">{</span> model <span class="token operator">:</span> String <span class="token punctuation">,</span> make <span class="token operator">:</span> String <span class="token punctuation">,</span> <span class="token function">display</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">}</span> <span class="token keyword">const</span> Car <span class="token operator">:</span> ICar <span class="token operator">=</span> <span class="token punctuation">{</span> model <span class="token operator">:</span> <span class="token string">'Prius'</span> <span class="token punctuation">,</span> make <span class="token operator">:</span> <span class="token string">'Toyota'</span> <span class="token punctuation">,</span> <span class="token function">display</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">'hi'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
Here, we have declared an interface called ICar
and created a Car
object. Car
now binds to the ICar
interface, making sure that the Car
object identifies all the properties in the interface.
Conclusion
I hope this article gave you a glimpse of how TypeScript can make your JavaScript more stable and error-less.
TypeScript is a very hot keyword with development, I am also approaching it and find it very interesting, so there is something interesting that everyone can share with me. Thanks !!!!!
Reference link:
https://www.freecodecamp.org/news/learn-typescript-in-5-minutes-13eda868daeb/