First of all, before we learn ts, we need to install ts
1. Install typescript
npm install -g typescript
//检查是否安装
tsc -v
2. Generate the configuration file, cd into the folder, and enter in the console
tsc --init
At this point we can see that a tsconfig.json file appears under the ts folder
3. Compile the ts file
(1) The first method can be compiled directly by command
For example, if we create a test.ts file, we can enter tsc + file name in the console to compile
tsc test.ts
Of course, this operation is more cumbersome, then consider the second way
(2) vscode automatically compiles ts files
Since browsers cannot recognize ts files, the compilation process is to compile ts files into js files .
How to set it?
The first and second steps are as follows
The third step is to click on the run task in the terminal
Look at the effect. At this time, we create a new demo.ts, and the console will automatically monitor and run
And the corresponding demo.js file is automatically generated in the js folder
At this time, we compared and achieved the expected goal.
Note: Of course, there is an easier way to run the task in the third step . You can run tsc -w directly in the ts directory to run all the ts files in the folder
How to run ts directly on the console and view output such as printing
1. Install the plugin Code Runner in vscode
2. Install the ts package
npm i typescript ts-node @types/node@* -g
3. Click the run button in the upper right corner of the file
At this point, you can start the learning path of ts~