Typescript中的配置文件

242 阅读2分钟

typescript init

  • npm install typescript -g 全局安装
打开vscode
tsc --init  自动生成 tsconfig.json

去掉注解 "removeComments": true

在vscode控制台
进入项目目录直接打 tsc, 不用指定文件
生成的.js文件就没有你在.ts里的注解了

// "removeComments": true,  

想要编译成.js文件的集合

控制台当运行tsc的时候
会看"include"到底要编译哪些文件,不加这个key默认编译全部
会看"exclude"不需要要编译哪些文件
"files"的功能和"include"类似

{
  "files": ["./demo.ts"],
  "include": ["./demo.ts"],
  "exclude": [./demo1.ts""]
  // 编译过程中一些属性和配置
  "compilerOptions": {
     …… ……
   }
 }

类型不能显示:any ,"noImplicitAny": true

默认是ture ,因为上面"strict": true,
把 "strict" 注释掉 ,才能把 "noImplicitAny" 改为 false
 
// "noImplicitAny": true, 不明确指定any类型不行 要给个类型
// "noImplicitAny": false, 不明确指定any类型也可以 直接:any

"strictNullChecks": true 强制检查null类型

// "strictNullChecks": false  不强制检查null类型
const teacher: string = null

"rootDir" 入口文件, "outDir" 编译过后生成文件放到哪里去,"outFile" 把所有输出文件放到的位置

// "outDir": "./build"
// "rootDir": "./src",
// "outFile": "./build/page.js" 把所有输出文件放到build目录下的page.js这一个文件里

// 会报一个错误,如果你想把输出的文件都打包到一个文件里去[page.js],他就不支持commonjs模块化的规范,它只支持 AMD 或 system

// "module": "amd"   上面的内容有讲解,不支持默认的 "module": "commonjs"

"incremental": true 之前编译过的内容,不会在编译,只会编译这次新增的内容

// "incremental": true 

"allowJs": true 编译一个es6的.js文件,生成es5的语法的文件

// "allowJs": true  可以对.js文件进行打包编译

"checkJs": true 检测.js文件的语法

// "checkJs": true  如果.js文件有东西写错,会出现下划线红色提示

"sourceMap": true 打包过程中生成代码的sourceMap

// "sourceMap": true,  生成后缀.map文件 

"noUnusedLocals": true 有东西被定义但没有被使用,红色下划线警告

// "noUnusedLocals": true,  

"noUnusedParameters": true 对函数的参数进行校验,下划线红色警告

// "noUnusedParameters": true

结语

前端react QQ群:788023830 ---- React/Redux - 地下老英雄

前端交流 QQ群:249620372 ---- FRONT-END-JS前端

(我们的宗旨是,为了加班,为了秃顶……,仰望大佬),希望小伙伴们加群一起学习