1. Via npm (Node.js Package Manager) command-line tool
If you use Visual Studio or VS Code IDE, simplest way for you to add to Visual Studio or VS Code is to search and add package or download from the TypeScript website.
You can also download TypeScript Tools for Visual Studio. If you search in Google, you will find the latest version of the download. Here is one download for VS 2017.
I have installed it via Visual Studio. I installed it on VS 2012. For you, this will look different if you're installing the latest version of TS for newer VS.
After download, right-click and select "Run as Administrator".
After that, click Next; and finally it installs.
How it works
TypeScript is modern JavaScript. In JavaScript, we declare any variable like the following.
but in TypeScript, the declaration is done like the following.
Syntax
AccessModifier Property/Variable Name Colon(:) dataType
For example:
Public StudentName : string = "";
Let's create a project and understand this by building a simple application.
Select Project >> Asp.Net Web Application and write application name.
Select the Empty template and click OK.
After this, we will add a new TypeScript file, as shown in the below image.
After clicking on the TypeScript file, give TypeScript file a name, such as Student.
The following dialog box will open. It means our application is not fully configured for supporting TypeScript. So, for full support, click on Yes.
Now, our application has configured and had full support for TypeScript. In the Solution Explorer, you can see that student.ts file is added. One more thing; when we create JavaScript file, its extension is
.js but in TypeScript file, the extension is
.ts.
And you can see in my application now that one file is available under the name Student.ts
I have created a Student class and added StudentName property. After creating this property, when we save this file (Student.ts), the JS file with the same class name is generated.
TypeScript file (Student.ts) code is as follows.
- class Student {
- public StudentName: string = " ";
- }
After clicking "Show All files", we can see the screen like this.
Now, let's see the JavaScript code which is auto-generated after saving the TypeScript file.
- var Student = (function() {
- function Student() {
- this.StudentName = "";
- }
- return Student;
- }());
For using this code, let's create a new web page named Student.aspx.
Click New Item >> web form, give a suitable name and click Add.
Now, the Web Form is created. Add reference to Sudent.js file in Web Form and use it.
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Student.aspx.cs" Inherits="LearnTypeScript.Student" %>
- <!DOCTYPE html>
- <html
- xmlns="https://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <script src="Student.js"></script>
- <body>
- <script>
- var stud = new Student();
- stud.StundentName = "Amit";
- alert(stud.StundentName);
- </script>
- </body>
- </html>
Code Explanation
We have added a reference to the Student.js file and created an object of the Student class and set the property name. Then, we have shown the alert message.
Result
Now, I want to change something in my code. I have created a function in the student.ts file and called it from the student.aspx page.
- class Student {
- public StudentName: string = "";
- validate(): boolean {
- alert("Amit Text function");
- return;
- };
- }
And used it from Student.aspx file.
In this article, we learned about TypeScript and why it was developed. We also learned the benefits of TypeScript.