如今写js不用TypeScript,伦家可能会以为你是外星人。
是的,TypeScript很大程度加强了代码的可读性,可跟踪性,可维护性和减小了bug。
那么没有理由不适用TypeScript进行js开发,可是回归本质,要根据实际出发,不是盲目的一来直接上TS。css
我参与过一些使用TS开发的项目,也发现使用TS出现的问题。
举点例子:html
app.get("/", function(req: any, res: any) { res.send("Hello," + data.name); });
node + typescript 入门级别的配置真的特别简单。node
简单三步,你就能够经过简单的一步npm run xxx,TS文件们就乖乖的变为了js文件。
而后执行node dist/xx.js就能启动你的服务了。
可是,这不是咱们所指望的。
咱们但愿我修改ts文件后,自动编译为js文件,而后启动服务。git
我了解的相对较好的有下面三种方式:github
这个方案是无心中看到的,star并非特别多,400左右。
ts-node-dev 是基于 ts-node 的。
ts-node 能够直接执行ts文件,是否是很酷。
一句代码就能够监听文件变化,并运行编译后的代码。
~~~
ts-node-dev --respawn app.ts
~~~typescript
我为何要将Typescript与Express、nodejs一块儿使用(译文)express
微软这也有一个参考。也有对tslint的支持。
基本思路就是package.json 的scripts。
其主要使用的是 concurrently 和# nodemon。
其package.json的scripts以下:npm
"scripts": { "start": "npm run serve", "build": "npm run build-sass && npm run build-ts && npm run tslint && npm run copy-static-assets", "serve": "node dist/server.js", "watch-node": "nodemon dist/server.js", "watch": "concurrently -k -p \"[{name}]\" -n \"Sass,TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-sass\" \"npm run watch-ts\" \"npm run watch-node\"", "test": "jest --forceExit --coverage --verbose", "watch-test": "npm run test -- --watchAll", "build-ts": "tsc", "watch-ts": "tsc -w", "build-sass": "node-sass src/public/css/main.scss dist/public/css/main.css", "watch-sass": "node-sass -w src/public/css/main.scss dist/public/css/main.css", "tslint": "tslint -c tslint.json -p tsconfig.json", "copy-static-assets": "ts-node copyStaticAssets.ts", "debug": "npm run build && npm run watch-debug", "serve-debug": "nodemon --inspect dist/server.js", "watch-debug": "concurrently -k -p \"[{name}]\" -n \"Sass,TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-sass\" \"npm run watch-ts\" \"npm run serve-debug\"" }
TypeScript with Node.js里面提供了更加简单的方法。 nodemon + ts-node
~~~
"scripts": {
"start": "npm run build:live",
"build": "tsc -p .",
"build:live": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts"
},
~~~
TypeScript-Node-Starter的package.json能够好好看看,具备很好的扩展性。json
gulp-typescript负责编译ts
gulp-nodemon负责启动服务
主要任务就是copy, compile和watchgulp
const gulp = require("gulp"); const ts = require("gulp-typescript"); const nodemon = require('gulp-nodemon') const del = require('del'); const sourcemaps = require('gulp-sourcemaps'); const tsProject = ts.createProject("tsconfig.json", { noImplicitAny: true }); // 默认任务 gulp.task("default", ["copy", "compile", "watch"], function () { console.log('started .....') }) // 复制配置文件 gulp.task('copy', function () { return gulp.src("./src/config/**/*.json") .pipe(gulp.dest("./dist/config")) }) // 编译 gulp.task("compile", function () { return gulp.src("./src/**/*.ts") .pipe(sourcemaps.init({ loadMaps: true })) .pipe(tsProject()) .js .pipe(sourcemaps.write('./maps')) .pipe(gulp.dest("dist")); }) gulp.task('build',['compile','copy']) // 删除 gulp.task('del', function (cb) { return del([ 'dist/**', ], cb); }); // 监听变化 gulp.task('watch', ['compile'], function (done) { var stream = nodemon({ script: 'dist/app.js', watch: 'src/**', tasks: ['copy', 'compile'], done: done, ext: 'ts html json' }) return stream })
使用typescript开发node js
TypeScript with Node.js
-------------------------
到这里,咱们已经又进了一步。
VSCode自己有插件TSLint插件,并且有新旧版。
ESLint插件默认是 能够读取tslint.json的配置的,若是没有,他有本身的默认配置。
新版的,若是有错误,默认是警告,而不是错误提示。
你去首选项勾选掉:tslint.alwaysShowRuleFailuresAsWarnings
便可。
咱们是须要添加eslint.json配置的
所以,咱们还须要安装tslint包。
还须要在package.json的文件里面添加一个脚本
"tslint": "tslint -c tslint.json -p tsconfig.json",
代码美化。不少编辑器都有相似功能, VSCode也不例外。
VSCode的Prettier内置了prettier-eslint
和prettier-tslint
插件。
你能够在配置里面json文件修改或者配置面板修改。
"prettier.eslintIntegration": false,
这样一来,Prettier使用的就是tslint的配置。
在这里之后, windows换将下,默认状况,你就能够Ctl + Shift + F
自动格式化代码了。并且是按照你的tslint配置来格式化的, 就问你怕不怕。
咱们最后来看一下package.json下scripts的配置
"scripts": { "build": "npm run tslint && npm run build-ts", "build-ts": "tsc", "watch-ts": "tsc -w", "watch-node": "nodemon dist/app.js", "tslint": "tslint -c tslint.json -p tsconfig.json", "dev": "concurrently \\"npm:watch-ts\\" \\"npm run watch-node\\"" }
这样一来,感受轻松多了。 想一想就没好,自动美化代码,编写后自动启动服务。 喝点茶,出去走走。