jsdoc
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseJSDoc - Quick Reference
JSDoc - 快速参考
When NOT to Use This Skill
不适用本技能的场景
- TypeScript projects - Use the skill for TSDoc and TypeScript-specific patterns
jsdoc-tsdoc - JSDoc generation config - This is for writing JSDoc comments, not configuring the tool
- Complex type systems - TypeScript provides better type checking than JSDoc annotations
- API documentation hosting - Use documentation platforms (TypeDoc, Docusaurus, etc.)
Deep Knowledge: Usewith technology:mcp__documentation__fetch_docsfor comprehensive tag reference, type expressions, and generation options.jsdoc
- TypeScript项目 - 针对TSDoc和TypeScript特定模式,请使用技能
jsdoc-tsdoc - JSDoc生成配置 - 本技能用于编写JSDoc注释,而非配置工具
- 复杂类型系统 - TypeScript提供的类型检查比JSDoc注解更完善
- API文档托管 - 使用文档平台(TypeDoc、Docusaurus等)
深度知识:如需全面的标签参考、类型表达式和生成选项,请使用工具,指定技术为mcp__documentation__fetch_docs。jsdoc
Pattern Essenziali
核心模式
Function Documentation
函数文档
javascript
/**
* Calculates the sum of two numbers.
* @param {number} a - The first number
* @param {number} b - The second number
* @returns {number} The sum of a and b
* @example
* const result = add(2, 3); // 5
*/
function add(a, b) {
return a + b;
}javascript
/**
* Calculates the sum of two numbers.
* @param {number} a - The first number
* @param {number} b - The second number
* @returns {number} The sum of a and b
* @example
* const result = add(2, 3); // 5
*/
function add(a, b) {
return a + b;
}Object Types
对象类型
javascript
/**
* @typedef {Object} User
* @property {string} id - Unique identifier
* @property {string} name - Full name
* @property {string} email - Email address
* @property {boolean} [active=true] - Optional, defaults to true
*/
/**
* Creates a new user.
* @param {User} userData - The user data
* @returns {Promise<User>} The created user
*/
async function createUser(userData) {
// ...
}javascript
/**
* @typedef {Object} User
* @property {string} id - Unique identifier
* @property {string} name - Full name
* @property {string} email - Email address
* @property {boolean} [active=true] - Optional, defaults to true
*/
/**
* Creates a new user.
* @param {User} userData - The user data
* @returns {Promise<User>} The created user
*/
async function createUser(userData) {
// ...
}Class Documentation
类文档
javascript
/**
* Represents a user in the system.
* @class
*/
class User {
/**
* Creates a User instance.
* @param {string} name - User's name
* @param {string} email - User's email
*/
constructor(name, email) {
/** @type {string} */
this.name = name;
/** @type {string} */
this.email = email;
}
/**
* Gets the user's display name.
* @returns {string} Display name
*/
getDisplayName() {
return this.name;
}
}javascript
/**
* Represents a user in the system.
* @class
*/
class User {
/**
* Creates a User instance.
* @param {string} name - User's name
* @param {string} email - User's email
*/
constructor(name, email) {
/** @type {string} */
this.name = name;
/** @type {string} */
this.email = email;
}
/**
* Gets the user's display name.
* @returns {string} Display name
*/
getDisplayName() {
return this.name;
}
}Callback Types
回调类型
javascript
/**
* @callback FetchCallback
* @param {Error|null} error - Error if failed
* @param {Object} data - Response data
*/
/**
* Fetches data from the API.
* @param {string} url - The URL to fetch
* @param {FetchCallback} callback - Called with the result
*/
function fetchData(url, callback) {
// ...
}javascript
/**
* @callback FetchCallback
* @param {Error|null} error - Error if failed
* @param {Object} data - Response data
*/
/**
* Fetches data from the API.
* @param {string} url - The URL to fetch
* @param {FetchCallback} callback - Called with the result
*/
function fetchData(url, callback) {
// ...
}Generic Types
泛型类型
javascript
/**
* @template T
* @param {T[]} array - Array of items
* @returns {T|undefined} First item or undefined
*/
function first(array) {
return array[0];
}javascript
/**
* @template T
* @param {T[]} array - Array of items
* @returns {T|undefined} First item or undefined
*/
function first(array) {
return array[0];
}Module Documentation
模块文档
javascript
/**
* @module utils/string
* @description String utility functions
*/
/**
* Capitalizes the first letter.
* @param {string} str - Input string
* @returns {string} Capitalized string
*/
export function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}javascript
/**
* @module utils/string
* @description String utility functions
*/
/**
* Capitalizes the first letter.
* @param {string} str - Input string
* @returns {string} Capitalized string
*/
export function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}Anti-Patterns
反模式
| Anti-Pattern | Why It's Wrong | Correct Approach |
|---|---|---|
| No documentation at all | No IDE hints, hard to use API | Document at least public functions and complex logic |
| Copying implementation as comment | Adds no value, becomes outdated | Describe behavior, not implementation |
Missing | No type checking or autocomplete | Always specify parameter types |
| Outdated documentation | Misleading, worse than none | Update docs when changing code |
| Over-documenting obvious code | Clutters code, maintenance burden | Document complex logic, skip trivial getters/setters |
| Using wrong tag for purpose | Confuses tools, incorrect types | Use |
| Not importing types | Broken type references | Use |
| Duplicate type info in TypeScript | Redundant, desynchronizes | Let TypeScript types speak for themselves |
| 反模式 | 问题所在 | 正确做法 |
|---|---|---|
| 完全不写文档 | 无IDE提示,API难以使用 | 至少对公共函数和复杂逻辑编写文档 |
| 复制实现代码作为注释 | 无实际价值,易过时 | 描述行为而非实现细节 |
缺少 | 无类型检查或自动补全 | 始终指定参数类型 |
| 文档过时 | 误导使用者,比无文档更糟 | 修改代码时同步更新文档 |
| 过度注释明显代码 | 使代码杂乱,增加维护负担 | 仅注释复杂逻辑,跳过琐碎的getter/setter |
| 使用错误标签 | 混淆工具,导致类型错误 | 使用 |
| 未导入类型 | 类型引用失效 | 使用 |
| 在TypeScript中重复类型信息 | 冗余,易不同步 | 让TypeScript类型自行说明 |
Quick Troubleshooting
快速故障排查
| Issue | Diagnosis | Solution |
|---|---|---|
| IDE not showing type hints | JSDoc comments malformed or incomplete | Ensure |
| Type errors in VS Code | Incorrect JSDoc type syntax | Use TypeScript-style types: |
| Broken type imports | Wrong import path in | Use |
| Generic types not working | Improper | Declare |
| Documentation not generated | JSDoc tool not configured | Run |
| Optional params showing as required | Missing | Use |
| Types not recognized | Typo or undeclared typedef | Define types with |
| Return type ignored | Missing | Always document return value with |
| 问题 | 诊断 | 解决方案 |
|---|---|---|
| IDE不显示类型提示 | JSDoc注释格式错误或不完整 | 确保存在 |
| VS Code中出现类型错误 | JSDoc类型语法不正确 | 使用TypeScript风格的类型: |
| 类型导入失效 | | 使用 |
| 泛型类型无法正常工作 | | 在参数前声明 |
| 文档未生成 | JSDoc工具未配置 | 使用正确配置运行 |
| 可选参数显示为必填 | 参数名称缺少 | 使用 |
| 类型不被识别 | 拼写错误或未声明类型定义 | 使用 |
| 返回类型被忽略 | 缺少 | 始终使用 |
Related Skills
相关技能
- TSDoc/JSDoc
- TypeScript
- TSDoc/JSDoc
- TypeScript