介绍

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

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script src="./main.ts"></script>
  10. </body>
  11. </html>

新建 main.ts

  1. alert('Hello World for Typescript, Build by parcel.');

编译运行

  1. > parcel index.html
  2. Server running at http://localhost:1234
  3. \ Building hmr-runtime.js...Browserslist: caniuse-lite is outdated. Please run next command `npm update caniuse-lite browserslist`
  4. Built in 541ms.

访问http://localhost:1234
ts-ok.png
参考:
B站千锋网教学视频《Typescript》