All skills
hyf0 avatar

/vue-options-api-best-practices

@bc922e4 official
by hyf0hyf0/vue-skills2.9k stars
167

Vue 3 Options API style (data(), methods, this context). Each reference shows Options API solution only.

Use this Skill: https://skilld.dev/gh/hyf0/vue-skills/vue-options-api-best-practices

This session only. Nothing lands on disk.

referencets-options-api-provide-inject-limitations.md

≈836 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Provide/Inject Has Limited Type Inference in Options API

Impact: MEDIUM - When using provide/inject with the Options API and TypeScript, injected properties won't be automatically typed. TypeScript will show errors that the property doesn't exist on the component, even though it works at runtime.

Task Checklist

  • Be aware that Options API inject has limited type support
  • Use type augmentation to add types to injected values
  • Consider using typed computed wrappers for injected values

The Problem

// Provider component (parent)
import { defineComponent } from 'vue'

export default defineComponent({
  provide() {
    return {
      theme: 'dark',
      user: { id: 1, name: 'John' }
    }
  }
})
// Consumer component (child) - Options API
import { defineComponent } from 'vue'

export default defineComponent({
  inject: ['theme', 'user'],

  mounted() {
    // TypeScript Error: Property 'theme' does not exist on type...
    console.log(this.theme)

    // TypeScript Error: Property 'user' does not exist on type...
    console.log(this.user.name)
  }
})

Solution 1: Type Augmentation

Augment the component type to include injected properties:

// types/injections.d.ts
import 'vue'

declare module 'vue' {
  interface ComponentCustomProperties {
    theme: 'light' | 'dark'
    user: { id: number; name: string }
  }
}

export {}
// Consumer component - now typed
import { defineComponent } from 'vue'

export default defineComponent({
  inject: ['theme', 'user'],

  mounted() {
    // Now works - typed from ComponentCustomProperties
    console.log(this.theme)  // 'light' | 'dark'
    console.log(this.user.name)  // string
  }
})

Note: This adds types globally to ALL components, not just those that inject these values.

Solution 2: Typed Computed Wrappers

Use the object syntax with computed property wrappers for type safety:

import { defineComponent } from 'vue'

interface User {
  id: number
  name: string
}

export default defineComponent({
  inject: {
    theme: {
      from: 'theme',
      default: 'light'
    },
    user: {
      from: 'user',
      default: () => ({ id: 0, name: 'Guest' })
    }
  },

  computed: {
    // Type via computed wrapper
    typedTheme(): 'light' | 'dark' {
      return this.theme as 'light' | 'dark'
    },
    typedUser(): User {
      return this.user as User
    }
  },

  mounted() {
    console.log(this.typedTheme)
    console.log(this.typedUser.name)
  }
})

Why This Happens

The Options API inject array syntax inject: ['theme'] doesn't provide type information to TypeScript. Vue knows about the injection at runtime, but TypeScript's static analysis can't trace the provide/inject relationship across components.

Reference

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    The skill provides comprehensive best practices and TypeScript integration guidelines for the Vue.js Options API. It contains educational content and code examples that are safe and consistent with official Vue.js documentation. No security issues were detected.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer7mo

    11 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at bc922e4. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 8 months ago
version
2.0.0
author
github.com/vuejs-ai
  • Vue
  • TypeScript
  • options-api
  • best-practices
  • lifecycle-hooks
  • prop-types
  • type-safety

README badge

README badge for hyf0/vue-skills/vue-options-api-best-practices

Teaches Vue 3 Options API patterns, TypeScript integration, and common mistakes like arrow functions in methods and lifecycle hooks. Covers prop typing, event handler safety, provide/inject limitations, and computed property type inference specific to the Options API style.

Generated from the current SKILL.md.

Does this skill cover Composition API?
No. This skill focuses exclusively on Options API patterns and does not include Composition API examples or guidance.
What TypeScript support does this provide?
The skill includes references for enabling type inference with defineComponent, typing event handlers, complex prop types, provide/inject limitations, and computed property return types in the Options API context.
Does this address method binding issues?
Yes. The skill covers arrow function pitfalls in methods and lifecycle hooks, which commonly break this context binding in the Options API.

Generated from the current SKILL.md. These answers refresh after source changes.