TS-入门(优化编译)

263 阅读1分钟
  • TS可以修复代码,类型检查,自动推断类型,自动跳转到定义变量的位置

  • npm install -g typescript
    

解决TS与JS的冲突问题

ts --init #生成配置文件tsconfig.json

自动编译

tsc --watch #ts编译时,同步生成js文件

修改编译出入口

// 在tsconfig.json中修改编译文件出入口
"target": "es2020",  
"rootDir": "./src",   
"outDir": "./dist",  

发出错误

tsc -noEmitOnError xxx.ts #ts编译出错时,不会同步编译js

降级编译

// 将ES6的语法编译为浏览器支持的语法
// 在tsconfig.json中修改
"target": 'es5'

严格模式

// 在tsconfig.json中修改 ts编译时的严格模式
"strict": true, // 启动严格模式
"noImplicitAny": true, // d隐式的any类型
"strictNullChecks": true, // 对于null和undefined进行检测