TypeScript Config 配置 Schema 通读,防忘

232 阅读2分钟

前言

每次新开项目的时候配置 TS 都会多少忘记,于是花了大概半天的时间通读并大概总结个人的最佳实践。

代码

{
  "compilerOptions": {
    // 设置产物的模块化规范
    "target": "ES6",
    // 设置源代码的模块化规范
    "module": "ESNext",
    // 设置源文件根目录,产物中的目录会以其为根目录生成目录(打包输出产物的时候)
    // "rootDir": "./src",
    // 设置 TS 通过什么规范来引入文件,module 为 UMD 的时候不能设置
    "moduleResolution": "Bundler",
    // 设置源文件根目录,import 的时候可以直接从这个目录下的一个文件起头,比如
    // ./src/dir1/dir1.1 以及 ./src/dir2/dir2.1,dir2.1/index.ts 中引入 dir1.1 的 module.ts,只需要 import *** from "src/dir2/dir2.1"
    "baseUrl": ".",
    // 配置路由别名,但注意如果是目录下所有文件,必须加上*,否则只会特指单个文件,
    // 以及会配合 baseUrl 使用,也就是路由会直接从 baseUrl 开始算起
    "paths": {
      "@/*": ["./src/*"]
    },
    // 允许 import ts文件,比如 .ts .mts. .tsx,但是需要结合 noEmit | emitDeclarationOnly
    // "allowImportingTsExtensions": true,
    // 允许 import JSON 文件作为对象
    // "resolveJsonModule": true,

    // 是否输出编译产物
    "declaration": true,
    // "declarationMap": true,
    // 只输出 .d.ts 文件
    // "emitDeclarationOnly": true,
    // 只支持 amd,system 模块
    // "outFile": "amd",
    "outDir": "./dist",
    // 不输出注释
    "removeComments": true,
    // 不输出产物
    // "noEmit": true,
    // 添加辅助函数
    // "importHelpers": true,
    // 将迭代降级处理
    // "downlevelIteration": true,
    // 换新行时使用的格式,lf 是 unix 格式
    "newLine": "lf",
    // 防止一些被认为未使用(实际上使用了)的引入被移除掉,将要停止维护,使用 verbatimModuleSyntax 代替之
    // "preserveValueImports": true,

    /* Interop Constraints */
    // 每个文件必须是模块
    "isolatedModules": true,
    // 不会移除掉未被使用的 exports, imports
    "verbatimModuleSyntax": true,
    // 允许 import fs from 'fs' 而非 import * as fs from 'fs'
    "allowSyntheticDefaultImports": true,
    // 修复 ES6 中引入 CJS/AMD/UMD 的错误
    // 1. 解决 import * as fs from 'fs' 的问题
    // 2. 使用新的工具函数处理,保证适配性
    "esModuleInterop": true,
    // "preserveSymlinks": true,
    "forceConsistentCasingInFileNames": true,

    /* Type Checking */
    "strict": true,
    // 不允许出现隐式 any
    "noImplicitAny": true,
    // 严格检查 null undefined 的情况
    "strictNullChecks": true,
    // "strictFunctionTypes": true,
    // "strictBindCallApply": true,
    // "strictPropertyInitialization": true,
    // "noImplicitThis": true,
    // 在 catch 包中使用 unknown 而非 any 表示类型 
    "useUnknownInCatchVariables": true,
    // 总是严格模式
    "alwaysStrict": true,
    // 未使用的局部变量会报错
    "noUnusedLocals": true,
    // 未使用的函数传参会报错
    "noUnusedParameters": true,
    // 更严格的可选 schema,开启之后无法传 undefined,要考虑创建一个更语义化的类型代替 undefined
    "exactOptionalPropertyTypes": true,
    // 确保 function 中每个分支都会返回一个值
    "noImplicitReturns": true,
    // 不允许出现空选择,switch case,也就是不能出现 fallthrough
    // "noFallthroughCasesInSwitch": true,
    // 对象中没有明确定义 schema 的值会加上 undefined
    "noUncheckedIndexedAccess": true,
    // 存在 override 的时候发出提醒
    "noImplicitOverride": true,
    // 在 schema 中如果定义了 [key: string]: T,使用未明确定义的 schema 的时候(比如 username),只能使用 obj['username'],不能使用 obj.username
    "noPropertyAccessFromIndexSignature": true,
    // 允许不可达代码的出现,让其不会置灰
    "allowUnreachableCode": true,

    /* Completeness */
    // 跳过被依赖的 .d.ts 的检查
    // "skipDefaultLibCheck": true,
    // 跳过所有 .d.ts 的检查
    // "skipLibCheck": true
  }
}