从零开始学习typescript系列2: typescript配置文件ts.config.js之详细解释

基本了解

tsconfig.json 是 ts 支持的配置文件

大体可以分为两个部分描述,

  • 第一部分:编译规则配置(compilerOptions),
  • 第二个部分:哪些文件进行编译(files,include,exclude)
json 复制代码
{
    "compilerOptions": {},
    "files": [], 
    "include": [],
    "exclude": [] 
}
shell 复制代码
# ts配置相关命令
tsc #  默认使用最近配置文件
tsc --all #   所有编译选项 
tsc --all  | grep "keyofStringsOnly" #  某一个编译选项 
tsc --init #  可以生产默认的配置文件

demo:常见配置

js 复制代码
{
  // files: 适用于比较小型的项目,规定几个特定的文件
  // 将compilerOption的编译规则应用于index.ts下
  "files": ["index.ts"], 
  // include: 所有src目录和test目录下的文件都会被编译
  "include":["src/**/*", "test/**/*"] 
  // exclude: 不需要被编译的文件目录
  "exclude": [ "node_modules","dist","**/*.test.ts"], 
  "compilerOptions": { 
    // 用于抑制隐式 any 索引错误的报告。在ts5.5版本是废弃的属性    
    "suppressImplicitAnyIndexErrors": true,
    // 忽略废弃警告
    "ignoreDeprecations": "5.0", 
    // 跳过对所有声明文件(.d.ts 文件)的类型检查
    // 跳过类型检查可能会隐藏潜在的类型不一致问题,建议仅在明确的情况下使用
    "skipLibCheck": true,
    // types: 告诉 ts 编译器要引入哪些第三方库或模块的类型声明
    // 当 TypeScript 编译器遇到使用 React 或 Lodash 的代码时,它会根据指定的类型声明文件进行类型检查和推断,以确保你的代码与这些库的 API 相符合。
    "types": ["react", "lodash"],
    // 没有设置baseUrl: import { example } from "./src/hello/world
    // 设置了baseUrl: import { example } from "hello/world
    "baseUrl": ".",
    // paths必须和baseUrl联合使用
    // 设置paths后:import from '@/components/Button'  import from  相当于  src/components/Button
    "paths": {
        "@/*": ["src/*"]
     },
    // lib: 引入ES功能库,如想在项目中用js中Set,Map,promise等,需引入es2015
    "lib": ["es6", "dom"],  
    //  编译成哪个版本的js,es3,es5,es6...
    "target": "es5",   
    // 哪个模块规范:  cjs,amd,umd,esm
    "module": "commonjs",  
    // 选择模块解析策略 比如 在node_modules中查找路径
    // 'node':  Node.js' CommonJS implementation  =》 ts 社区最常用的,推荐大多数项目
    // 'node16' or 'nodenext': Node.js' ECMAScript Module Support from TypeScript 4.7 onwards
    // 'classic': used in TypeScript before the release of 1.6. 
    "moduleResolution": "node", 
    "experimentalDecorators": true,  // 启用实验性的ES装饰器
    // 允许从没有设置默认导出的模块中默认导入
    // false: 必须 import * as React from 'react 
    // true: 可以 import React from 'react'
    "allowSyntheticDefaultImports": true,   
    // sourceMap: 把 ts 文件编译成 js 文件的时候,同时生成对应的 map 文件
    "sourceMap": true,   
    "strict": true,  // 启用所有严格类型检查选项
    // noImplicitAny: 不允许用any,如果初学ts,建议项目部太复杂的情况下,可以借此来进行限制,前置自己培养对ts的理解 (实际情况不是)
    "noImplicitAny": true,  // 在表达式和声明上有隐含的 any类型时报错
    "alwaysStrict": true,  // 以严格模式检查模块,并在每个文件里加入 'use strict'
    "declaration": true,   // 生成相应的.d.ts文件
    // 删除编译后的所有的注释
    "removeComments": true,   
    "noImplicitReturns": true,  // 不是函数的所有返回路径都有返回值时报错
    "importHelpers": true,  // 从 tslib 导入辅助工具函数
    // typeRoots: 指定类型声明文件的根目录路径
    "typeRoots": [
      "./node_modules/@types/", // 这是个目录
      "./types" // 这是个目录
    ],
    "outDir": "./dist",
    "rootDir": "./src",
    "keyofStringsOnly": false, 
  },

}
相关推荐
摘星编程26 分钟前
【成长纪实】HarmonyOS Next学习地图:新手避坑指南与核心知识点拆解
学习·华为·harmonyos·鸿蒙开发
咖啡の猫42 分钟前
Vue初始化脚手架
前端·javascript·vue.js
拉不动的猪2 小时前
如何处理管理系统中(Vue PC + uni-app 移动端):业务逻辑复用基本方案
前端·javascript·架构
边洛洛2 小时前
next.js项目部署流程
开发语言·前端·javascript
非凡ghost2 小时前
Syncovery Premium(文件同步软件)
前端·javascript·后端
神膘护体小月半3 小时前
bug 记录 - 路由守卫 beforeRouteLeave 与 confirm 结合,不生效问题
javascript·vue
岁月宁静3 小时前
Vue 3.5 + WangEditor 打造智能笔记编辑器:语音识别功能深度实现
前端·javascript·vue.js
非凡ghost3 小时前
BiliLive-tools(B站录播一站式工具) 中文绿色版
前端·javascript·后端
非凡ghost3 小时前
bkViewer小巧精悍数码照片浏览器 中文绿色版
前端·javascript·后端
西洼工作室3 小时前
前端监控:错误捕获与行为日志全解析
前端·javascript