Author: The official website of the College of Programmers: www.chengxuyuan.com

What is typescript

TypeScript is a superset of JavaScript that supports the ECMAScript 6 standard. TypeScript a free and open source programming language developed by Microsoft. TypeScript is designed for large applications that can be compiled into pure JavaScript that runs in any browser.

2. Differences between JavaScript and TypeScript

TypeScript is a superset of JavaScript that extends JavaScript’s syntax so that existing JavaScript code works with TypeScript without modification, and TypeScript provides compile-time static type checking through type annotations. TypeScript processes existing JavaScript code and only compiles TypeScript code within it.

Install typescript

We need to install TypeScript using the NPM tool. If you already have the NPM tool installed in your local environment, you can use the following command to install it:

npm install -g typescript
Copy the code

Because the above installation method is limited by the network, so we can use Taobao image for installation

npm config set registry https://registry.npm.taobao.org
npm config set disturl https://npm.taobao.org/dist
npm -g install typescript
Copy the code

If you need to specify the typescript version, use the @ symbol, for example:

NPM - g install ([email protected]) (mailto: [email protected])Copy the code

After installation, we can use TSC to execute TypeScript code. Here’s the version number:

$Tsc-v Version 4.0.2Copy the code

4. Install Visual Studio Code

Many ides support TypeScript plugins such as Visual Studio, Sublime Text 2, WebStorm/PHPStorm, Eclipse, etc. Here we write TypeScript in Visual Studio Code. Visual Studio Code is a cross-platform source Code editor for writing modern Web and cloud applications that runs on Mac OS X, Windows and Linux, developed by Microsoft. Download: code.visualstudio.com/

Install Visual Studio Code on Windows

1. Download Visual Studio Code.

2. Double-click vscodesetup. exe to install.

3. After installation, open Visual Studio Code interface similar to the following:

Build your first TypeScript file

(1) Create a TypeScript file greeter.ts for the first page and type the following code into the file:

function greeter(person: string) {
 return "Hello, " + person;
}
let user = "Jane User";
document.body.innerHTML = greeter(user);
Copy the code

(2) Then execute the following command to convert TypeScript to JavaScript code. In the left window, click on the currently edited code file and select Open in Command Prompt. At this point we can use the TSC command in the bottom right of the screen to execute the TypeScript file code.

tsc greeter.ts
Copy the code

The output is a greeter.js file. Let’s open greeter.js to see what the compiled code looks like:

function greeter(person) {
 return "Hello, " + person;
}
var user = "Jane User";
document.body.innerHTML = greeter(user);
Copy the code

(3) If there is an error during operation:

Unable to load file C: Users\Administrator\AppData\Roaming\ NPM \ Tsc.ps1 because scripts are not allowed to run on this system

Solution: Run powerShell as an administrator (always open it as a pipeman):

Copy the following command:

set-ExecutionPolicy RemoteSigned
Copy the code

Type A or Y to solve the problem

Ok, did you learn it?