Manu Artero
Manu Artero

Reputation: 10263

VSCode - Reference a type of a module from JS

VisualStudioCode

Using Visual Studio Code for JS programming I can access some features from typescript; since the editor will parse all .d.ts files around, it'll help me with variable types. For example it does recognize the following:

any.js

/** 
 * @param {string} s
 * @return {Promise<Person>}
 */
function foo(s){ ... }

foo('Jhon').then((p) => p.name )

index.d.ts

interface Person { 
  name: string 
  surname: string
}

Now, I want to access types (interfaces, classes... whatever) declared in node.d.ts declaration file; for example it declares the module stream which declares the Readable interface.

I'm looking for something like this:

const stream = require('stream')

/**
 * @param {stream.Readable} stream
 */
function goo(stream) { ... }

But that does not work.I've tried with:

Upvotes: 1

Views: 337

Answers (1)

Matt Bierner
Matt Bierner

Reputation: 65513

As of VS Code 1.18, this is a limitation when using require with JSDoc types. I've opened this issue to track this specifically

Some possible workarounds:

Use import:

import * as stream from 'stream'

/**
 * @param {stream.Readable} stream
 */
function goo(stream) { ... }

or import Readable explicitly:

const stream = require('stream')
const {Readable} = require('stream')

/**
 * @param {Readable} stream
 */
function goo(stream) { ... }

https://github.com/Microsoft/TypeScript/issues/14377 also tracks allowing you to specify module imports in jsdocs directly.

Upvotes: 2

Related Questions