Typescript 开发环境的最佳实践

Posted cylee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Typescript 开发环境的最佳实践相关的知识,希望对你有一定的参考价值。

Typescript 开发环境的最佳实践

0?? git init(略)

1???? 初始化:$ yarn add -D ts-node typescript

2?? 生成 tsconfig.json:$ yarn tsc -init

3?? 配置 TSLint:$ yarn add tslint -D

4?? 生成 tslint.json:$ yarn tslint --init

5?? 创建 src/index.ts:$ mkdir src && echo "console.log(‘Hello Typescript‘)" > src/index.ts

6?? 执行 .ts 文件:$ yarn ts-node src/index.ts

7?? 安装 husky(没错,就是二哈??,哈士奇??): $ yarn add husky -D

8?? 打开 package.json,添加 husky 的 hook: 每次提交代码前,都会执行一次TSLint的检查命令。

{
    "husky": {
        "pre-commit": "yarn tslint -c tslint.json ‘./**/*.ts‘"
    }
}

9?? 安装命令行交互神器 commander.js: $ yarn add commander

const commander = require(‘commander‘);
const pkg = require(‘../package.json‘);

commander
  .version(pkg.version)
  .description(pkg.description)
  .usage(‘[options] <command> [...]‘)
  .option(‘-c, --city [name]‘, ‘Add city name‘)
  .parse(process.argv);

if (process.argv.slice(2).length === 0) {
    commander.help();
    process.exit()
}

1??0?? 测试命令: $ yarn ts-node src/index.ts -h

$ yarn ts-node src/index.ts -h
yarn run v1.10.1
$ C:\\Users\\Lee\\Desktop\\ts-weather\\node_modules\\.bin\\ts-node src/index.ts -h
Usage: index.ts [options] <command> [...]

Options:
  -V, --version  output the version number
  -c, --city     Add city name
  -h, --help     output usage information
Done in 1.91s.

1??1?? 获取命令行输入:$ yarn ts-node src/index.ts --city dongguan

console.log(commander.city) // => dongguan

1??2?? 添加命令行颜色$ yarn add colors

const colors = require(‘colors‘);
const commander = require(‘commander‘);
const pkg = require(‘../package.json‘);

commander
  .version(pkg.version)
  .description(pkg.description)
  .usage(‘[options] <command> [...]‘)
  .option(‘-c, --city [name]‘, ‘Add city name‘)
  .parse(process.argv);

if (process.argv.slice(2).length === 0) {
    commander.outputHelp(colors.red);
    process.exit()
}
?

以上是关于Typescript 开发环境的最佳实践的主要内容,如果未能解决你的问题,请参考以下文章

第1908期使用TypeScript开发Web应用的最佳实践

Vuejs301- Vue 3.0前的 TypeScript 最佳入门实践

19 条 Node.js 生产环境中的最佳实践

Angular + TypeScript 是否为最佳实践?

最佳实践丨云开发CloudBase多环境管理实践

聊聊TypeScript类型声明那些最佳实践