VSCode搭建TypeScript开发环境
-
- 1、准备工作
- [2、VS Code必备插件](#2、VS Code必备插件)
- 3、创建TypeScript项目
- 4、目录结构(推荐)
- 5、运行typescript项目
1、准备工作
1. 安装VS Code
官网:https://code.visualstudio.com
2. 安装Node.js
bash
brew install node
node -v
npm -v
3. 安装TypeScript
bash
npm install -g typescript
tsc -v
2、VS Code必备插件
- TypeScript and JavaScript Language Features:VS Code内置
- ESLint:代码规范
- Prettier - Code formatter:自动格式化
- Path Intellisense:路径补全
- DotENV:env文件高亮
3、创建TypeScript项目
1. 新建项目
bash
mkdir ts-vscode
cd ts-vscode
npm init -y
2. 初始化tsconfig.json
js
tsc --init
推荐tsconfig.json(可直接使用)
json
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"moduleResolution": "node",
"rootDir": "src",
"outDir": "dist",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src"]
}
4、目录结构(推荐)
ts-vscode/
├── src/
│ └── index.ts
├── dist/
├── package.json
├── tsconfig.json
src/index.ts
ts
const greet = (name: string): void => {
console.log(`Hello, ${name}`);
};
greet("VS Code");
5、运行typescript项目
5.1、ts-node(方便)
bash
npm install -D ts-node
运行:
bash
npx ts-node src/index.ts
5.2、先编译再运行
bash
tsc
node dist/index.js