
使用方式
关于
创建 Vue 3 组件,构建原生 JS Composable,配置 Vite 项目,仅使用 JavaScript(无 TypeScript)设置路由和状态管理。生成带 @typedef、@param 和 @returns 注解的 JSDoc 类型代码,无需 TS 编译器即可实现完整类型覆盖。
Vue 专家(JavaScript)
使用 JavaScript 和 JSDoc 类型注解(而非 TypeScript)构建 Vue 3 应用的高级 Vue 专家。
核心工作流程
- 设计架构 — 规划组件结构和组合式函数,使用 JSDoc 类型注解
- 实现 — 使用
<script setup>(无lang="ts")构建,需要时使用.mjs模块 - 注解 — 添加全面的 JSDoc 注释(
@typedef、@param、@returns、@type)实现完整类型覆盖;然后运行带 JSDoc 插件的 ESLint(eslint-plugin-jsdoc)验证覆盖率 — 修复所有缺失或格式错误的注解后再继续 - 测试 — 使用 JavaScript 文件通过 Vitest 验证;确认所有公共 API 的 JSDoc 覆盖率;如果测试失败,重新检查相关组合式函数或组件,修正逻辑或注解,重新运行直到套件通过
参考指南
根据上下文加载详细指导:
| 主题 | 参考 | 加载时机 |
|------|------|----------|
| JSDoc 类型 | references/jsdoc-typing.md | JSDoc 类型、@typedef、@param、类型提示 |
| 组合式函数 | references/composables-patterns.md | 自定义组合式函数、ref、reactive、生命周期钩子 |
| 组件 | references/component-architecture.md | props、emits、slots、provide/inject |
| 状态 | references/state-management.md | Pinia、store、响应式状态 |
| 测试 | references/testing-patterns.md | Vitest、组件测试、模拟 |
代码模式
带 JSDoc 类型的 props 和 emits 的组件
<script setup>
/**
* @typedef {Object} UserCardProps
* @property {string} name - Display name of the user
* @property {number} age - User's age
* @property {boolean} [isAdmin=false] - Whether the user has admin rights
*/
/** @type {UserCardProps} */
const props = defineProps({
name: { type: String, required: true },
age: { type: Number, required: true },
isAdmin: { type: Boolean, default: false },
})
/**
* @typedef {Object} UserCardEmits
* @property {(id: string) => void} select - Emitted when the card is selected
*/
const emit = defineEmits(['select'])
/** @param {string} id */
function handleSelect(id) {
emit('select', id)
}
</script>
<template>
<div @click="handleSelect(props.name)">
{{ props.name }} ({{ props.age }})
</div>
</template>
带 @typedef、@param 和 @returns 的组合式函数
// composables/useCounter.mjs
import { ref, computed } from 'vue'
/**
* @typedef {Object} CounterState
* @property {import('vue').Ref<number>} count - Reactive count value
* @property {import('vue').ComputedRef<boolean>} isPositive - True when count > 0
* @property {() => void} increment - Increases count by step
* @property {() => void} reset - Resets count to initial value
*/
/**
* Composable for a simple counter with configurable step.
* @param {number} [initial=0] - Starting value
* @param {number} [step=1] - Amount to increment per call
* @returns {CounterState}
*/
export function useCounter(initial = 0, step = 1) {
/** @type {import('vue').Ref<number>} */
const count = ref(initial)
const isPositive = computed(() => count.value > 0)
function increment() {
count.value += step
}
function reset() {
count.value = initial
}
return { count, isPositive, increment, reset }
}
跨文件使用的复杂对象 @typedef
// types/user.mjs
/**
* @typedef {Object} User
* @property {string} id - UUID
* @property {string} name - Full display name
* @property {string} email - Contact email
* @property {'admin'|'viewer'} role - Access level
*/
// Import in other files with:
// /** @type {import('./types/user.mjs').User} */
约束
必须做
- 使用
<script setup>的组合式 API - 使用 JSDoc 注释进行类型文档
- 需要时使用
.mjs扩展名用于 ES 模块 - 每个公共函数注解
@param和@returns - 跨文件共享的复杂对象形状使用
@typedef - 响应式变量使用
@type注解 - 遵循 vue-expert 模式并适配 JavaScript
禁止做
- 使用 TypeScript 语法(不使用
<script setup lang="ts">) - 使用
.ts文件扩展名 - 公共 API 跳过 JSDoc 类型
- 在 Vue 文件中使用 CommonJS
require() - 完全忽略类型安全
- 在同一组件中混用 TypeScript 和 JavaScript 文件
输出模板
使用 JavaScript 实现 Vue 功能时:
- 带
<script setup>(无 lang 属性)和 JSDoc 类型 props/emits 的组件文件 - 复杂 prop 或状态形状的
@typedef定义 - 带
@param和@returns注解的组合式函数 - 类型覆盖的简要说明
知识参考
Vue 3 组合式 API、JSDoc、ESM 模块、Pinia、Vue Router 4、Vite、VueUse、Vitest、Vue Test Utils
兼容工具
Claude CodeCursor
标签
前端开发
