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

Visual Studio Code using JavaScript with a TypeScript File as Reference for Intellisense

我目前正在使用 VisualStudioCode 编写一个 nodejs 应用程序,我正在使用文档注释 link 将函数的参数 类 以便 IntelliSense 可以启动,但我遇到了一些当我想使用 classes/types 个模块时出现问题。

我目前的处理方式:

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

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

foobar 我现在可以看到所有可用的 attributes/functions 我可以拜访 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
}

但显然不是这样的

使用所谓的导入类型

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

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