介绍
Typescript是Javascript的一个超集,主要提供类型检查和对ES6的支持,由Microsoft开发维护,已在Github上开源、
特点:
- 增加了代码的可读性和可维护性
- 包容性:Typescript是Javascript的超集,既兼容ES5语法开发,也支持ES6语法
- 拥有非常活跃的社区,Vue、React、Angular主流框架纷纷支持Typescript
安装与编译
- 全局安装 npm install -g typescript
- 编译命令 tsc hello.ts,将typescript代码编译成ES5脚本
配置本地开发环境
parcel版本
参考地址:https://www.parceljs.cn/typeScript.html
新建 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="./main.ts"></script>
</body>
</html>
新建 main.ts
alert('Hello World for Typescript, Build by parcel.');
编译运行
> parcel index.html
Server running at http://localhost:1234
\ Building hmr-runtime.js...Browserslist: caniuse-lite is outdated. Please run next command `npm update caniuse-lite browserslist`
√ Built in 541ms.
访问http://localhost:1234
参考:
B站千锋网教学视频《Typescript》