| 
 | 1 | +# 编译选项  | 
 | 2 | + | 
 | 3 | +TypeScript 提供了非常多的编译选项,但是官方文档对每一项的解释很抽象,这一章会详细介绍每一个选项的作用,并给出对应的示例。  | 
 | 4 | + | 
 | 5 | +索引(点击选项跳转到详细介绍):  | 
 | 6 | + | 
 | 7 | +选项 | 类型 | 默认值 | 描述  | 
 | 8 | +--- | --- | --- | ---  | 
 | 9 | +[`allowJs`](#allowjs) | `boolean` | `false` | 允许编译 js 文件  | 
 | 10 | +[`allowSyntheticDefaultImports`](#allowsyntheticdefaultimports`) | `boolean` | `false` | 允许对不包含默认导出的模块使用默认导入。这个选项不会影响生成的代码,只会影响类型检查。  | 
 | 11 | + | 
 | 12 | +## allowJs  | 
 | 13 | + | 
 | 14 | +> 允许编译 js 文件。  | 
 | 15 | +
  | 
 | 16 | +设置为 `true` 时,js 文件会被 tsc 编译,否则不会。一般在项目中 js, ts 混合开发时需要设置。  | 
 | 17 | + | 
 | 18 | +[查看示例](https://github.com/xcatliu/typescript-tutorial/tree/master/examples/compiler-options/01-allowJs)  | 
 | 19 | + | 
 | 20 | +```bash  | 
 | 21 | +# 设置为 true 时,编译后的文件包含 foo.js  | 
 | 22 | +├── lib  | 
 | 23 | +│   ├── foo.js  | 
 | 24 | +│   └── index.js  | 
 | 25 | +├── src  | 
 | 26 | +│   ├── foo.js  | 
 | 27 | +│   └── index.ts  | 
 | 28 | +├── package.json  | 
 | 29 | +└── tsconfig.json  | 
 | 30 | +```  | 
 | 31 | + | 
 | 32 | +```bash  | 
 | 33 | +# 设置为 false 时,编译后的文件不包含 foo.js  | 
 | 34 | +├── lib  | 
 | 35 | +│   └── index.js  | 
 | 36 | +├── src  | 
 | 37 | +│   ├── foo.js  | 
 | 38 | +│   └── index.ts  | 
 | 39 | +├── package.json  | 
 | 40 | +└── tsconfig.json  | 
 | 41 | +```  | 
 | 42 | + | 
 | 43 | +## allowSyntheticDefaultImports  | 
 | 44 | + | 
 | 45 | +> 允许对不包含默认导出的模块使用默认导入。这个选项不会影响生成的代码,只会影响类型检查。  | 
 | 46 | +
  | 
 | 47 | +`export = foo` 是 ts 为了兼容 commonjs 创造的语法,它对应于 commonjs 中的 `module.exports = foo`。  | 
 | 48 | + | 
 | 49 | +在 ts 中,如果要引入一个通过 `export = foo` 导出的模块,标准的语法是 `import foo = require('foo')`,或者 `import * as foo from 'foo'`。  | 
 | 50 | + | 
 | 51 | +但由于历史原因,我们已经习惯了使用 `import foo from 'foo'`。  | 
 | 52 | + | 
 | 53 | +这个选项就是为了解决这个问题。当它设置为 `true` 时,允许使用 `import foo from 'foo'` 来导入一个通过 `export = foo` 导出的模块。当它设置为 `false` 时,则不允许,会报错。  | 
 | 54 | + | 
 | 55 | +当然,我们一般不会在 ts 文件中使用 `export = foo` 来导出模块,而是在[写(符合 commonjs 规范的)第三方库的声明文件](../basics/declaration-files#export-1)时,才会用到 `export = foo` 来导出类型。  | 
 | 56 | + | 
 | 57 | +比如 React 的声明文件中,就是通过 `export = React` 来导出类型:  | 
 | 58 | + | 
 | 59 | +```ts  | 
 | 60 | +export = React;  | 
 | 61 | +export as namespace React;  | 
 | 62 | + | 
 | 63 | +declare namespace React {  | 
 | 64 | +    // 声明 React 的类型  | 
 | 65 | +}  | 
 | 66 | +```  | 
 | 67 | + | 
 | 68 | +此时若我们通过 `import React from 'react'` 来导入 react 则会报错,[查看示例](https://github.com/xcatliu/typescript-tutorial/tree/master/examples/compiler-options/02-allowSyntheticDefaultImports)  | 
 | 69 | +:  | 
 | 70 | + | 
 | 71 | +```ts  | 
 | 72 | +import React from 'react';  | 
 | 73 | +// Module '"typescript-tutorial/examples/compiler-options/02-allowSyntheticDefaultImports/false/node_modules/@types/react/index"' can only be default-imported using the 'esModuleInterop' flagts(1259)  | 
 | 74 | +```  | 
 | 75 | + | 
 | 76 | +解决办法就是将 `allowSyntheticDefaultImports` 设置为 `true`。  | 
 | 77 | + | 
 | 78 | +---  | 
 | 79 | + | 
 | 80 | +- [上一章:代码检查](./lint.md)  | 
 | 81 | +- [下一章:感谢](../thanks/README.md)  | 
0 commit comments