Environment: Mac OS

Prerequisites: Node and NPM have been installed

Development tool: VS Code

Installation and commissioning

Install TS and TS-Nodes globally

NPM install [email protected] -g NPM install [email protected] -gCopy the code

Record the path of the executable file after the TS-Node is installed.

Create a new directory tsdemo and create a 1.ts file in it. Write console.log(‘Hello, ts! ‘) and save.

Note to Windows users that you need to run some commands separately (not for Linux and macOS users)

npm init -y
npm i -D ts-node typescript
Copy the code

Create a.vscode directory under tsDemo and create a launch.json directory with the following contents:

 {
     "configurations": [{"name": "ts-node"."type": "node"."request": "launch"."program": "Note that this is written as the ts-node executable; ${workspaceRoot}/node_modules/ts-node/dist/bin.js"."args": ["${relativeFile}"]."cwd": "${workspaceRoot}"."protocol": "inspector"}}]Copy the code

Use VS Code to open 1.ts under tsDemo, find Debug,

If you see the result, the debugging is normal (note that the selected file is determined to be 1.ts).

Command line program

Create a new add.ts file under tsdemo

#! /usr/bin/env ts-node console.log('hello world')Copy the code

#! /usr/bin/env ts-node this line of comment code has a history, called shebang

Then add execute permission to the add.ts file (Windows users don’t need to do this, just type./1.ts in Git Bash) :

chmod +x ./1.ts
Copy the code

Run./1.ts and output ‘Hello world’.

Continue writing code in add.ts:

#! /usr/bin/env ts-node console.log('hello world') console.log(process.argv)Copy the code

Run again at this time, error:

/usr/local/lib/node_modules/ts-node/src/index.ts:261 return new TSError(diagnosticText, diagnosticCodes) ^ TSError: ⨯ Unable to compile TypeScript: 2.ts(2,13): error TS2304: Cannot find name 'process'. at createTSError (/usr/local/lib/node_modules/ts-node/src/index.ts:261:12) at getOutput (/usr/local/lib/node_modules/ts-node/src/index.ts:367:40) at Object.compile (/usr/local/lib/node_modules/ts-node/src/index.ts:557:11) at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:439:43) at Module._extensions.. js (module.js:663:10) at Object.require.extensions.(anonymous function) [as .ts] (/usr/local/lib/node_modules/ts-node/src/index.ts:442:12) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Function.Module.runMain (module.js:693:10)Copy the code

Process is a global variable in Node.js and is impossible to miss. The reason is that there is no type declaration.

This is the great thing about TS: IF you don’t tell me what process is, I won’t allow you to use process.

So how do you tell TS what process is?

Here’s how:

NPM install @types/node # Run again./add.ts >./add.ts ['node', '/Users/frank/TypeScript/tsdemo/2.ts' ]Copy the code

That will do.

Now it’s finally time to write normal code.

#! /usr/bin/env ts-node function add(a: number, b: number): number { return a + b } const a = parseInt(process.argv[2]) const b = parseInt(process.argv[3]) if (Number.isNaN(a) || Number.isnan (b)) {console.log(' parameter error ') process.exit(1)} console.log(add(a, B)) // 0 generally represents the end of the normal execution of the program // non-0 represents the error of the program // the so-called happy families are all alike, but each unhappy family has its own misfortune.Copy the code

Oh, not yet, the execution found that the error again,

/ Users/lijingwei/NVM/versions/node/v14.16.1 / lib/node_modules/ts - node/SRC/index, ts: 261 return new TSError(diagnosticText, diagnosticCodes) ^ TSError: ⨯ Unable to compile TypeScript: 1.ts(9,12): error TS2339: Property 'isNaN' does not exist on type 'constructor'.1.ts (9,31): error TS2339: Property 'isNaN' does not exist on type 'NumberConstructor'. at createTSError (/ Users/lijingwei/NVM/versions/node/v14.16.1 / lib/node_modules/ts - node/SRC/index, ts: 261:12) at getOutput (/ Users/lijingwei/NVM/versions/node/v14.16.1 / lib/node_modules/ts - node/SRC/index, ts: 367:40) running at Object.com (/ Users/lijingwei/NVM/versions/node/v14.16.1 / lib/node_modules/ts - node/SRC/index, ts: 557:11) at the Module. M. _compile (/ Users/lijingwei/NVM/versions/node/v14.16.1 / lib/node_modules/ts - node/SRC/index, ts: 439:43) at the Module. The _extensions.. js (internal/modules/cjs/loader.js:1092:10) at Object.require.extensions.<computed> [as .ts] (/ Users/lijingwei/NVM/versions/node/v14.16.1 / lib/node_modules/ts - node/SRC/index, ts: 442:12) at the Module. The load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) at Object.<anonymous> (/ Users/lijingwei/NVM/versions/node/v14.16.1 / lib/node_modules/ts - node/SRC/bin, ts: 147:12)Copy the code

The reason is that the code uses a new syntax called number. isNaN, which ts supports by default but must be configured to use.

This is done by creating a new tsconfig.json file in the tsdemo directory:

{
  "compilerOptions": {
    "lib": ["ES2015"]
  }
}
Copy the code

In this case, run./add.ts 1 2.