typescript-koa-postgresql 实现一个简单的rest风格服务器 —— typescript 开发环境配置

 最近需要用 nodeJS 写一个后台程序,为了能够获得 IDE 的更多代码提示,决定用 typescript 来编写,随便也学习下 ts,在这记录下实现过程。

1、新建文件夹typescript-koa-postgresql,初始化项目

yarn init -y

2、安装 typescript

yarn add typescript @types/node --dev

3、配置  typescript 编译环境,在项目根目录下新建文件 tsconfig.json 

 1 {
 2   "compilerOptions": {
 3     "target": "es2017",
 4     "outDir": "./dist",
 5     "module": "commonjs",
 6     "emitDecoratorMetadata": true,
 7     "experimentalDecorators": true,
 8     "lib": [
 9       "es6"
10     ],
11     "noImplicitAny": false,
12     "sourceMap": false,
13     "allowJs": true
14   },
15   "include": [
16     "./src/**/*"
17   ],
18   "exclude": [ 
19     "node_modules" 
20   ] 
21 }

4、测试  typescript 环境,新文件夹 src 并添加文件 server.ts 

console.log("Hello TypeScript");

 在 package.json 中加入

"scripts": {
     "build": "tsc"
 }

运行 

yarn run build
node ./dist/server.js

输出

Hello TypeScript

至此  typescript 环境 配置完成

目录结构如下:

猜你喜欢

转载自www.cnblogs.com/lifefriend/p/10020831.html