How Angular: Typescript useful for programming
Why TypeScript is Useful
TypeScript is an open source programming language which gives an advanced level of auto-completion, data navigation, Data Integrity, and data refactoring. Having such tools is almost a requirement for large projects.
Setting up of TypeScript
Typescript is an Open Source technology. This Angular language can be run by any platform and any operating system. Opposite, you have to follow the tools to write and test a Typescript program −
A Text Editor
The text editor helps you to write your source code. Examples of a few editors include Windows Notepad, Notepad++, Emacs, vim or vi, etc. Editors used may vary with Operating Systems.
The source file name of TypeScript is written in .ts extension.
The TypeScript Compiler
Installation of Windows
Furthermore, follow the steps given below to install Node.js in Windows environment.
First of all, download setup, In addition, run the .msi installer for Node.
If the installation goes successfully due to verification, enter the command node –v in the terminal window.
Finally, The next command in the terminal window installs programs.
Your First TypeScript Code
Let’s start with a quick example of “Hello World” because you can have a better idea to get started another.
Line 1 defines a variable by the name of the message. Data variable mixed up with in-store data values in a program.
Line 2 prints the variable’s value to the prompt. Since it refers to the terminal window. The element log (), which displays text on the final screen.
Compile and Execute a TypeScript Program
Let’s find out how to execute and compile a TypeScript program due to code in Visual Studio. Follow the steps given below −
Step 1 − Save the file with the .ts extension. Noteworthy save the file as Test.ts. The code editor marks errors in the code, if any, while you save it.
Step 2 − Press on button Right-click, then click the TypeScript file under the Working Files option in VS Code’s Explore Pane window. Then, select Open in Command Prompt option.
Step 3 − To compile the file using the following command on the terminal window.
Step 4 − Finally, The file compiles data to Test.js. To get an output of your written program, you must type the following in the terminal.
TypeScript and Object Orientation
Object − A single object is a real-time presence of any other entity. According to Grady Brooch, there every object must have three features −
State:- It describes the attributes of a particular object
Behavior:- Describes how the object will act
Identity:- A unique value that describes an object from a set of similar such objects.
Class:- OOP classes define the blueprint for creating objects. A class encapsulates data for the object.
Method:- Methods facilitate communication between objects.
Example:- TypeScript and Object Orientation
The above example defines a class greeting. The class has a method greet (). This method prints the value string for “Hello World” on the final screen. The new keyword creates a class object (obj). Much as invoked object gives the method greet ().
Output: Finally, the output will look like as given below,
Variable Declaration in TypeScript
When you declare a single variable, you have four options −
Declare its major type and its value in the single statement.
Declare its type but no value. In this paradigm, the variable set to a value to undefined.
Declare its value but no type. The variable type set to any value pointer.
Declare neither value not type. As a result, the data type of the variable initialized to undefined.
Advanced static Types in TypeScript
TypeScript string values: This static type the –strictNullChecks option gives and compiler command and it also involves how non-nullable types differ from nullable types. Finally, It gives illustration how you can write safety code by being explicit about null and undefined in the type system.