> For the complete documentation index, see [llms.txt](https://yangsx95.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yangsx95.gitbook.io/notes/programming-language/typescript.md).

# TypeScript

因为JavaScript的动态类型不适合用于大型项目的维护，所以微软公司开发了以JavaScript为基础构建的TypeScript语言，是一个JavaScript超集。就像他的名字一样，ts相比较与js，扩展了js并增加了类型，并且由弱类型语言转换为了**强类型语言**。ts可以被任何支持js的平台执行，但是不可以被js解析器直接执行，需要通过编译，将ts转换为js（就像less一样）。

ts对js的扩展：

* 强类型
* 支持ES新特性
* 丰富的配置项
* 将ts编译为任意版本的es，解决了浏览器问题

## 构建TypeScript环境

保证首先要有`Node.js`环境，并在命令台使用npm命令安装TypeScript：

```console
npm install -g typescript
```

## Hello World

```shell
$ echo 'console.log("hello world")' > helloworld.ts
$ tsc helloworld.ts
$ ll
total 16
-rw-r--r--  1 yangsx  staff    28B  9 20 12:22 helloworld.js # 编译为了js
-rw-r--r--  1 yangsx  staff    27B  9 20 12:21 helloworld.ts
# 执行js脚本
$ node helloworld.js
hello world
```
