setup-better-typescript

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Set Up Better TypeScript

搭建Better TypeScript

Set up
@better-typescript/better-typescript
in the package from which the linter will run.
在要运行代码检查工具的包中设置
@better-typescript/better-typescript

Procedure

步骤

  1. Detect npm or Bun from
    packageManager
    and the lockfile, then use that package manager so it updates the existing lockfile.
  2. 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
      package.json
      and
      tsconfig.json
      , because
      better-typescript
      loads
      ./tsconfig.json
      from its working directory.
  3. Add the dev dependency in the chosen package:
    npm
    sh
    npm install --save-dev @better-typescript/better-typescript
    Bun
    sh
    bun add --dev @better-typescript/better-typescript
    Run the command in the chosen directory.
  4. Add this exact script to the chosen
    package.json
    , preserving all other fields and scripts:
    json
    "lint:bts": "better-typescript"
  5. Configure the linter only when the user requests file or rule selection. Create
    better-typescript.json
    beside the chosen
    tsconfig.json
    . All rules already run by default, so do not create an empty or no-op config.
    json
    {
      "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.
    add_inclusions
    replaces the active rules for matching files, and
    add_exclusions
    removes rules. Globs are relative to the chosen directory.
  6. Run the new script from the chosen directory:
    npm
    sh
    npm run lint:bts
    Bun
    sh
    bun run lint:bts
    Confirm that the command analyzes the intended project. A completed analysis exits successfully even when it reports violations; do not fix those violations unless asked.
  1. packageManager
    和锁文件检测当前使用的是npm还是Bun,然后使用对应的包管理器,以确保更新现有的锁文件。
  2. 选择要搭建的包:
    • 在单包仓库中,使用仓库根目录。
    • 在多仓项目(monorepo)中,如果在根目录运行代码检查工具可以分析所有目标代码,则使用根目录。否则,使用每个指定的TypeScript工作区。仅当范围不明确时才询问用户。
    • 所选目录必须同时包含
      package.json
      tsconfig.json
      ,因为
      better-typescript
      会从其工作目录加载
      ./tsconfig.json
  3. 在所选包中添加开发依赖:
    npm
    sh
    npm install --save-dev @better-typescript/better-typescript
    Bun
    sh
    bun add --dev @better-typescript/better-typescript
    在所选目录中运行上述命令。
  4. 将以下脚本精确添加到所选的
    package.json
    中,保留所有其他字段和现有脚本:
    json
    "lint:bts": "better-typescript"
  5. 仅当用户要求选择文件或规则时才配置代码检查工具。在所选的
    tsconfig.json
    旁边创建
    better-typescript.json
    。所有规则默认已启用,因此不要创建空配置或无操作配置。
    json
    {
      "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
    会移除指定规则。通配符路径相对于所选目录。
  6. 在所选目录中运行新脚本:
    npm
    sh
    npm run lint:bts
    Bun
    sh
    bun run lint:bts
    确认命令分析的是目标项目。即使报告了代码违规,完成分析后命令仍会成功退出;除非用户要求,否则不要修复这些违规问题。