nodejs包typings拓展自动补全功能

来源:互联网 发布:插画师软件 编辑:程序博客网 时间:2024/06/07 05:57

使用Typings添加自动补全

typings官方描述是一个TypeScript的定义管理器,集成管理了常用函数的语法规则。不知道跟微软有没有关系,不过这点倒是真的挺方便的。

官方描述以及使用说明 
https://www.npmjs.com/package/typings

vscode可以识别typings,因此可以通过typings来作为插件拓展vscode的功能。具体使用方法如下:

-配置jsconfig.json

在使用typings之前,需要在vscode里面配置一下名为jsconfig.json的文件。配置方法很简单,随便选中一个js文件,vscode右下角会弹出一个绿色的小灯泡,如图所示: 
配置jsconfig.json 
点击进去,顶部会提示 
“Create a jsconfig.json to enable richer IntelliSense and code navigation across the entire workspace.” 
选择create,vscode就会创造一个jsconfig.json的文件,内容大致如下:

{    // See https://go.microsoft.com/fwlink/?LinkId=759670    // for the documentation about the jsconfig.json format    "compilerOptions": {        "target": "es6",        "module": "commonjs",        "allowSyntheticDefaultImports": true    },    "exclude": [        "node_modules",        "bower_components",        "jspm_packages",        "tmp",        "temp"    ]}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

所有需要的参数都会帮我们设置好。此时我使用的是vscode v1.2.0生成的jsconfig,低版本自动生成的配置可能会比里面的少一点,但不影响。

-安装typings

使用npm全局安装typings

npm install -g typings
  • 1
  • 1

-安装语法插件

以安装node.js自动补全为例,在项目根目录下使用bash或者cmd,输入

typings install dt~node --global
  • 1
  • 1

其中”dt~”为使用DefinitelyTyped类型定义的意思,vscode可以识别这种定义。 
之后可以看到在项目目录下出来了新的文件夹“typings” 
typings文件夹 
现在输入process,自动地补全出来了~window下可能需要重启下vscode才能看到自动补全的效果。 
这里写图片描述

-express、lodash等的自动补全

类似地,可以使用以上的方法,实现其他模块的自动补全。

typings install dt~express --globaltypings install dt~lodash --global
  • 1
  • 2
  • 1
  • 2
//vscode官方文档上express的typings使用的是typings install dt~express dt~serve-static dt~express-serve-static-core --global
  • 1
  • 2
  • 1
  • 2

关于typings的其他相关.

-基本用法

语法复制自typings的Quick Start,个人对注释本地化了一下。

# 安装Typings的命令行代码. npm install typings --global# 搜索对应模块的typings定义. typings search tape# 根据名称寻找一个可获得的typings定义. typings search --name react# 如果你用一个独立包的模块: # 或者并不是安装全局模块# 比如并不是在命令行通过输入npm install -g typings这种方式安装的. typings install debug --save# 如果是通过script标记# 或者是子环境的一部分# 或者全局typings命令不可用的时候: typings install dt~mocha --global --save# 从其他版本处安装typings定义(比如env或者npm). typings install env~atom --global --savetypings install npm~bluebird --save# 使用该文件`typings/index.d.ts` (在`tsconfig.json`文件使用或者用 `///` 定义). cat typings/index.d.ts
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

以上。个人很喜欢使用vscode,通过typings拓展自动补全功能大大地提升了代码开发效率。 
最后,亲测前端用的js也可以通过这种方法实现自动补全。

0 0