Visual Studio Code使用typings拓展自动补全功能

3,551 阅读3分钟

转载地址 blog.csdn.net/liyijun4114… //转载只为个人记录

今天正巧在看vscode官方文档的时候,看到了介绍其使用typings拓展自动补全功能的介绍,同时查了一点其他资料,感觉用起来非常方便。由于脑子总是会清空一些不常用的东西,因此记在这里方便以后参考。

参考来源:

**官方介绍: ** code.visualstudio.com/Docs/runtim…

**gyzhao写的”Visual Studio Code 使用 Typings 实现智能提示功能”: ** www.cnblogs.com/IPrograming…

vscode的自动补全 vscode原本是只有es原生api有带自动补全的功能

**但是如果使用node.js或者其require进去的相关函数就比较可怜了。 **

除了process模块,path、fs、__dirname等也同样没有自动补全。似乎在低版本下使用__dirname还会提示语法错误。

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

官方描述以及使用说明 www.npmjs.com/package/typ…

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

配置jsconfig.json **在使用typings之前,需要在vscode里面配置一下名为jsconfig.json的文件。配置方法很简单,随便选中一个js文件,vscode右下角会弹出一个绿色的小灯泡,如图所示: **

**点击进去,顶部会提示 ** **“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" ] } 所有需要的参数都会帮我们设置好。此时我使用的是vscode v1.2.0生成的jsconfig,低版本自动生成的配置可能会比里面的少一点,但不影响。

安装typings 使用npm全局安装typings

npm install -g typings

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

typings install dt~node --global

**其中”dt~”为使用DefinitelyTyped类型定义的意思,vscode可以识别这种定义。 之后可以看到在项目目录下出来了新的文件夹“typings” **

**现在输入process,自动地补全出来了~window下可能需要重启下vscode才能看到自动补全的效果。 **

express、lodash等的自动补全 类似地,可以使用以上的方法,实现其他模块的自动补全。 typings install dt~express --global typings install dt~lodash --global //vscode官方文档上express的typings使用的是 typings install dt~express dt~serve-static dt~express-serve-static-core --global 关于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 --save typings install npm~bluebird --save

** 使用该文件typings/index.d.ts (在tsconfig.json文件使用或者用 /// 定义). ** cat typings/index.d.ts