JSDoc frequent patterns
Previously, I’ve wrote the post on Why use JSDoc. This time, I am going to dive into details and share several of the most frequent patterns that I find myself using.
Basic object
Use@typedef
@type
to describe the object. You may also leave optional comments.
Limited set of values
Mix set of values to create a new type.
Function
Use @param
@return
to add description to functions.
Promises
Use Promise<...>
to work promises.
Boolean logic with types
Sometimes its useful to mix your type. For example if you need a base type and several extensions.
Importing types
Use @import
to get @typedef
from another file
With 3-rd party types
Import types from 3-rd party libraries. Types are available when library uses typescript or JSDoc itself(almost always).
Module type
Whole module can be used as a type, by importing it inside @type {...}
. Works with both common-js-modules and es6-modules.
VSCode Support
Visual Studio Code — lightweight, simple yet complex modern editor. It has a great support of JSDoc by default. It validates your code in terms of defined types, analyze and help you to be more productive.