全局安装TypeScript
npm install typescript -g
安装ts-node
npm install -g ts-node
tsconfig.json
removeComments: true 在编译的时候,去掉注释
strict: true 编译和书写严格按照TypeScript书写
noImplicitAny: true 首先去掉strict: true才能看到效果或者设置为false。允许注解类型any不用特意标明
strictNullChecks: true 允许使用null值为true
rootDir 入口文件
outDir 编译好的文件
sourceMap: true 编译生成sourceMap。排查错误文件
noUnusedLocals: true 没使用到的变量不进行打包
noUnusedParameters: true 没使用到的方法不进行打包
include: [] 编译哪些文件
exclude: [] 不编译哪些文件
files: [] 用法跟include很像
files跟include区别:
include可以编译目录以及文件,而files只能编译文件。
include包含的文件能被exclude排除,而files包含的文件不会被exclude排除
https://www.tslang.cn/docs/handbook/compiler-options.html
联合类型
一个变量可能有两种或两种以上的类型
类型保护-->类型断言-->in语法-->typeof语法-->instanceof语法
创建TS项目
创建package.json npm init -y
创建tsconfig.json tsc --init
格式化代码快捷键 win: alt+shift+f