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

来源:互联网 发布:软件需求分析报告 编辑:程序博客网 时间:2024/06/02 04:24

配置项目

创建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
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // 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
 */
classperson {
    constructor() {
         
    }
 
    /**
     * print
     */
    publicprint() {
        console.log('this is a person')
    }
}
 
let p = newperson();
p.print();

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

现在运行试试结果吧。


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

0 0
原创粉丝点击