|
| 1 | +--- |
| 2 | +title: tsconfig.json 是什么 |
| 3 | +layout: docs |
| 4 | +permalink: /zh/docs/handbook/tsconfig-json.html |
| 5 | +oneline: 了解 TSConfig 的工作原理 |
| 6 | +translatable: true |
| 7 | +--- |
| 8 | + |
| 9 | +## 概览 |
| 10 | + |
| 11 | +当目录中出现了 `tsconfig.json` 文件,说明该目录是 Typescript 项目的根目录。`tsconfig.json` 文件指定了编译项目所需的根目录下的文件以及编译选项。 |
| 12 | + |
| 13 | +JavaScript 项目可以使用 `jsconfig.json` 文件代替,它的作用与 `tsconfig.json` 基本相同,只是默认启用了一些 JavaScript 相关的编译选项。 |
| 14 | + |
| 15 | +一个项目将以下列之一的方式编译: |
| 16 | + |
| 17 | +## 使用 `tsconfig.json` 或者 `jsconfig.json` |
| 18 | + |
| 19 | +- 调用 tsc 命令并且没有其它输入文件参数时,在这种情况下编译器会搜索 `tsconfig.json` 文件,从当前目录开始搜索,一直搜寻到父级目录。 |
| 20 | + |
| 21 | +- 调用 tsc 命令并且没有其他输入文件参数,使用 `--project` (或者只是 `-p`)的命令行选项来指定包含了 `tsconfig.json` 的目录,或者包含有效配置的 `.json` 文件路径。 |
| 22 | + |
| 23 | +当命令行中指定了输入文件参数, `tsconfig.json` 文件会被忽略。 |
| 24 | + |
| 25 | +## 示例 |
| 26 | + |
| 27 | +`tsconfig.json` 文件示例: |
| 28 | + |
| 29 | +- 使用 `files` 属性 |
| 30 | + |
| 31 | + ```json tsconfig |
| 32 | + { |
| 33 | + "compilerOptions": { |
| 34 | + "module": "commonjs", |
| 35 | + "noImplicitAny": true, |
| 36 | + "removeComments": true, |
| 37 | + "preserveConstEnums": true, |
| 38 | + "sourceMap": true |
| 39 | + }, |
| 40 | + "files": [ |
| 41 | + "core.ts", |
| 42 | + "sys.ts", |
| 43 | + "types.ts", |
| 44 | + "scanner.ts", |
| 45 | + "parser.ts", |
| 46 | + "utilities.ts", |
| 47 | + "binder.ts", |
| 48 | + "checker.ts", |
| 49 | + "emitter.ts", |
| 50 | + "program.ts", |
| 51 | + "commandLineParser.ts", |
| 52 | + "tsc.ts", |
| 53 | + "diagnosticInformationMap.generated.ts" |
| 54 | + ] |
| 55 | + } |
| 56 | + ``` |
| 57 | + |
| 58 | +- 使用 `"include"` 和 `"exclude"` 属性 |
| 59 | + |
| 60 | + ```json tsconfig |
| 61 | + { |
| 62 | + "compilerOptions": { |
| 63 | + "module": "system", |
| 64 | + "noImplicitAny": true, |
| 65 | + "removeComments": true, |
| 66 | + "preserveConstEnums": true, |
| 67 | + "outFile": "../../built/local/tsc.js", |
| 68 | + "sourceMap": true |
| 69 | + }, |
| 70 | + "include": ["src/**/*"], |
| 71 | + "exclude": ["node_modules", "**/*.spec.ts"] |
| 72 | + } |
| 73 | + ``` |
| 74 | + |
| 75 | +## TSConfig 基础 |
| 76 | + |
| 77 | +根据你想要执行代码的 JavaScript 运行时环境,可能有一个基本配置,你可以参考[github.com/tsconfig/bases](https://github.com/tsconfig/bases/)来使用。 |
| 78 | +这些你的项目扩展的 `tsconfig.json` 文件,它通过运行时的支持来简化你的 `tsconfig.json` 文件配置。 |
| 79 | + |
| 80 | +<!-- 这些是你的项目扩展的""文件,它通过处理运行时支持简化你的“” --> |
| 81 | +举个例子,如果你的项目是基于 Node.js 12.x 写的,那么你可以使用 npm 依赖:[`@tsconfig/node12`](https://www.npmjs.com/package/@tsconfig/node12): |
| 82 | + |
| 83 | +```json tsconfig |
| 84 | +{ |
| 85 | + "extends": "@tsconfig/node12/tsconfig.json", |
| 86 | + |
| 87 | + "compilerOptions": { |
| 88 | + "preserveConstEnums": true |
| 89 | + }, |
| 90 | + |
| 91 | + "include": ["src/**/*"], |
| 92 | + "exclude": ["node_modules", "**/*.spec.ts"] |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +这使你的 `tsconfig.json` 专注在你自己选择的项目环境上,而不是所有的运行时环境。现在已经有了一些 tsconfig 基础配置,我们希望社区能够为不同的环境添加更多的内容。 |
| 97 | + |
| 98 | +- [推荐配置](https://www.npmjs.com/package/@tsconfig/recommended) |
| 99 | +- [Node 10](https://www.npmjs.com/package/@tsconfig/node10) |
| 100 | +- [Node 12](https://www.npmjs.com/package/@tsconfig/node12) |
| 101 | +- [Node 14](https://www.npmjs.com/package/@tsconfig/node14) |
| 102 | +- [Deno](https://www.npmjs.com/package/@tsconfig/deno) |
| 103 | +- [React Native](https://www.npmjs.com/package/@tsconfig/react-native) |
| 104 | +- [Svelte](https://www.npmjs.com/package/@tsconfig/svelte) |
| 105 | + |
| 106 | +## 细节 |
| 107 | + |
| 108 | +当`"compilerOptions"` 属性忽略的时候,会使用编译器的默认配置。请参考我们支持的[编译器选项](/tsconfig)列表 |
| 109 | + |
| 110 | +## TSConfig 参考 |
| 111 | + |
| 112 | +想要了解更多的配置选项的信息,请访问 [TSConfig Reference](/tsconfig) |
| 113 | + |
| 114 | +## 模式 |
| 115 | + |
| 116 | +`tsconfig.json` 的模式可以在这里找到 [the JSON Schema Store](http://json.schemastore.org/tsconfig). |
0 commit comments