Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for JSDoc Evangelism
Peter Vivo
Peter Vivo

Posted on • Edited on

     

JSDoc Evangelism

TL;DR

Working with a legacy codebase—something many of us can't avoid from time to time—led me to try JSDoc instead of TypeScript. I have to reveal the surprising truth!

First of all, let's clarify: JSDoc or TS simply means enhancing the developer experience (including later review, reuse, or understanding in any environment: git web page, random editors, Chrome, Firefox DevTools, Vim, cat, etc.).

The First Step

Just open your editor, write a proper comment, and test if JSDoc is working.

/** @typedef { 'JS' | 'JQuery' | 'TS' | 'JSDoc' } Phase; *//** * On typing ' editor will popup string list. * * @type {Phase} */constbadCase='React';// !!!! lint alert !!!!/** @type {Phase} */constgoodCase='JQuery';
Enter fullscreen modeExit fullscreen mode

JSDoc vs. TypeScript

In my experience, JSDoc can replace TypeScript in many scenarios, with the added bonus of interoperability between the two.

The biggest advantage of JSDoc, in my opinion, is that it uses a standard JS comment system, which means it won't break any JavaScript code and will run everywhere JS can run.

FeatureJSDocTypeScript
CompilationNo need to compile the codeMandatory to compile
DependencyWorks without any dependenciesRequires TypeScript as a dependency
NamespaceDoesn't interfere with types and other imports—you can use the same name for components and types in React, for exampleTypeScript names can conflict with other imports
ReworkNo need to change existing code, just add a commentSome parts of your code need to be rewritten in TS
Legacy CodeCan be used when transforming to TypeScript isn't an option—many legacy projects fall in this categoryRequires management approval to modify the project
Incremental AdoptionYou can use JSDoc where you need it—even in new commits only, easily searchable for future changesCan be introduced gradually but still requires modifying build systems
Future FlexibilityCan easily be translated to TypeScript later since they use similar under-the-hood mechanicsRequires work to revert back to JS if needed
Runtime ExpectationsSince it's just a comment, it's clear that it doesn't perform runtime type checks, just like TypeScriptTypeScript may give the impression of runtime type safety

JSDoc Editor Experience

I can write JSDoc in any editor, but it's rare for editors to fully understand it.

Node Module Experience

I also created an npm module, jsdoc-duck, as a JSDoc-coded module. This highlighted that without TypeScript, creating a JSDoc npm module isn't straightforward. Maybe if I spent more time figuring out Vite build parameters, a solution could be found. But the good news is that you don't need to use that module via npm—you can simply copy index.js to a local place, which avoids adding a new dependency and mitigates any risks if the module owner makes breaking changes.

Wormhole Between JSDoc and TypeScript

The good news is that TypeScript and JSDoc are compatible. JSDoc uses slightly different syntax, but you can use JSDoc module types in TypeScript projects and vice versa. Ultimately, the choice is yours.

Follow the Yellow Brick Road

VS Code is a great example of how well it can show your types in JSDoc code—in my opinion, it's surprisingly low-noise compared to TypeScript.

syntax highlight of JSDoc by VSCode

Bonus

VS-Code Snippets

"@type":{"prefix":["ty"],"body":["/** @type {$0} */"],"description":"jsdoc type"},"@typedef":{"prefix":["td"],"body":["/** @typedef {$1} Foo */"],"description":"jsdoc typedef"},
Enter fullscreen modeExit fullscreen mode

JSDoc-Duck Code Example

In this view, syntax highlighting doesn't help to understand the types. However, this short program is a good example of how JSDoc can use TypeScript's advanced features too.

import{useMemo,useReducer}from"react";/** * @template T - Payload Type * @typedef {T extends { type: infer U, payload?: infer P } ? { type: U, payload?: P } : never} ActionType *//** @template AM - Actions Map @typedef {{ [K in AM['type']]: K }} Labels *//** @template AM - Actions Map @typedef {{ [T in AM["type"]]: Extract<AM, { type: T }> extends { payload: infer P } ? (payload: P) => void : () => void }} Quack *//** * @template ST - State * @template AM - Actions Map * @typedef {(state: ST, action: AM) => ST} Reducer *//** * Factory function to create a typed action map. * @template AM - Actions Map * @param {Labels<AM>} labelsObject - The keys representing action labels. * @param {function} dispatch - The dispatch function for actions. * @return {Quack<AM>} The resulting typed action map. */exportconstquackFactory=(labelsObject,dispatch)=>Object.keys(labelsObject).reduce(/**     * @arg {Quack<AM>} acc     * @arg {keyof Labels<AM>} type     * @return {Quack<AM>}     */(acc,type)=>({...acc,[type]:(payload)=>{dispatch({type,payload});}}),{});/** * A factory hook to create a state and a typed dispatch functions\ * @exports useDuck * @template AM - Actions Map * @template ST - State Typer * @param {(st: ST, action: AM) => ST} reducer - The reducer function to manage the state. * @param {ST} initialState - The initial state value. * @return {[ST, Quack<AM>]} The current state and a map of action dispatch functions. */exportconstuseDuck=(reducer,initialState,labels)=>{const[state,dispatch]=useReducer(reducer,initialState);constquack=useMemo(()=>quackFactory(labels,dispatch),[dispatch,labels]);return([state,quack]);};
Enter fullscreen modeExit fullscreen mode

Happy coding! Borrowing, instead of adding dependencies, can simplify things.

Top comments(9)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
digitaldrreamer profile image
Abdullah Bashir
20y/o @ www.goremote.africa Fullstack Dev | Avid Reader | Loves Svelte(Kit) for it's simplicity | AI and IoT enthusiast
  • Email
  • Location
    Nigeria
  • Education
    Obafemi Awolowo University
  • Pronouns
    He/Him
  • Work
    Fullstack Developer
  • Joined

Nice. These are the reasons I left TS for JsDoc. Simple comments, no transpiling. Thanks.

Seems like English isn't your first language though. You could pass your post through an AI to fix your grammar next time.

CollapseExpand
 
pengeszikra profile image
Peter Vivo
Pipeline operator and touch bar fanatic from Hungary.God speed you!
  • Location
    Pomaz
  • Education
    streetwise
  • Work
    full stack developer at TCS
  • Joined

thx, I&AI will be fix the gramar

CollapseExpand
 
pengeszikra profile image
Peter Vivo
Pipeline operator and touch bar fanatic from Hungary.God speed you!
  • Location
    Pomaz
  • Education
    streetwise
  • Work
    full stack developer at TCS
  • Joined

fixed

CollapseExpand
 
trplx_gaming profile image
Gabriel Ibe
Experienced frontend web dev, game dev and intermediate Java programmer Always ready for a challenge 🙂
  • Pronouns
    He, Him
  • Joined

I'm not against anything that tries to improve upon JS, be it JSDoc or TS

For me personally coming from a strong statically typed language like Java, TS is way easier to transition to.

Well as for JSDoc, I hate comments and seeing that it's mandatory to improve my code, I don't even bother as far as TS does what an average statically typed language does and even more, it's my defacto productivity language 🤓

CollapseExpand
 
pengeszikra profile image
Peter Vivo
Pipeline operator and touch bar fanatic from Hungary.God speed you!
  • Location
    Pomaz
  • Education
    streetwise
  • Work
    full stack developer at TCS
  • Joined

I understund your point. My question is how fare need to go with a statically typed program in so dynamical domain as web development. If we just talking about pure JS or TS then it is great area to using type system. But when we are try to work DOM then even the simpledocument.querySelect return type will be rapidly diverge by type of given tag.

JSDoc technically equivalent with TS just the syntax is bit different, plus JSDoc don't force us.

CollapseExpand
 
trplx_gaming profile image
Gabriel Ibe
Experienced frontend web dev, game dev and intermediate Java programmer Always ready for a challenge 🙂
  • Pronouns
    He, Him
  • Joined

Been a while😅

I've been using TS for quite a while now and I'd say it's one of the most flexible compiled(transpiled) languages I've ever worked with!

I'm a Fullstack dev but more focus on backend because i prioritize functionality over design and when I use TS in the backend. It was a lil bit off-putting when I had to define the types of the request and response objects of a http server connection 😣

Now this is where the flexibility comes in, I was able to define it but customizing the type in my own way😅

CollapseExpand
 
brense profile image
Rense Bakker
I am a fullstack developer with experience designing and building responsive web apps. Proficient with ReactJS, Typescript, Javascript, HTML and CSS.
  • Joined

This is far more work than just relying on typescript type inference, which requires no extra work... You can add type inference with typescript to jsdoc, but then you're just using both where you could simply use one :B

CollapseExpand
 
pengeszikra profile image
Peter Vivo
Pipeline operator and touch bar fanatic from Hungary.God speed you!
  • Location
    Pomaz
  • Education
    streetwise
  • Work
    full stack developer at TCS
  • Joined

Yes if we import type from TS then we can use TS interface. But if we working on JSDoc then we don't have interface, for simplify everything is@typedef. I think that is even more simplify the process.

CollapseExpand
 
brense profile image
Rense Bakker
I am a fullstack developer with experience designing and building responsive web apps. Proficient with ReactJS, Typescript, Javascript, HTML and CSS.
  • Joined

I disagree, with Typescript it's much more simple, because you can use type inference. If you want to add that capability to jsdoc with @ts-check, you're just using both jsdoc and typescript and you can just eliminate the middleman (jsdoc).

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Pipeline operator and touch bar fanatic from Hungary.God speed you!
  • Location
    Pomaz
  • Education
    streetwise
  • Work
    full stack developer at TCS
  • Joined

More fromPeter Vivo

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp