Visual Studio代码使用JavaScript和TypeScript文件作为Intellisense的参考

问题描述 投票:0回答:1

我目前正在使用VisualStudioCode编写nodejs应用程序,我正在使用doc注释将函数的参数链接到类,以便IntelliSense可以启动,但是当我想使用类/类型的模块时遇到了一些问题。

我目前如何处理事情:

class Foo{
    constructor(){
      this.bar = "value"
    }
}

/**
 * @param {Foo} parameter 
 */
function foobar(parameter){
  parameter.bar.charAt(0); //parameter.bar now with IntelliSense
}

foobar中,我现在可以看到我可以在bar上调用的所有可用属性/函数。

现在,如果节点模块中的某个位置是TypeScript文件:

declare module 'coollib' {
  namespace lib {
    type CoolLibType = {
      begin?: string | number | Date;
      liveBuffer?: number;
      requestOptions?: {};
      highWaterMark?: number;
      lang?: string;
    }
  }
  export = lib;
}

我怎么能参考这个?我想在我的JavaScript文件中做这样的事情:

const CoolLibType = require('coollib')
/**
 * @param {CoolLibType} obj 
 */
function foobar(obj){
  obj.lang.charAt(0); //with cool IntelliSense
}

但它显然不会像这样工作。

javascript node.js typescript visual-studio-code intellisense
1个回答
0
投票

使用所谓的导入类型。

/**
 * @param {typeof import('coollib')} obj
 */
function foobar(obj) {
  obj.lang.charAt(0); //with cool IntelliSense
}

阅读有关TypeScript 2.9 release notes中导入类型的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.