战魔
网络游戏 | 104M | 2020-12-16
下载来自: 98游戏 浏览: 3 次 2026-09-03 11:12:42:09

Visual Studio Code 运行 TypeScript,首要前提是本地已安装 Node.js 和 VS Code。
VS Code 自带 TypeScript 语言服务(无需额外安装扩展),但需确保使用的是项目级 TypeScript 版本:
TS 5.4.5); typescript 到 devDependencies); npm install --save-dev typescript
在项目目录中创建标准结构(推荐):
src/ 文件夹; hello.ts(扩展名必须为 .ts); tsconfig.json 中的 rootDir 和 outDir。在 src/hello.ts 中输入如下示例:
Visual Studio 18.8.1 官方固定版本安装引导程序,当前条目使用微软发布历史中的 Professional Web Installer,适合旧项目兼容、环境回退、复现特定构建链和排查版本差异等场景。
function greet(name: string): string {
return `hello, ${name}!`;
}
const message = greet('Visual Studio Code');
console.log(message);✅ 注意:类型标注(如 : string)和返回类型声明增强可维护性,VS Code 将实时提供类型检查与错误提示。
Ctrl +); tsconfig.json): npx tsc --init
tsconfig.json,确保关键项如下: {
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"sourceMap": true,
"strict": true,
"noEmit": false
},
"include": ["src/**/*"]
}npx tsc
成功后将在 dist/ 下生成 hello.js 和 hello.js.map;
node dist/hello.js
输出:hello, Visual Studio Code!
.ts 文件(开发调试首选)ts-node(含类型检查支持): npm install --save-dev ts-node @types/node
npx ts-node src/hello.ts
✅ 支持实时类型校验(加 --files 参数可强制全量检查);
❌ 注意:ts-node 默认不启用 strict 模式下的全部检查,建议仍以 npx tsc --noEmit 做最终验证。
.ts 源码(断点即停,无需切换 .js)Ctrl+Shift+P → 输入并选择 “Debug: Open launch.json”; .vscode/launch.json; ts-node): {
"version": "0.2.0",
"configurations": [
{
"name": "调试 TypeScript",
"type": "pwa-node",
"request": "launch",
"program": "${workspaceFolder}/src/hello.ts",
"runtimeArgs": ["-r", "ts-node/register"],
"skipFiles": ["<node_internals>/**"],
"env": { "TS_NODE_FILES": "true" }
}
]
}.ts 文件中任意行左侧点击设断点 → 按 F5 启动调试,即可逐行执行源码并查看变量值。⚠️ 提示:避免混用
preLaunchTask: "tsc: build"与ts-node配置,否则易引发路径冲突或重复编译。
还没有玩家发表评论,快来抢占沙发吧!