All skills
vuejs-ai avatar

/vue-options-api-best-practices

@bc922e4 official
by Vue AIvuejs-ai/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/vuejs-ai/skills/vue-options-api-best-practices

This session only. Nothing lands on disk.

referencets-options-api-arrow-functions-validators.md

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

Use Arrow Functions for Prop Validators in TypeScript < 4.7

LEGACY CONCERN: This issue was fixed in TypeScript 4.7 (released May 2022). Most modern projects using TypeScript 4.7+ do not need this workaround. If you're starting a new project or have upgraded TypeScript recently, you can safely use regular functions in prop validators. This rule is primarily relevant for legacy codebases still running TypeScript < 4.7.

Impact: HIGH - If your TypeScript version is less than 4.7, using regular functions for validator and default prop options can cause TypeScript to fail when inferring the type of this, which breaks type inference for the ENTIRE component, not just the prop.

Task Checklist

  • Check your TypeScript version (tsc --version)
  • If TypeScript < 4.7, use arrow functions for all validator and default prop options
  • Consider upgrading to TypeScript 4.7+ to avoid this limitation entirely

The Problem

TypeScript needs to infer the type of this inside regular functions. In Vue's Options API context, this inference can fail in versions before 4.7, causing cascading type inference failures.

BAD - Can break type inference in TS < 4.7:

import { defineComponent, PropType } from 'vue'

interface Book {
  title: string
  author: string
  year: number
}

export default defineComponent({
  props: {
    book: {
      type: Object as PropType<Book>,
      required: true,
      // Regular function - causes inference issues in TS < 4.7
      validator: function(book: Book) {
        return book.title.length > 0
      }
    },
    count: {
      type: Number,
      // Regular function - causes inference issues in TS < 4.7
      default: function() {
        return 0
      }
    }
  },
  // Type inference for computed, methods, etc. may break!
  computed: {
    bookTitle() {
      // 'this' might be typed as 'any' due to broken inference
      return this.book.title
    }
  }
})

GOOD - Use arrow functions:

import { defineComponent, PropType } from 'vue'

interface Book {
  title: string
  author: string
  year: number
}

export default defineComponent({
  props: {
    book: {
      type: Object as PropType<Book>,
      required: true,
      // Arrow function - safe for all TS versions
      validator: (book: Book) => book.title.length > 0
    },
    count: {
      type: Number,
      // Arrow function - safe for all TS versions
      default: () => 0
    }
  },
  computed: {
    bookTitle() {
      // 'this' is properly typed
      return this.book.title  // Type: string
    }
  }
})

Why Arrow Functions Work

Arrow functions don't have their own this binding, so TypeScript doesn't need to infer a this type for them. This avoids the type inference bug that affects regular functions in older TypeScript versions.

For Object/Array Default Values

Arrow functions are especially important for object and array defaults (which Vue requires to be functions anyway):

props: {
  config: {
    type: Object as PropType<Config>,
    // Must be a function, use arrow syntax
    default: () => ({
      enabled: true,
      maxItems: 10
    })
  },
  tags: {
    type: Array as PropType<string[]>,
    // Must be a function, use arrow syntax
    default: () => []
  }
}

When You Can Ignore This

  • TypeScript 4.7+: This issue was fixed in TypeScript 4.7 (May 2022). If your project uses 4.7 or later, regular functions work fine. Since TypeScript 4.7 has been available for over 3 years, most actively maintained projects have already upgraded and do not need this workaround.
  • New projects: If you're starting a new Vue project in 2024 or later, you'll almost certainly be using TypeScript 4.7+ by default and can ignore this rule entirely.
  • Arrow functions are still fine: While not required in modern TypeScript, using arrow functions for validators and defaults remains a valid stylistic choice and causes no issues.

Checking Your TypeScript Version

# Check installed TypeScript version
npx tsc --version

# Or check package.json
grep typescript package.json

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
  • lifecycle-hooks
  • type-safety
  • prop-validation
  • computed-properties

README badge

README badge for vuejs-ai/skills/vue-options-api-best-practices

Teaches Vue 3 Options API patterns including TypeScript integration, method binding, and lifecycle hook best practices. Covers type inference for props, event handlers, computed properties, and the this-context gotchas specific to Options API syntax.

Generated from the current SKILL.md.

Does this skill cover Vue 3 Composition API?
No. This skill focuses exclusively on Vue 3 Options API (data(), methods, this context). Composition API patterns are not included.
How does this skill handle TypeScript with Options API?
The skill includes dedicated references for TypeScript integration, such as using defineComponent for type inference, strict mode for this context, and typing complex props, event handlers, computed properties, and provide/inject.
What common mistakes does this skill address?
It covers arrow functions in methods and lifecycle hooks (which break this binding), stateful methods leaking state across component instances, and missing type safety for injected properties.
Can I use this skill if I'm migrating from older Vue versions?
The skill addresses TypeScript compatibility with older versions in the context of prop validators and Options API patterns, but is designed for Vue 3.

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