导读
TypeScript是一种强类型编程语言,它建立在 JavaScript 的基础上,能在任何规模上为您提供更好的工具。
TypeScript 是 JavaScript 的 超集 :因此 JS 语法是合法的 TS。
安装
- 全局安装
npm i typescript -g- 项目安装
npm i typesctipt -D检查
tsc -v
# 输出版本号
# Version 5.8.3拓展
其它编译 ts 的插件,如:ts-node
npm i ts-node -g
npm i @types/node -D可以直接执行 ts 文件
ts-node demo.ts编译
使用 tsc 命令将 ts 文件编译为 js 文件
tsc demo.ts
# 编译生成 demo.js 文件自动监听文件变化,自动编译
tsc demo.ts -w常用命令
- 编译文件
tsc xxx.ts- 自动监听编译
tsc [xxx.ts] -W
# 不指定具体文件时,监听所有文件- 生成 ts 配置文件
tsconfig.json
tsc --init配置文件
初始化
tsc --init- 检测编译
tsc -W或者 vscode > 终端 > 运行任务(新建选项中) > tsc 监视

配置项
| 配置 | 说明 |
|---|---|
| noImplicitAny | 禁止使用隐含的 any 类型,如函数参数没有设置具体类型 |
| strictNullChecks | 开启时不否允许将 null、undefined 赋值给其他类型比如字符串 |
| target | 转换成 JS 的版本 |
| strict | 是否严格模式执行 |
| module | 使用的模块系统 |