14.1 tsconfig.json 的作用

  • 用于标识 TypeScript 项目的根路径;
  • 用于配置 TypeScript 编译器;
  • 用于指定编译的文件。

    14.2 tsconfig.json 重要字段

  • files - 设置要编译的文件的名称;

  • include - 设置需要进行编译的文件,支持路径模式匹配;
  • exclude - 设置无需进行编译的文件,支持路径模式匹配;
  • compilerOptions - 设置与编译流程相关的选项。

    14.3 compilerOptions 选项

    compilerOptions 支持很多选项,常见的有 baseUrltargetbaseUrlmoduleResolutionlib 等。
    compilerOptions 每个选项的详细说明如下:
    1. {
    2. "compilerOptions": {
    3. /* 基本选项 */
    4. "target": "es5", // 指定 ECMAScript 目标版本: 'ES3' (default), 'ES5', 'ES6'/'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'
    5. "module": "commonjs", // 指定使用模块: 'commonjs', 'amd', 'system', 'umd' or 'es2015'
    6. "lib": [], // 指定要包含在编译中的库文件
    7. "allowJs": true, // 允许编译 javascript 文件
    8. "checkJs": true, // 报告 javascript 文件中的错误
    9. "jsx": "preserve", // 指定 jsx 代码的生成: 'preserve', 'react-native', or 'react'
    10. "declaration": true, // 生成相应的 '.d.ts' 文件
    11. "sourceMap": true, // 生成相应的 '.map' 文件
    12. "outFile": "./", // 将输出文件合并为一个文件
    13. "outDir": "./", // 指定输出目录
    14. "rootDir": "./", // 用来控制输出目录结构 --outDir.
    15. "removeComments": true, // 删除编译后的所有的注释
    16. "noEmit": true, // 不生成输出文件
    17. "importHelpers": true, // tslib 导入辅助工具函数
    18. "isolatedModules": true, // 将每个文件做为单独的模块 (与 'ts.transpileModule' 类似).
    19. /* 严格的类型检查选项 */
    20. "strict": true, // 启用所有严格类型检查选项
    21. "noImplicitAny": true, // 在表达式和声明上有隐含的 any类型时报错
    22. "strictNullChecks": true, // 启用严格的 null 检查
    23. "noImplicitThis": true, // this 表达式值为 any 类型的时候,生成一个错误
    24. "alwaysStrict": true, // 以严格模式检查每个模块,并在每个文件里加入 'use strict'
    25. /* 额外的检查 */
    26. "noUnusedLocals": true, // 有未使用的变量时,抛出错误
    27. "noUnusedParameters": true, // 有未使用的参数时,抛出错误
    28. "noImplicitReturns": true, // 并不是所有函数里的代码都有返回值时,抛出错误
    29. "noFallthroughCasesInSwitch": true, // 报告 switch 语句的 fallthrough 错误。(即,不允许 switch case 语句贯穿)
    30. /* 模块解析选项 */
    31. "moduleResolution": "node", // 选择模块解析策略: 'node' (Node.js) or 'classic' (TypeScript pre-1.6)
    32. "baseUrl": "./", // 用于解析非相对模块名称的基目录
    33. "paths": {}, // 模块名到基于 baseUrl 的路径映射的列表
    34. "rootDirs": [], // 根文件夹列表,其组合内容表示项目运行时的结构内容
    35. "typeRoots": [], // 包含类型声明的文件列表
    36. "types": [], // 需要包含的类型声明文件名列表
    37. "allowSyntheticDefaultImports": true, // 允许从没有设置默认导出的模块中默认导入。
    38. /* Source Map Options */
    39. "sourceRoot": "./", // 指定调试器应该找到 TypeScript 文件而不是源文件的位置
    40. "mapRoot": "./", // 指定调试器应该找到映射文件而不是生成文件的位置
    41. "inlineSourceMap": true, // 生成单个 soucemaps 文件,而不是将 sourcemaps 生成不同的文件
    42. "inlineSources": true, // 将代码与 sourcemaps 生成到一个文件中,要求同时设置了 --inlineSourceMap --sourceMap 属性
    43. /* 其他选项 */
    44. "experimentalDecorators": true, // 启用装饰器
    45. "emitDecoratorMetadata": true // 为装饰器提供元数据的支持
    46. }
    47. }

声明全局类型

.d.ts
index.html

  1. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.0/jquery.js"></script>

index.ts

  1. $(function() {
  2. console.log('hello')
  3. $('body').html('<div>Hello</div>')
  4. })

jquery.d.ts

  1. // 定义全局变量
  2. // declare var $: (param: () => void) => void // 声明变量
  3. // 定义全局函数
  4. declare function $(param: () => void): void
  5. //
  6. declare function $(param: string): {html: (html: string) => {}}

模块代码的类型描述文件

yarn add jquery
jquery.d.ts

  1. // es6 模块
  2. declare module 'jquery' {
  3. interface JqueryInstance {
  4. html: (html: string) => JqueryInstance
  5. }
  6. // 混合类型
  7. function $(readyFunc: () => void): void;
  8. function $(selector: string): JqueryInstance;
  9. namespace $ {
  10. namespace fn {
  11. class init()
  12. }
  13. }
  14. export = $;
  15. }

index.ts

  1. import $ from 'jquery';
  2. $(function() {
  3. $("body").html("<div>Hello</div>")
  4. new $.fn.init();
  5. });