@21kb/tsconfig 中文文档教程

发布于 3年前 浏览 31 项目主页 更新于 3年前

tsconfig-header

自以为是,但完全可扩展的 TypeScript config

Install

npm install --save-dev @21kb/tsconfig

Usage

Extending the config

{
  "extends": "@21kb/tsconfig/tsconfig.json",
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "dist"
  }
}

Overriding existing options

{
  "extends": "@21kb/tsconfig/tsconfig.json",
  "compilerOptions": {
    "outDir": "dist,
    // Emit `.jsx` files with the JSX unchanged
+    "jsx": "preserve",
  }
}

target设置为ESNext包括lib 来自 ESNext。 这些 API 是 被认为是不稳定的,并且随着 JavaScript 规范的发展而改变。 如果你 更喜欢使用另一个版本, 检查相关的ECMAScript 您希望升级到的版本,并将其设置为 target

{
  "extends": "@21kb/tsconfig/tsconfig.json",
  "compilerOptions": {
    "outDir": "dist",
    "target": "ES2021"
  }
}

您可能还想研究调整 lib 以匹配 target,或者 完全删除 lib,让 TypeScript 处理它。

Why?

Why are type-checking options like allowUnreachableCode (no-unreachable), noImplicitReturns (consistent-return), noUnusedLocals (no-unused-labels), and noUnusedParameters (no-unused-vars) not enabled?

大多数(如果不是全部)TypeScript 类型检查 ESLint 中已经存在这些特性。 使用 ESLint 捕获这些错误 (警告)代替。

Why is jsx set?

虽然可以说并非每个 TypeScript 项目都是 React 项目,每个 React 项目应该是一个 TypeScript 项目*。 设置这个 选项只影响 .jsx 文件中 JS 的输出。 如果你没有使用 React 在您的项目中,那么此设置根本不会影响您。

* 这是一个意见,而不是事实。 根据我的经验,TypeScript 有很大帮助 在由单个工程师或一个团队从事的中小型项目中 小型工程师团队。 另一方面,许多人认为 TypeScipt 的回报递减

Why is outDir not set?

请参阅 TypeScript#29172

tsconfig-header

Opinionated, yet fully extensible TypeScript config

Install

npm install --save-dev @21kb/tsconfig

Usage

Extending the config

{
  "extends": "@21kb/tsconfig/tsconfig.json",
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "dist"
  }
}

Overriding existing options

{
  "extends": "@21kb/tsconfig/tsconfig.json",
  "compilerOptions": {
    "outDir": "dist,
    // Emit `.jsx` files with the JSX unchanged
+    "jsx": "preserve",
  }
}

Setting the target to ESNext includes lib from ESNext. These APIs are considered unstable, and change as the JavaScript specification evolves. If you prefer to use another version, check the relevant ECMAScript version you wish to upgrade to, and set that as the target:

{
  "extends": "@21kb/tsconfig/tsconfig.json",
  "compilerOptions": {
    "outDir": "dist",
    "target": "ES2021"
  }
}

You might also want to look into tweaking lib to match target, or remove lib altogether, and let TypeScript handle it.

Why?

Why are type-checking options like allowUnreachableCode (no-unreachable), noImplicitReturns (consistent-return), noUnusedLocals (no-unused-labels), and noUnusedParameters (no-unused-vars) not enabled?

Most (if not all) TypeScript type-checking features are already present in ESLint. Use ESLint to catch these errors (warnings) instead.

Why is jsx set?

While the argument could be made that not every TypeScript project is a React project, every React project should be a TypeScript project*. Setting this option only affects the output of JS in .jsx files. If you are not using React in your project, then this setting should not affect you at all.

* This is an opinion, not a fact. In my experience, TypeScript helps greatly in small to mid-size projects being worked on by an individual engineer, or a small team of engineers. On the other hand, many argue that TypeScipt has deminishing returns

Why is outDir not set?

See TypeScript#29172.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文