Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSdoc for file stream

Tags:

node.js

jsdoc

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?

like image 739
Chacha Avatar asked Nov 06 '17 19:11

Chacha


People also ask

What is JSDoc used for?

JSDoc is a markup language used to annotate JavaScript source code files. Using comments containing JSDoc, programmers can add documentation describing the application programming interface of the code they're creating.

Why is JSDoc important?

JSDoc's purpose is to document the API of your JavaScript application or library. It is assumed that you will want to document things like modules, namespaces, classes, methods, method parameters, and so on. JSDoc comments should generally be placed immediately before the code being documented.

Can I use JSDoc in TypeScript?

You can use most JSDoc type syntax and any TypeScript syntax, from the most basic like string to the most advanced, like conditional types.


1 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");
like image 55
Aayush Sharma Avatar answered Sep 28 '22 05:09

Aayush Sharma