[英]Visual Studio Code using JavaScript with a TypeScript File as Reference for Intellisense
I am currently using VisualStudioCode to write a nodejs app and I'm using the doc comments to link the parameters of functions to classes so the IntelliSense can kick in, but I encountered some problems when I want to use classes/types of modules. 我目前正在使用VisualStudioCode编写一个nodejs应用程序,并且正在使用doc注释将函数的参数链接到类,以便可以使用IntelliSense,但是当我要使用模块的类/类型时遇到了一些问题。
How I currently handle things: 我目前的处理方式:
class Foo{
constructor(){
this.bar = "value"
}
}
/**
* @param {Foo} parameter
*/
function foobar(parameter){
parameter.bar.charAt(0); //parameter.bar now with IntelliSense
}
In foobar
I can now see all available attributes/functions I can call on bar
. 现在,在
foobar
我可以看到可以在bar
调用的所有可用属性/函数。
Now, if somewhere in a node-module a TypeScript file: 现在,如果在节点模块中的某个位置有TypeScript文件:
declare module 'coollib' {
namespace lib {
type CoolLibType = {
begin?: string | number | Date;
liveBuffer?: number;
requestOptions?: {};
highWaterMark?: number;
lang?: string;
}
}
export = lib;
}
How can I reference this? 我该如何参考呢? I would like to do something like this in my JavaScript file:
我想在我的JavaScript文件中执行以下操作:
const CoolLibType = require('coollib')
/**
* @param {CoolLibType} obj
*/
function foobar(obj){
obj.lang.charAt(0); //with cool IntelliSense
}
But it obviously doesn't work like this. 但这显然行不通。
Use what's called an import type . 使用所谓的导入类型 。
/**
* @param {typeof import('coollib')} obj
*/
function foobar(obj) {
obj.lang.charAt(0); //with cool IntelliSense
}
Read more about import types in TypeScript 2.9 release notes . 在TypeScript 2.9发行说明中阅读有关导入类型的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.