动机一直想在服务端开发中使用typescript。主要原因是:javascript很灵活,但是如果记性不好,确实会让你很头疼。看了看自己一个月前写的代码,一脸茫然。类型检查有好有坏,但在团队开发中,限制个人自由还是很有效的;node对模块等es6特性的支持并不尽如人意。目前我只使用node的长期支持版本能够支持的特性,个人并不想使用babel之类的工具;开始用webstorm开发,结果越来越像visualstudio系列一样臃肿;切换到vscode,但它的绝配是typescript;之前的问题,我一个一个尝试使用,但总是被一些困难绊倒,主要是:vscode开发调试typescript环境的搭建,因为vscode的版本更新很快,网上的资料是复杂的;tsconfig.json的配置以及tslint.json配置中全局变量的使用和设置;经过多次不断的尝试解决问题,终于到了今天令我满意的地步。环境搭建,基于最新版本(1.26.1)安装node.js,从官网下载操作系统对应的安装包,按照提示安装。全局安装typescriptnpmi-gtypescript生成并配置tsconfig.json运行命令:tsc--initconfigurationfile:{"compilerOptions":{"target":"es2017",//SpecifyECMAScripttargetversion:'ES3'(default),'ES5','ES2015','ES2016','ES2017',or'ESNEXT'"module":"commonjs",//指定要使用的模块:'commonjs','amd','system','umd'or'es2015'"moduleResolution":"node",//选择模块解析策略:'node'(Node.js)or'classic'(TypeScriptpre-1.6)"emitDecoratorMetadata":true,//提供装饰器元数据支持"experimentalDecorators":true,//启用装饰器"allowSyntheticDefaultImports":true,//允许从没有设置默认导出的模块中进行默认导入。"strict":true,//启用所有严格类型检查选项"noImplicitAny":true,//报告带有隐式任何类型的表达式和声明的错误"alwaysStrict":true,//检查严格模式模块中的所有内容,并添加'在每个文件中使用strict'"sourceMap":true,"noEmit":false,//不生成输出文件"removeComments":true,//编译后删除所有注释"importHelpers":true,//从导入辅助函数tslib"strictNullChecks":true,//启用严格空检查"lib":["es2017"],//指定要包含在编译中的库文件"typeRoots":["node_modules/@types"],"types":["node",]],"outDir":"./dist","rootDir":"./src"},"include":[//需要编译的ts文件A*表示文件匹配**表示忽略文件深度"./src/*.ts","./src/**/*.ts"],"exclude":["node_modules","dist","**/*.test.ts","public"]}生成项目配置package.json运行命令:npminit配置文件:{"name":"arest","version":"0.1.0","description":"restserverusekao2,typescript&mongodb.","main":"app.ts","scripts":{"test":"echo\"错误:未指定测试\"&&exit1"},"repository":{"type":"git","url":"git+https://github.com/zoutk/arest.git"},"keywords":["rest","koa2","typescript","mongo"],"dependencies":{"koa":"^2.5.2"},"author":"zhoutk@189.cn","license":"MIT","bugs":{"url":"https://github.com/zoutk/arest/issues"},"主页":"https://github.com/zoutk/arest#readme","devDependencies":{"@types/koa":"^2.0.46","@types/node":"^10.9.4","tslint":"^5.11.0","typescript":"^3.0.3"}}监听文件变化并编译运行命令:tsc-wconfigurevscodeprojectstartlaunch.json配置完成后按F5开始调试运行程序{"version":"0.2.0","configurations":[{"type":"node","request":"launch","name":"Launcher","program":"${workspaceFolder}/dist/app.js","outFiles":["${workspaceFolder}/**/*.js"]}]}tslint的配置和效果使用tslint,可以自定义一些团队常用的编码规则,这里需要注意的是,不仅typescript必须全局安装,tslint必须在项目中安装,否则不生效这个鬼折腾了我好久!{"rules":{"class-name":true,"comment-format":[false,"check-space"],"indent":[true,"spaces"],"无重复变量”:true,“no-eval”:true,“no-internal-module”:true,“无尾随空格”:false,“no-var-keyword”:true,“one-line":[true,"check-open-brace","check-whitespace"],"quotemark":[true,"single"],"semicolon":[true,"never"],"triple-equals":[true,"allow-null-check"],"typedef-whitespace":[true,{"call-signature":"nospace","index-signature":"nospace","parameter":"nospace","property-declaration":"nospace","variable-declaration":"nospace"}],"variable-name":[true,"ban-keywords"],"whitespace":[true,"check-branch","check-decl","check-operator","check-separator","check-type"]}}全局变量的使用全局变量虽然不能被滥用,但也不能没有。以下几点是关键:在tsconfig.json中添加“types”:["node"]npmi@types/nodeglobals.d.ts(文件名随意取),其中声明了全局变量(这是为了让ide知道)在入口文件(app.ts)中引入global模块,赋值给node的全局变量global(这是为了让代码让我知道)项目地址对于这个项目,我去将express+mysql的成功经验移植到koa2+mongohttps://github.com/zoutk/arest使用方法gitclonehttps://github.com/zoutk/arestcdarestnpmitsc-w用vscode打开项目,并按F5运行总结终于踏入了typescript的坑,痛并快乐着!
