JSdoc for file stream

Viewed 831

I am creating a stream as :-

var stream = fs.createWriteStream("filepath");

Now, I am passing this as argument to a function. When writing the JSdoc for this method, I am not sure what would be its type. So, can someone tell me what would be its type in JSdoc?

2 Answers

This is kind of a late answer but I recently encountered a similar problem.

The object returned by fs.createWriteStream is of type WriteStream as described by the API Reference of Node

You can use import in jsDoc @type as follows.

const fs = require('fs');

/**
 * Some definition explaining what this constant is
 * @constant
 * @type {import('fs').WriteStream}
 */
const stream = fs.createWriteStream("filepath");

I found about it here.

In my case, I was reusing the WriteStream file multiple times so I used a combination of @typedef and @type

/**
 * @typedef {import('fs').WriteStream} WriteStream
 */
const fs = require('fs');

/**
 * Some definition explaining what this constant is
 * @constant
 * @type WriteStream
 */
const stream = fs.createWriteStream("filepath");

I don't think JSDocs has a specific type for this node api. However, you can put the class as type and I think it will be equally valid. According to node documentation the class is stream.Writable, so you can use that. Node Docs

Related