TypeScript Helloworld

版权声明:原创文章,转载请保留此声明,谢绝商业用途Copy。 https://blog.csdn.net/prufeng/article/details/82193728

TypeScript配置入门实例。
https://github.com/prufeng/hellowork-js/tree/master/typescript

1. Install TypeScript

npm install -g typescript

tsc --version
tsc --help

2. Helloworld

2.1 New test.ts

class Startup {
    public static main(): number {
        let date = new Date();
        console.log('Hello World at ' + date.toLocaleString());
        return 0;
    }
}

Startup.main();

2.2 Test in Command Line

tsc test.ts
node test.js

Test done, console output: Hello World

3. TypeScript Project

3.1 tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true
  }
}

Parameters’ options please refer to:
https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
https://www.typescriptlang.org/docs/handbook/compiler-options.html

3.2 Build project

tsc -p .

The test.ts has been compiled to dist/out-tsc/

test.js   
test.js.map 

4. Work with VSCode

4.1 Run Build Task(Ctrl + Shift + B)

Select tsc:build-tsconfig.json, compile the project.
Select tsc:watch-tsconfig.json, will start a watch job in terminal to compile the project files automately when they are modified.

4.2 Debug(F5)

Set main module or start scripts in package.json, then can run with F5 in VS Code.
e.g.

"main": "test.js"

More details please refer to:
https://code.visualstudio.com/Docs/languages/typescript

目录:

猜你喜欢

转载自blog.csdn.net/prufeng/article/details/82193728