VS Code 本地运行 TypeScript 全攻略

要在 VS Code 上 本地运行 .ts 文件,可以按照以下步骤来配置 TypeScript 运行环境,包括 安装检查、设置、运行和测试。

1. 检查 TypeScript 是否已安装

打开 VS Code 终端(快捷键 Ctrl + ~)运行:

tsc -v

如果 成功 显示类似:

Version X.X.X

说明 TypeScript 已安装,可以跳过 第二步

如果 报错

bash: tsc: command not found

或者

tsc : 无法将“tsc”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。

说明 未安装 TypeScript,请进行 第二步

2. 安装 TypeScript

全局安装 TypeScript:

npm install -g typescript

然后再次运行:

tsc -v

如果 成功显示 TypeScript 版本号,说明安装成功 ✅。

如果要在 某个项目中使用 TypeScript(本地安装),请在项目文件夹运行:

npm init -y
npm install --save-dev typescript

然后,创建 tsconfig.json:

npx tsc --init

# Created a new tsconfig.json with:                                                                       
#                                                                                                      TS 
#   target: es2016
#   module: commonjs
#   strict: true
#   esModuleInterop: true
#   skipLibCheck: true
#   forceConsistentCasingInFileNames: true


# You can learn more at https://aka.ms/tsconfig

3. 安装 ts-node(支持直接运行 .ts 文件)

TypeScript (tsc) 默认需要先编译成 .js 再运行,但如果想要 直接运行 .ts 文件,需要安装 ts-node:

npm install -g ts-node

然后检查:

ts-node -v

如果成功显示:

vX.X.X

说明 ts-node 安装成功 ✅。

4. 创建 .ts 测试文件

在 VS Code 项目目录下,创建 test.ts,输入:

const greeting: string = "Hello, TypeScript!";
console.log(greeting);

然后 保存 文件。

5. 运行 TypeScript 文件

方法 1:使用 ts-node 直接运行

VS Code 终端 输入:

ts-node test.ts

# 如果是本地安装,请使用 npx ts-node test.ts 运行

 如果输出:

Hello, TypeScript!

说明 运行成功 ✅。

方法 2:使用 tsc 编译并运行

适用于:需要严格检查 TypeScript 代码。

1、编译 TypeScript 为 JavaScript

tsc test.ts

执行后,会生成 test.js 文件。

内容为: 

var greeting = "Hello, TypeScript!";
console.log(greeting);

2、运行 JavaScript 

node test.js

如果输出:

Hello, TypeScript!

说明 编译 & 运行成功 ✅。

3、如果 tsc 命令未找到,请先安装 TypeScript:

npm install -g typescript
方法 3:使用 deno 直接运行

适用于:不依赖 Node.js,更加安全。

1、安装 Deno(官网:https://deno.land/)

1)Mac

brew install deno

2)Windows

iwr https://deno.land/install.ps1 -useb | iex

2、运行 TypeScript 文件

deno run test.ts

Deno 直接支持 TypeScript,无需编译。

6. 检查 tsconfig.json 是否生效(可选)

如果项目使用 tsconfig.json,可以运行:

tsc --showConfig

如果有输出类似:

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "noImplicitThis": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictBindCallApply": true,
    "strictPropertyInitialization": true,
    "strictBuiltinIteratorReturn": true,
    "alwaysStrict": true,
    "useUnknownInCatchVariables": true
  },
}

说明 TypeScript 配置文件已生效 ✅。

如果 tsc --showConfig 没有输出,说明 tsconfig.json 可能不存在或格式错误,需要在 VS Code 终端运行:

npx tsc --init

然后 检查 tsconfig.json 是否正确生成。

7. 让 VS Code 自动识别 TypeScrip

如果 VS Code 无法识别 .ts 语法 或 报错:

1、打开 VS Code 设置(Ctrl / Cmd + Shift + P),输入:

TypeScript: Select TypeScript Version

2、选择 "Use workspace version",让 VS Code 使用安装的 TypeScript 版本。

8. 总结

检查 TypeScript 是否安装

tsc -v

安装 TypeScript 和 ts-node

npm install -g typescript ts-node

创建 .ts 文件

console.log("Hello, TypeScript!");

使用 ts-node 直接运行

ts-node test.ts

或先编译再运行

tsc test.ts
node test.js

或使用 deno 运行

deno run test.ts

检查 tsconfig.json 是否生效

tsc --showConfig

完成以上步骤,VS Code 就可以直接运行 TypeScript 文件了!

你可能感兴趣的:(TS,JS,typescript,javascript,VSCode,本地启动)