惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
Google DeepMind News
Google DeepMind News
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
Webroot Blog
Webroot Blog
Stack Overflow Blog
Stack Overflow Blog
T
The Blog of Author Tim Ferriss
D
Docker
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Know Your Adversary
Know Your Adversary
A
About on SuperTechFans
U
Unit 42
NISL@THU
NISL@THU
M
MIT News - Artificial intelligence
T
The Exploit Database - CXSecurity.com
K
Kaspersky official blog
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hacker News - Newest:
Hacker News - Newest: "LLM"
Engineering at Meta
Engineering at Meta
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 【当耐特】
WordPress大学
WordPress大学
Attack and Defense Labs
Attack and Defense Labs
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
小众软件
小众软件
G
Google Developers Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园 - 司徒正美
腾讯CDC
大猫的无限游戏
大猫的无限游戏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Security @ Cisco Blogs
aimingoo的专栏
aimingoo的专栏
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
N
News and Events Feed by Topic
Google Online Security Blog
Google Online Security Blog
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog

Deno

Deno 2.8 | Deno Claw Patrol: an open-source security firewall for agents | Deno Fresh 2.3: Zero JS by default, View Transitions, and Temporal support | Deno Deno 2.7: Temporal API, Windows ARM, and npm overrides | Deno Build a dinosaur runner game with Deno, pt. 6 | Deno Build a dinosaur runner game with Deno, pt. 5 | Deno Deno Deploy is Generally Available | Deno Introducing Deno Sandbox | Deno Build a dinosaur runner game with Deno, pt. 4 | Deno Build a dinosaur runner game with Deno, pt. 3 | Deno Build a dinosaur runner game with Deno, pt. 2 | Deno React / Next.js Denial-of-Service Vulnerability: Deno Deploy users protected | Deno Deno 2.6: dx is the new npx | Deno Build a dinosaur runner game with Deno, pt. 1 | Deno React Server Functions / Next.js Vulnerability: Deno Deploy users protected | Deno My highlights from the new Deno Deploy | Deno Deno's Other Open Source Projects | Deno How Deno protects against npm exploits | Deno Help Us Raise $200k to Free JavaScript from Oracle | Deno Deno 2.5: Permissions in the config file | Deno Fresh 2.0 Graduates to Beta, Adds Vite Support | Deno Deno 2.4: deno bundle is back | Deno JavaScript™ Trademark Update | Deno What's coming to JavaScript | Deno A brief history of JavaScript | Deno Reports of Deno's Demise Have Been Greatly Exaggerated | Deno An Update on Fresh | Deno How Plaid migrated 100 services to a new database platform 5x faster with Deno | Deno Deno 2.3: Improved deno compile, local npm packages, and more | Deno Add JSR packages with pnpm and Yarn | Deno Zero-config Debugging with Deno and OpenTelemetry | Deno Exploring Art with TypeScript, Jupyter, Polars, and Observable Plot | Deno Deno v Oracle Update 3: Fighting the JavaScript Trademark | Deno Build a custom RAG AI agent in TypeScript and Jupyter | Deno How to get deep traces in your Node.js backend with OTel and Deno | Deno toranoana.deno #20 登録受付中(2025年3月14日) | Deno Node just added TypeScript support. What does that mean for Deno? | Deno The Dino 🦕, the Llama 🦙, and the Whale 🐋 | Deno Publish a lint rule, get a prize | Deno Deno 2.2: OpenTelemetry, Lint Plugins, node:sqlite | Deno If you're not using npm specifiers, you're doing it wrong | Deno How Deno's documentation is evolving | Deno Oracle justified its JavaScript trademark with Node.js—now it wants that ignored | Deno Introducing the JSR open governance board | Deno Intro to Wasm in Deno | Deno Announcing OpenAI on JSR | Deno Deno in 2024 | Deno Goodbye WinterCG, welcome WinterTC | Deno Build a SolidJS app with Deno | Deno Run your Next.js SSR app on Deno Deploy | Deno Solve Advent of Code 2024 with Deno and Win Prizes! | Deno Deno v. Oracle: Canceling the JavaScript Trademark | Deno Deno 2.1: Wasm Imports and other enhancements | Deno Build a Typesafe API with tRPC and Deno | Deno Self-contained Executable Programs with Deno Compile | Deno Build a Database App with Drizzle ORM and Deno | Deno Introducing your new JavaScript package manager: Deno | Deno Announcing Growthbook on JSR | Deno Build an Astro site with Deno | Deno How to convert CommonJS to ESM | Deno Announcing Deno 2 | Deno The Final Touches: What’s New In v2.0.0-rc.10 | Deno Announcing Stable V8 Bindings for Rust | Deno Deno 2.0 Release Candidate | Deno Secure, efficient private npm registries with Cloudsmith and Deno | Deno Painting the Plane as We Fly It: Designing JSR | Deno Introducing Web Cache API support on Deno Deploy | Deno Deno 1.46: The Last 1.x Release | Deno Protect your cloud spend with new Deno Deploy spend limits | Deno What we got wrong about HTTP imports | Deno Benchmarking AWS Lambda Cold Starts Across JavaScript Runtimes | Deno Announcing Supabase on JSR | Deno Deno 1.45: Workspace and Monorepo Support | Deno Introducing KV Backup for Deno Subhosting | Deno A Gentle Intro to TypeScript | Deno Announcing Hono on JSR | Deno How We Made the Deno Language Server Ten Times Faster | Deno How the Guardian uses Deno to audit accessibility and performance across their 2.7 million articles | Deno Introducing More Flexible Domain Association for Deno Subhosting | Deno The stabilization process of the Standard Library has begun | Deno Deno 1.44: Private npm registries, improved Node.js compat, and performance boosts | Deno How we built a secure, performant, multi-tenant cloud platform to run untrusted code | Deno The Deno Standard Library is now available on JSR | Deno Your Low Code Solution Needs an Escape Hatch | Deno Deno 1.43: Improved Language Server performance | Deno How Slack used Deno to save months of engineering effort in launching their new platform | Deno JSR Is Not Another Package Manager | Deno Announcing the Hookdeck SDK on JSR | Deno Announcing the Neon Serverless Driver on JSR | Deno An intro to TSConfig for JavaScript Developers | Deno How we built JSR | Deno How Netlify used Deno Subhosting to build a successful edge functions product | Deno Introducing Simpler Project Creation in Deno Deploy | Deno Deno 1.42: Better dependency management with JSR | Deno Introducing deployctl, the command line interface for Deno Deploy | Deno Introducing JSR - the JavaScript Registry | Deno How to add Monaco to a Next.js app and securely run untrusted user code | Deno Survey Results and Roadmap | Deno Deno 1.41: smaller deno compile binaries | Deno Webhooks suck, but here are alternatives | Deno
How to document your JavaScript package | Deno
2024-05-11 · via Deno

Creating and publishing open source packages is a great way to contribute to the ecosystem and community. You made something cool and want people to use it. But simply publishing your module to a registry and crossing your fingers won’t get users. Helping your users become successful with your package means not only writing concise, descriptive documentation, but also ensuring your users can access the documentation within their workflows (e.g. in VSCode) to save them time.

Thanks to JSDoc it’s easy to write documentation that is coupled with your code and can be consumed by users in a variety of formats. When combined with a modern publishing flow like JSR, you can easily create comprehensive documentation for your package that not only fits within your workflow, but also integrates directly in the tools your users consume your package with. This blog post aims to cover best practices when writing JSDoc-style comments to get your users up and running as quickly as possible:

  • Why JSDoc?
  • A brief intro to JSDoc
  • Provide good type information
  • Tags, tags, tags
  • Add examples
  • But what should I document?
  • Use markdown
  • Link internally
  • Keep JSDoc up-to-date
  • Audit your JSDoc
  • What’s next

Why JSDoc?

While a good README answers “why should I use your package?”, good documentation should answer “how can I use your package?”. Users browsing your documentation have a problem they need to solve, and your documentation should provide them with the answer in the fewest clicks and keyboard taps.

JSDoc is a great way to write reference documentation that is coupled with the code itself and can be consumed by users in a variety of formats, such as HTML, markdown, JSON, or in their IDE or text editor. Here is a quick diagram of an example JSDoc-style comment and how it appears as documentation in various mediums:

JSDoc diagram on JSR

When you write JSDoc-style comments in your code and publish to JSR, it will appear formatted on your package’s documentation page on JSR, VSCode tooltips and auto-complete, and in `deno doc` output.

Writing good JSDoc can improve the success of your package. Before we dive into some best practices, here’s a brief high-level introduction to JSDoc.

A brief intro to JSDoc

JSDoc turns your comments in your code into a documentation object that can be rendered and displayed in a variety of formats.

JSDoc comments are any block comments that begin with /** and end with */ that precede a block of code. Here’s an example:


function sum(value1, value2) {
  return value1 + value2;
}

This JSDoc will then appear as a tooltip in your IDE:

Example of JSDoc with sum function

JSDoc comments can span multiple lines. Each line should start with * and should be indented by one space.


function sum(value1, value2) {
  return value1 + value2;
}

The first paragraph of a JSDoc comment is the most important. It is a summary of the symbol and is shown in tooltips, auto-completions in your editor, and is indexed by search. The first paragraph should be a concise description of the symbol, and should be written in a way that helps users quickly understand what this function does.

For example, don’t write:


function replaceSpacesWithUnderscores(value) {
  return value.replace(/ /g, "_");
}

Instead, concisely describe what the function does:


function replaceSpacesWithUnderscores(value) {
  return value.replace(/ /g, "_");
}

Additional information like the implementation details, caveats, or examples should be added in subsequent paragraphs. Because JSDoc supports markdown, you can even use headings to separate different sections.

Simple and concise summaries help users quickly filter through a list of symbols during auto-complete and find the one they need. Once they’ve found the symbol, they can read through the rest to learn about the details.

Provide good type information

After the succinct descriptive summary, it’s important to provide good type information for the symbols you are exposing in your package. This serves two main purposes:

  1. It allows auto-completion on parameters and return values in your editor, because the editor knows the types of the parameters and return values.
  2. It helps users quickly filter through the list of functions to find the one they need. For instance, if they are looking for a function that combines two strings, they can filter out functions that don’t take two strings as parameters.

Here, we’ll use TypeScript to add type information. TypeScript, one of the fastest growing programming languages, is a strongly typed language built on JavaScript that enhances code quality and maintainability, while improving developer productivity.


export function sum(value1: number, value2: number): number {
  return value1 + value2;
}

In your editor, you’ll see the type information for the parameters and return value when you hover over the function:

Getting type information for parameters and return value in VSCode on hover

When a user types sum( in their editor, they’ll see the type information for the parameters:

Getting type information in the parameters on VSCode

On the return value, you can immediately get completion for methods on the returned number:

Getting completion options for number type in VSCode

JSDoc supports a variety of tags that can be used to provide additional information about your symbols, such as @param for parameters, @returns for the return value, or @typeParam for type parameters. Here’s an example of a function with type information and tags:


declare function find(value: string, needle: string): number;

In your editor, you’ll see the type information for the parameters and return value, as well as the additional information provided by the tags:

Seeing param info in the hover on VSCode

On JSR, the tags are rendered in HTML. Here’s an example of the @param and @return tags in the JSDoc of the move function in deno_std/fs:

Seeing param and return value information on JSR

Add examples to JSDoc

Examples are another great way to help users quickly understand how to use your library. This is especially useful for functions that have complex behavior or many parameters. Examples can be added to your JSDoc comments using the @example tag:


declare function find(value: string, needle: string): number;

The best examples are concise and demonstrate the most common use cases of your function. They should be easy to understand and can be copied and pasted into a project.

You can even provide multiple examples if there are multiple use cases worth mentioning. Here’s an example of how multiple examples appear on JSR from the move function of deno_std/fs:

Note the text immediately following @example serves as the title, and the text beneath the example becomes its description on JSR:

How examples appear on JSR

But what should I document?

You should document every symbol your package exports, including functions, classes, interfaces, and type aliases.

This extends beyond just one JSDoc comment per symbol. For classes and interfaces for example, you should document the symbol itself, each method or property on it, including constructors. Here’s an example of an Oak interface with JSDoc comments on its properties:


export interface ListenOptionsBase {
  
  port?: number;
  
  hostname?: string;
  secure?: false;
  
  signal?: AbortSignal;
}

This is how the JSDoc comments on properties appear on JSR:

How property documentation appears on JSR

If your package consists of multiple modules, adding a JSDoc comment to the top of each module file with the @module tag will be helpful. This module comment should include a description and examples of how to use its exported symbols.

Here’s an example of @module in Oak’s application.ts file:

In JSR, the first paragraph becomes the description beneath the modules on the main docs page of your package:

Oak module description

Note the main doc page only includes the first paragraph. The subsequent JSDoc comment appears when you click through to the module page:

Oak's application module description

Use markdown for a better documentation experience

Using markdown in JSDoc lets you organize your documentation in a more readable and engaging way. This can help you create documentation that is easier to understand, and allows you to link to external resources or other parts of your documentation using links.

Some useful markdown features you can use in your JSDoc comments include:

  • # my heading for section headings
  • - hello world for bullet points
  • **important** for bold
  • _noteworthy_ for italic
  • > quote for block quotes
  • [foo](https://example.com) for links
  • `console.log("foo")` for inline code snippets

On JSR, you can also use [!IMPORTANT] to highlight important information in your documentation that you want to draw attention to.

This module-level JSDoc comment will appear at the top level in JSR as such:

Markdown in JSDoc example

Sometimes, your documentation refers to another symbol within your package. To make it easy for your users to navigate throughout your docs, you can link within your documentation using the @link , @linkcode , and @linkplain tags. These tags accept a name path or URL, from which it generates an HTML anchor element. Here’s an example:


export interface StyleOptions {
  
  color: "black" | "red" | "green";
  
  bold: boolean;
  
  italic: boolean;
}


declare function print(message: string, options: StyleOptions): void;

In VSCode, the hover tooltip now includes clickable links that will take you directly to the code where that symbol is defined:

Example of linkcode in VSCode hover tooltip

Here’s an example of how @linkcode appears in JSR. In the JSDoc for Oak’s serve function, it references Application, which is becomes a clickable link on JSR:

Example of linkcode in JSR

You can also reference built-in JavaScript objects, like ArrayBuffer, and JSR will automatically link to the relevant MDN documentation.

Keep JSDoc up-to-date with code changes

One benefit about using JSDoc is that writing documentation in the comments happens at the same time as writing the code. That means anytime we need to make a change to a function, interface, or module, we can make the necessary change to the JSDoc with minimal context switching costs.

But how does one make sure that the docs in the comments are updated? Starting with docs, á la docs-driven-development, can help you spec out and reason about the requirements before writing a single line of code. Sometimes, this means catching potential problems earlier and saving time from having to re-write code. (For a more macro-level approach to this, check out “Readme-driven development”.)

If you’ve included code examples in your documentation, you can type check them from the command line with deno test --doc. This is a helpful tool to ensure that your examples within your documentation are up-to-date and working.

For example, building on our sum function from earlier, let’s add an example with a code snippet:


export function sum(value1: number, value2: number): number {
  return value1 + value2;
}

Then, we can check the code in this example block by running deno test --doc :

deno test --doc
Check file:/
error: TS2345 [ERROR]: Argument of type 'string' is not assignable to parameter of type 'number'.
const finalValue = sum(1, "this is a string");
                          ~~~~~~~
    at file:/

Whoops! After we fix the type error in the code in our documentation:

deno test --doc
Check file:/

ok | 0 passed | 0 failed (0ms)

This offers a quick way to type check your code examples in your documentation before publishing.

Audit your JSDoc

If you’re publishing to JSR, it will handle all of the formatting and generation of documentation based off your JSDoc-style comments. However, if you’re interested in using your tooling to audit or test what the JSDoc comment output looks like, here are some suggestions.

  • deno doc <file>: This Deno command will print the JSDoc documentation for each of thefile’s exported members. This command also accepts an--htmlflag that generates a static site with documentation, and a --json flag to generate JSON output that you can display yourself.
  • deno doc --lint`: This command will check for problems, such as missing return type or missing JSDoc comment on a public type. These lints help you write better documentation and catch potential issues before you publish.
  • deno test --doc`: We mentioned this command earlier in this post, but this allows you to easily type check your documentation examples.
  • jsdoc <directory>: JSDoc’s own CLI can generate a static documentation site with its default template, and offers a variety of configuration option flags. If the default template is a bit boring, there are others such as docdash, which provides hierarchical navigation and syntax highlighting.

What’s next?

Writing good JSDocs for your JavaScript package is critical to its success. Let’s recap the best practices:

  • Write a concise summary: The first paragraph of your JSDoc comment should be a concise description of the symbol that helps users quickly understand what it does.
  • Provide good type information: Type information helps users quickly filter through the list of functions and find the one they need.
  • Use tags: Tags like @param, @returns, and @typeParam provide more information about specific parts of your function or class.
  • Add examples: Examples help users quickly understand how to use your library.
  • Document everything: Document every symbol you are exposing in your package, including whole modules if you expose multiple modules.
  • Link internally: Use @link, @linkcode, and @linkplain to link to other parts of your documentation to help users navigate your docs.
  • Test your documentation: Use deno test --doc to type check your documentation examples before publishing, and deno doc --lint to check for issues in your JSDoc comments.

By following these best practices, you can create comprehensive documentation for your package that helps users get up and running with your package as quickly as possible.

🚨️ Read more about JSR 🚨️