一个最简单的typescript工程
初级:
1.新建一个文件夹~/a/
2.cd ~/a/
3.npm init -y 生成package.json
4.新建index.ts,内容:console.log("hello ts-world.");
5.在package.json里面添加依赖
npm install ts-node typescript --save-dev
npm install tslint --save-dev
npm install husky --save-dev
6.生成typescript需要的文件
tsc --init
tslint --init
7.测试:ts-node index.ts
进阶:
1.npm install commander --save
npm install colors --save
npm install @types/node --save
2.代码:
const { program } = require('commander'); program.version('0.0.1') .option('-d, --debug', 'output extra debugging') .option('-s, --small', 'small pizza size') .option('-p, --pizza-type <type>', 'flavour of pizza'); program.parse(process.argv);
3.测试:ts-node index.ts -h
4.代码:
import commander from "commander"; import colors from "colors"; const command = commander .option('-d, --debug', 'output extra debugging') .option('-s, --small', 'small pizza size') .option('-c, --city [name]', 'add city name'); command.parse(process.argv); if (process.argv.slice(2).length === 0) { command.outputHelp(colors.red); process.exit(); } console.log(command.city);
5.测试:ts-node index.ts -h
ts-node index.ts --city chengdu
ts-node index.ts