Exploring Typescript Shortly
Mahin Tazuar
Posted on November 12, 2022
Introducing:
Typescript Developed by Microsoft. Typescript is a Superset of Javascript. Some Top Purpose of typescript is, When we build Enterprise or Large Scale projects, we face data type-checking related issues. Suppose we expect a number type data from a variable but if we don't get it, that can be a big issue. So Typescript Smart Intelligent System Help to handle all data type of this project for Strickly Specify them. Typescript gives a Compiler error. That means when we are writing code if the code was wrong typescript gives an error this time. Code not rendering before the solution. After Compile The typescript is given a vanilla javascript Code. That's why it Helps to Support an old browser. We also configure more and select the javascript version.
Installation :
Requierd Node js
npm i -g typescript
npm i typescript
Variable and Function Type checking :
Explicity variable with Array
Explicit Means, when we define a variable with the let keyword but did assign a value. This time we can change the value any more and also we can define any type of data type. Typescript Explicity helps to define a variable with a fixed expected data type.
Union Type
Union Type Means We can fix accepted data type. Like a variable value should be a number or string. If we see the below image we can be clear about what is union type.
Dynamic Type Define
Simply when we don't know which data type set in the future into the variable. This time we can use any type which provides typescript. that helps to set any type of data like the below image.
Function Type
Typescript's beautiful feature is function type handling. Using typescript we can define Function type when variable initialization and we can define function parameter type also function return type. Typescripts have partial power to determine automatically which type should be returned.
Function Signatures
We can fixed data type where variable define.
Type Aliases
Suppose if we need to use multiple types explicitly, we can use them as a variable. So that we can use it multiple times.
Interface
Interface is an other most interesting typescript feature. when we are need to check multiple variable data type, we need to write typescript multiple time. But typescript interface helps for that. Typescript give interface feature that's why we do not write same type checking code in multiple time. Below the picture is example. And we can make it reusable inside all file using import/export.
Generics Types
Sometimes we do not know which type of data comes to a variable. So it creates complexity in checking data type. That's why typescript gives another feature called Generics Type checking. It helps to check dynamic data like the below image and it also works well with the interface.
Enum Type
Enum Type is collection of a index. we can predefine index name for easily implementation.
Tuples
After defining an array within a variable, we can change a value using this array index. But we can not add or modify new data types which do not exist in this array. Because of typescripts have guess capability. Typescript Auotmetically guesses the data type, then strictly forces to use this data type.
Now if we need to fix the data type using by array index, i mean we can update or modify data following this array's existing index data type. Like the below image.
Working with class
Typescript works well with the class or javascript object-oriented programming. When we are using class then we can define the class constructor data type.
Access Modifers in Class
When we create an object using a class, this object property accessibility is open. We know javascript give some modifier but typescript helps it when the type is defined we can also define the public/private access modifier. Typescript introduces a new modifier which is called read-only.
Posted on November 12, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
July 20, 2023