setup-better-typescript
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSet Up Better TypeScript
搭建Better TypeScript
Set up in the package from which the linter will run.
@better-typescript/better-typescript在要运行代码检查工具的包中设置。
@better-typescript/better-typescriptProcedure
步骤
-
Detect npm or Bun fromand the lockfile, then use that package manager so it updates the existing lockfile.
packageManager -
Choose the setup package:
- In a single-package repository, use the repository root.
- In a monorepo, use the root when running there analyzes all intended code. Otherwise use each requested TypeScript workspace. Ask about scope only when it is unclear.
- The chosen directory must contain both and
package.json, becausetsconfig.jsonloadsbetter-typescriptfrom its working directory../tsconfig.json
-
Add the dev dependency in the chosen package:npmsh
npm install --save-dev @better-typescript/better-typescriptBunshbun add --dev @better-typescript/better-typescriptRun the command in the chosen directory. -
Add this exact script to the chosen, preserving all other fields and scripts:
package.jsonjson"lint:bts": "better-typescript" -
Configure the linter only when the user requests file or rule selection. Createbeside the chosen
better-typescript.json. All rules already run by default, so do not create an empty or no-op config.tsconfig.jsonjson{ "commands": [ { "type": "add_inclusions", "files": "src/**/*.ts", "rules": ["no-throw", "no-error-type"] }, { "type": "add_exclusions", "files": "src/**/*.test.ts", "rules": "no-throw" } ] }Use only the file globs and rule names the user wants. Commands apply in order.replaces the active rules for matching files, andadd_inclusionsremoves rules. Globs are relative to the chosen directory.add_exclusions -
Run the new script from the chosen directory:npmsh
npm run lint:btsBunshbun run lint:btsConfirm that the command analyzes the intended project. A completed analysis exits successfully even when it reports violations; do not fix those violations unless asked.
-
从和锁文件检测当前使用的是npm还是Bun,然后使用对应的包管理器,以确保更新现有的锁文件。
packageManager -
选择要搭建的包:
- 在单包仓库中,使用仓库根目录。
- 在多仓项目(monorepo)中,如果在根目录运行代码检查工具可以分析所有目标代码,则使用根目录。否则,使用每个指定的TypeScript工作区。仅当范围不明确时才询问用户。
- 所选目录必须同时包含和
package.json,因为tsconfig.json会从其工作目录加载better-typescript。./tsconfig.json
-
在所选包中添加开发依赖:npmsh
npm install --save-dev @better-typescript/better-typescriptBunshbun add --dev @better-typescript/better-typescript在所选目录中运行上述命令。 -
将以下脚本精确添加到所选的中,保留所有其他字段和现有脚本:
package.jsonjson"lint:bts": "better-typescript" -
仅当用户要求选择文件或规则时才配置代码检查工具。在所选的旁边创建
tsconfig.json。所有规则默认已启用,因此不要创建空配置或无操作配置。better-typescript.jsonjson{ "commands": [ { "type": "add_inclusions", "files": "src/**/*.ts", "rules": ["no-throw", "no-error-type"] }, { "type": "add_exclusions", "files": "src/**/*.test.ts", "rules": "no-throw" } ] }仅使用用户指定的文件通配符和规则名称。命令按顺序执行。会替换匹配文件的生效规则,add_inclusions会移除指定规则。通配符路径相对于所选目录。add_exclusions -
在所选目录中运行新脚本:npmsh
npm run lint:btsBunshbun run lint:bts确认命令分析的是目标项目。即使报告了代码违规,完成分析后命令仍会成功退出;除非用户要求,否则不要修复这些违规问题。