如何在vscode 中配置:TypeScript开发node环境

配置项目

创建package.json

建一个目录的文件夹,起一个你喜欢的名字。

node项目嘛,当然要先建立一个package.json

?
1
npm init

跟着提示下一步就好了。

创建 tsconfig.json

typescript 的项目都需要一个tsconfig.json

?
1
tsc --init

会创建一个这样内容的tsconfig.json

?
1
2
3
4
5
6
7
8
9
10
11
12
{
     "compilerOptions" : {
         "module" : "commonjs" ,
         "target" : "es5" ,
         "noImplicitAny" : false ,
         "sourceMap" : false
 
     },
     "exclude" : [
         "node_modules"
     ]
}

因为写node.js你可以修改tagget为es6, 要调试把 sourceMap改为true, 添加allowjs为true,就可以ts和js混合玩了.

?
1
2
3
4
5
6
7
8
9
10
11
12
{
     "compilerOptions" : {
         "module" : "commonjs" ,
         "target" : "es6" ,
         "noImplicitAny" : false ,
         "sourceMap" : true ,
         "allowJs" : true
     },
     "exclude" : [
         "node_modules"
     ]
}

安装 node 的 .d.ts 库

还是因为是node.js开发,我们要安装node的ts库, 为了强类型,主要还是为了智能感知。

?
1
typings install dt~node --global

国内的用typings 的时候,而且就会抽风,好久都完成不了,不要紧,Ctrl+C掉了,再执行,多试几次,总会有成功的时候的。

配置 vscode

打开 vscode

?
1
code .

windows话 也可以右键菜单,open with Code.

配置 TypeScript 编译

按ctrl + shift + b, 如果没有配置过,task, 就会在上面提示。

选择【配置任务运行程序】

会在.vscode文件夹下生成一个 task.json, 基本不用动,我到现在是没有改过它。

?
1
2
3
4
5
6
7
8
9
10
{
     // for the documentation about the tasks.json format
     "version" : "0.1.0" ,
     "command" : "tsc" ,
     "isShellCommand" : true ,
     "args" : [ "-p" , "." ],
     "showOutput" : "silent" ,
     "problemMatcher" : "$tsc"
}

小试身手

我们来创建一个app.ts 文件,随便写点东西吧

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
  * person
  */
class person {
     constructor() {
         
     }
 
     /**
      * print
      */
     public print() {
         console.log( 'this is a person' )
     }
}
 
let p = new person();
p.print();

按Ctrl+Shift+B,编译一下,就会生成一个app.js.

现在运行试试结果吧。


转:http://www.2cto.com/kf/201606/521390.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值