文件相关选项的配置

  1. // tsconfig.base
  2. {
  3. // 需要编译的文件
  4. "files": [
  5. "src/a.ts"
  6. ],
  7. /**
  8. * 编译器需要编译的文件或者目录
  9. * 支持通配符
  10. * src/* src下一级目录文件
  11. * src下二级目录文件
  12. */
  13. "include": [
  14. "src"
  15. ],
  16. /**
  17. * 编译器需要排除的文件或者文件夹
  18. * ts默认会排除node-modules下的所有文件,及所有的声明文件
  19. */
  20. "exclude": [
  21. "src/lib"
  22. ]
  23. }
  24. // tsconfig
  25. {
  26. "extends": "./tsconfig.base",
  27. "exclude": [],
  28. // 保存时,编译器自动编译,但是vscode暂不支持此功能
  29. "compileOnSave": true
  30. }

编译相关选项的配置

  1. {
  2. "extends": "./tsconfig.base",
  3. "exclude": [],
  4. // 保存时,编译器自动编译
  5. "compileOnSave": true,
  6. "compilerOptions": {
  7. // ts在第一次编译后,生成一个存储编译信息的文件,二次编译时,会根据这个文件做增量编译,这样可以提高编译速度
  8. // "incremental": true, // 增量编译
  9. // "tsBuildInfoFile": "./buildFile", // 增量编译文件的存储位置
  10. "diagnostics": true, // 打印诊断信息
  11. // "target": "es5", // 目标语言的版本
  12. // "module": "amd", // 生成代码的模块标准
  13. // "outFile": "./app.js", // 将多个相互依赖的文件生成一个文件,可以用在 AMD 模块中
  14. // "lib": ["dom", "es5", "scripthost", "es2019.array"], // TS 需要引用的库,即声明文件,es5 默认 "dom", "es5" , "scripthost"
  15. // "allowJs": true, // 允许编译 JS 文件(js、jsx)
  16. // "checkJs": true, // 允许在 JS 文件中报错,通常与 allowJS 一起使用
  17. // "outDir": "./out", // 指定输出目录
  18. // "rootDir": "./src", // 指定输入文件目录(用于输出)
  19. // "declaration": true, // 生成声明文件
  20. // "declarationDir": "./d", // 声明文件的路径
  21. // "emitDeclarationOnly": true, // 只生成声明文件
  22. // "sourceMap": true, // 生成目标文件的 sourceMap
  23. // "inlineSourceMap": true, // 生成目标文件的 inline sourceMap
  24. // "declarationMap": true, // 生成声明文件的 sourceMap
  25. // "typeRoots": [], // 声明文件目录,默认 node_modules/@types
  26. // "types": [], // 声明文件包
  27. // "removeComments": true, // 删除注释
  28. // "noEmit": true, // 不输出文件
  29. // "noEmitOnError": true, // 发生错误时不输出文件
  30. // "noEmitHelpers": true, // 不生成 helper 函数,需额外安装 ts-helpers
  31. // "importHelpers": true, // 通过 tslib 引入 helper 函数,文件必须是模块
  32. // "downlevelIteration": true, // 降级遍历器的实现 (es3/5)
  33. // "strict": true, // 开启所有严格的类型检查
  34. // "alwaysStrict": false, // 在代码中注入 "use strict"
  35. // "noImplicitAny": true, // 不允许隐式的 any 类型
  36. // "strictNullChecks": true, // 不允许把 null、undefined 赋值给其他类型变量
  37. // "strictFunctionTypes": false, // 不允许函数参数双向协变
  38. // "strictBindCallApply": true, // 严格的 bind/call/apply 检查
  39. // "strictPropertyInitialization": true, // 类的实例属性必须初始化
  40. // "noImplicitThis": false, // 不允许 this 有隐式的 any 类型
  41. // "noUnusedLocals": true, // 检查只声明,未使用的局部变量
  42. // "noUnusedParameters": true, // 检查未使用的函数参数
  43. // "noFallthroughCasesInSwitch": true, // 防止 switch 语句贯穿
  44. // "noImplicitReturns": true, // 每个分支都要有返回值
  45. // "esModuleInterop": true, // 允许 export = 导出,由import from 导入
  46. // "allowUmdGlobalAccess": true, // 允许在模块中访问 UMD 全局变量
  47. // "moduleResolution": "node", // 模块解析策略
  48. // "baseUrl": "./", // 解析非相对模块的基地址
  49. // "paths": {}, // 路径映射,相对于 baseUrl
  50. // "rootDirs": [], // 将多个目录放在一个虚拟目录下,用于运行时
  51. // "listEmittedFiles": true, // 打印输出的文件
  52. // "listFiles": true, // 打印编译的文件(包括引用的声明文件)
  53. }
  54. }

ts模块解析策略

  • node解析策略
    相对方式导入,会检查以下文件,如果都没有找到,会在moduleB目录下接着找,查找package.json 有没有types属性,如果没有则会查找index
    非相对方式导入,会查找当前目录下的node_modules,依次解析,当前目录没有,就会向上查找,直至查到node_modules
    十三、tsconfig配置 - 图1

  • classc解析策略
    classc解析策略用于AMD | System | ES2015模块
    相对导入,会依次解析下面的文件,
    非相对方式导入,会从node_modules目录下查找,如果本级目录没有,就会向上查找
    十三、tsconfig配置 - 图2

TS默认使用node的解析策略

工程引用配置

基础配置

  1. {
  2. "compilerOptions": {
  3. "target": "es5",
  4. "module": "commonjs",
  5. "strict": true,
  6. // 工程可以被引用,可以进行增量编译
  7. "composite": true,
  8. // 生成声明文件,工程引用必须的
  9. "declaration": true
  10. }
  11. }

子工程配置

  1. <!--common-->
  2. {
  3. "extends": "../../tsconfig.json",
  4. "compilerOptions": {
  5. "outDir": "../../dist/common"
  6. }
  7. }
  8. <!--client-->
  9. {
  10. "extends": "../../tsconfig.json",
  11. "compilerOptions": {
  12. "outDir": "../../dist/client"
  13. },
  14. "references": [
  15. { "path": "../common" }
  16. ]
  17. }
  18. <!--server-->
  19. {
  20. "extends": "../../tsconfig.json",
  21. "compilerOptions": {
  22. "outDir": "../../dist/server"
  23. },
  24. "references": [
  25. { "path": "../common" }
  26. ]
  27. }
  28. <!--test-->
  29. {
  30. "extends": "../tsconfig.json",
  31. "references": [
  32. { "path": "../src/client" },
  33. { "path": "../src/server" }
  34. ]
  35. }