一个最简单的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 

 

posted on   拥剑公子  阅读(342)  评论(0编辑  收藏  举报

编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示