| description |
|---|
安装 TypeScript,完成第一次编译与运行。 |
- 安装 TypeScript
- 编写第一个
.ts文件 - 编译成
.js并运行
- 已安装 VS Code(见 安装与上手 VS Code)
- 已安装 Node.js(见 安装 Node.js 并在 VS Code 运行 JavaScript)
首次体验可以用“全局安装”,更简单;长期使用建议“项目内安装”。
方式 A:全局安装(上手更快)
npm install -g typescript方式 B:项目内安装(更推荐)
npm init -y
npm install -D typescript新建 hello.ts,写入:
const message: string = "hello from ts";
console.log(message);全局安装时,直接运行:
tsc hello.ts项目内安装时,使用:
npx tsc hello.ts编译后会生成 hello.js,再运行:
node hello.jstsc命令找不到:先用npx tsc,或确认是否全局安装成功。- 没生成
hello.js:确认当前目录是否有写入权限。
(需要深入时再查即可。)