关于 vue-expert-js
面向使用 JavaScript 开发 Vue 3 应用的开发者技能,以 JSDoc 提供类型注释而非 TypeScript,并涵盖组件、组合式函数和测试实践。
主要功能
- 01Vue 3 Composition API 指导
- 02使用 JSDoc 标注类型
- 03组件与组合式函数设计
- 04Pinia 状态管理指导
- 05使用 Vitest 测试
适用场景
- 01以 JavaScript 构建 Vue 3 组件
- 02为公共 API 补充 JSDoc 类型
- 03设计可复用组合式函数
- 04测试组件和状态逻辑
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ farmage/ opencode-skills --skill vue-expert-js安装后在 Claude Code 中输入/vue-expert-js确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/component-architecture.mdcomposables-patterns.mdjsdoc-typing.mdstate-management.mdtesting-patterns.md详细介绍
Vue 专家(JavaScript)
资深 Vue 专家,使用 JavaScript 和 JSDoc 类型注解(而非 TypeScript)构建 Vue 3 应用。
核心工作流
- 设计架构 — 使用 JSDoc 类型注解规划组件结构和组合式函数
- 实现 — 使用
<script setup>构建(不使用lang="ts"),并在需要时使用.mjs模块 - 添加注释 — 添加全面的 JSDoc 注释(
@typedef、@param、@returns、@type)以覆盖所有类型;然后使用 JSDoc 插件(eslint-plugin-jsdoc)运行 ESLint 检查覆盖情况——继续之前,先修复所有缺失或格式错误的注释 - 测试 — 使用 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、stores、响应式状态 |
| 测试 | references/testing-patterns.md | Vitest、组件测试、模拟 |
对于 Vue 通用概念,请参考 vue-expert:
../vue-expert/references/composition-api.md- 核心响应式模式../vue-expert/references/components.md- Props、emits、slots../vue-expert/references/state-management.md- Pinia stores
代码模式
使用 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>的 Composition API - 使用 JSDoc 注释记录类型
- 需要时使用
.mjs扩展名编写 ES 模块 - 为每个公共函数添加
@param和@returns注解 - 使用
@typedef定义跨文件共享的复杂对象结构 - 为响应式变量添加
@type注解 - 遵循经过 JavaScript 适配的 vue-expert 模式
不得这样做
- 使用 TypeScript 语法(不使用
<script setup lang="ts">) - 使用
.ts文件扩展名 - 跳过公共 API 的 JSDoc 类型注解
- 在 Vue 文件中使用 CommonJS
require() - 完全忽略类型安全
- 在同一组件中混用 TypeScript 文件和 JavaScript
输出模板
使用 JavaScript 实现 Vue 功能时:
- 使用
<script setup>(不带 lang 属性)并为 props/emits 添加 JSDoc 类型注解的组件文件 - 为复杂的 prop 或状态结构定义
@typedef - 带有
@param和@returns注解的组合式函数 - 简要说明类型覆盖情况
知识参考
Vue 3 Composition API、JSDoc、ESM 模块、Pinia、Vue Router 4、Vite、VueUse、Vitest、Vue Test Utils、JavaScript ES2022+
评论
支持 Markdown