使用 VS Code 搭建 TypeScript 开发环境

使用 VS Code 搭建 TypeScript 开发环境

TypeScript 是 JavaScript 的超集,TypeScript 只是增强了 JavaScript 而非改变了 JavaScript,TypeScript 同样也是基于 ECMAScript 标准的编程语言。因此非常流行的 Vue 和 React 及我们常用 Jquery 等类库都可以使用 TypeScript 来编码,TypeScript 强大的智能类型分析系统,能够使你的代码更加强壮。

下载 TypeScript

TypeScript 官网(中文网)提供了多种下载方式,我们使用 NPM 来下载 TypeScript,如果你没有安装 NPM,请到 Nodejs 官网进行安装。

在 CMD(Windows 系统)或者终端(macOS 系统)中输入一下命令:

npm install -g typescript

安装完毕后我们输入 tsc -v 可以查看当前安装的 TypeScript 版本号。当前最新的版本是 2.1.5。

下载 VSCode

VSCode 是很棒的编辑器。在 VSCode 官网就可以下载相应系统的 VSCode 安装包

创建项目

mkdir typescript-demo
npm init -y //创建一个 package.json
tsc --init //创建一个 tsconfig.json
touch index.html
touch hello.ts

index.html

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>TypeScript with VSCode</title>
  </head>

  <body>
    <h1 class="text-center"></h1>
    <script src="./hello.js"></script>
  </body>
</html>

hello.ts

function sayHello(person: string) {
  return "hello" + person;
}

console.log("object :", sayHello("monkey"));

先执行npm install -g live-server安装 live-server 之后,修改 package.json

  "scripts": {
    "start": "tsc -w & live-server",
  },

执行 npm start 就可以写一些 ts 的小 demo 了

猜你喜欢

转载自www.cnblogs.com/mybilibili/p/11601970.html