playbook/docs/typescript/configuration.md

1.6 KiB
Raw Permalink Blame History

TypeScript 配置清单

本文件汇总 TypeScript 项目常用配置文件说明。

1) tsconfig.json

关键编译选项:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,
    "exactOptionalPropertyTypes": true,
    "skipLibCheck": true,
    "outDir": "dist",
    "rootDir": "src"
  },
  "include": ["src"]
}
  • strict: true:启用全部严格检查(必须)
  • noUncheckedIndexedAccess:数组/对象索引访问返回 T | undefined(推荐)
  • skipLibCheck:跳过 .d.ts 检查,加快编译

2) .prettierrc

{
  "semi": false,
  "singleQuote": true,
  "printWidth": 100,
  "trailingComma": "all",
  "tabWidth": 2
}

3) eslint.config.jsFlat Config

import tseslint from 'typescript-eslint'

export default tseslint.config(
  ...tseslint.configs.recommendedTypeChecked,
  {
    languageOptions: {
      parserOptions: { projectService: true },
    },
    rules: {
      '@typescript-eslint/no-explicit-any': 'error',
      '@typescript-eslint/no-floating-promises': 'error',
    },
  },
)

4) .editorconfig

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.{ts,tsx,js,jsx,json}]
indent_style = space
indent_size = 2
max_line_length = 100

5) .vscode/settings.json

{
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.rulers": [100],
  "typescript.tsdk": "node_modules/typescript/lib"
}