TypeScript Installation
This article introduces the installation of the TypeScript environment.
We will use the npm tool for installation. If you are not familiar with npm, you can refer to our NPM Usage Introduction.
NPM Installation of TypeScript
If your local environment already has the npm tool installed, you can use the following commands to install.
Using a domestic mirror:
npm config set registry https://registry.npmmirror.com
Install typescript:
npm install -g typescript
After installation, we can use the tsc
command to execute TypeScript code. Here is how to check the version number:
$ tsc -v
Version 3.2.2
Next, we create a file named app.ts
with the following code:
var message: string = "Hello World";
console.log(message);
We usually use .ts
as the extension for TypeScript code files.
Then, execute the following command to convert TypeScript to JavaScript code:
tsc app.ts
At this point, a file named app.js
will be generated in the current directory (same directory as app.ts
), with the following code:
var message = "Hello World";
console.log(message);
Use the node command to execute the app.js
file:
$ node app.js
Hello World
The process of TypeScript converting to JavaScript is illustrated below:
Introduction to Visual Studio Code
Many IDEs have TypeScript plugins, such as: Visual Studio, Sublime Text 2, WebStorm / PHPStorm, Eclipse, etc.
This section mainly introduces Visual Studio Code, a cross-platform source code editor for writing modern web and cloud applications, developed by Microsoft, and can run on Mac OS X, Windows, and Linux.
Download link: https://code.visualstudio.com/.
Installing Visual Studio Code on Windows
Download Visual Studio Code.
Double-click the
VSCodeSetup.exe
icon.After installation, the Visual Studio Code interface will look similar to the following:
We can click the currently edited code file in the left window, select Open in Command Prompt (open in terminal), and then we can use the
tsc
command to execute TypeScript file code in the lower right part of the screen.
Installing Visual Studio Code on Mac OS X
For Mac OS X installation and configuration of Visual Studio Code, see: https://code.visualstudio.com/Docs/editor/setup.
Installing Visual Studio Code on Linux
For Linux installation and configuration of Visual Studio Code, see: https://code.visualstudio.com/Docs/editor/setup.