介绍
gts 是来自谷歌(Google)团队开源的 TypeScript 编码格式风格、代码格式化、代码自动修复工具,集成了eslint、prettier,无需任何配置,开箱即用。
如何使用
在你的项目内使用 npx gts init
命令,它将自动为你的项目进行配置:
- 在你的
package.json
文件中添加必要的依赖及脚本命令:"scripts": { // 执行测试 "test": "echo \"Error: no test specified\" && exit 1", // 代码检查 "lint": "gts lint", // 清空编译后的产物 "clean": "gts clean", // 编译你的项目 "compile": "tsc", // 修复你的项目代码 "fix": "gts fix", // 以下 npm 钩子事件,如果不需要可以进行删除 "prepare": "npm.cmd run compile", "pretest": "npm.cmd run compile", "posttest": "npm.cmd run lint" }, "devDependencies": { "gts": "^3.0.3", "typescript": "^4.0.3", "@types/node": "^14.11.2" }
- 添加一个继承Google TypeScript Style 的
tsconfig.json
文件:{ "extends": "./node_modules/gts/tsconfig-google.json", "compilerOptions": { "rootDir": ".", "outDir": "build" }, "include": [ "src/**/*.ts", "test/**/*.ts" ] }
- 添加一个
.prettierrc.js
文件、.eslintrc.json
文件,以及.eslintignore
文件:// .prettierrc.js 文件 module.exports = { ...require('gts/.prettierrc.json') }
// .eslintrc.json 文件 { "extends": "./node_modules/gts/" }
// .eslintignore 文件 build/