|
| 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 文件的目录。 |
| 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 | +你可以通过扩展这些已经处理过不同的 JavaScript 运行时环境的 `tsconfig.json` 文件来简化你项目中的 `tsconfig.json`。 |
| 79 | + |
| 80 | +举个例子,如果你的项目是基于 Node.js 12.x 写的,那么你可以使用 npm 模块:[`@tsconfig/node12`](https://www.npmjs.com/package/@tsconfig/node12): |
| 81 | + |
| 82 | +```json tsconfig |
| 83 | +{ |
| 84 | + "extends": "@tsconfig/node12/tsconfig.json", |
| 85 | + |
| 86 | + "compilerOptions": { |
| 87 | + "preserveConstEnums": true |
| 88 | + }, |
| 89 | + |
| 90 | + "include": ["src/**/*"], |
| 91 | + "exclude": ["node_modules", "**/*.spec.ts"] |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +这使你的 `tsconfig.json` 专注在你的项目的目标环境上,而不是所有可能的运行时环境。现在已经有了一些 tsconfig 基础配置,我们希望社区能够为不同的环境添加更多的内容。 |
| 96 | + |
| 97 | +- [推荐配置](https://www.npmjs.com/package/@tsconfig/recommended) |
| 98 | +- [Node 10](https://www.npmjs.com/package/@tsconfig/node10) |
| 99 | +- [Node 12](https://www.npmjs.com/package/@tsconfig/node12) |
| 100 | +- [Node 14](https://www.npmjs.com/package/@tsconfig/node14) |
| 101 | +- [Deno](https://www.npmjs.com/package/@tsconfig/deno) |
| 102 | +- [React Native](https://www.npmjs.com/package/@tsconfig/react-native) |
| 103 | +- [Svelte](https://www.npmjs.com/package/@tsconfig/svelte) |
| 104 | + |
| 105 | +## 细节 |
| 106 | + |
| 107 | +当没有指定 `"compilerOptions"` 时,会使用编译器的默认配置。请参考我们支持的[编译器选项](/tsconfig)列表。 |
| 108 | + |
| 109 | +## TSConfig 参考 |
| 110 | + |
| 111 | +想要了解更多的配置选项的信息,请访问 [TSConfig Reference](/tsconfig)。 |
| 112 | + |
| 113 | +## 协议 |
| 114 | + |
| 115 | +`tsconfig.json` 的协议可以在这里找到 [the JSON Schema Store](http://json.schemastore.org/tsconfig)。 |
0 commit comments