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-strict-mode-options-api.md

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

Enable strict Mode for Proper Options API TypeScript Support

Impact: HIGH - Without strict: true (or at minimum noImplicitThis: true) in your tsconfig.json, the this context in Options API components is typed as any. This silently disables type checking for all property access on component instances.

Task Checklist

  • Enable strict: true in tsconfig.json (recommended)
  • Or enable noImplicitThis: true at minimum
  • Wrap components with defineComponent() for proper inference
  • Verify type errors appear when accessing non-existent properties

The Problem

TypeScript's default behavior without strict mode allows implicit any typing, which defeats the purpose of using TypeScript with Vue's Options API.

tsconfig.json without strict mode:

{
  "compilerOptions": {
    "target": "ES2020"
    // No strict mode - this is DANGEROUS
  }
}

Component with hidden type errors:

import { defineComponent } from 'vue'

export default defineComponent({
  data() {
    return {
      count: 0,
      message: 'Hello'
    }
  },
  methods: {
    increment() {
      // Without strict mode, these errors are SILENT:
      this.cont++          // Typo: should be 'count'
      this.nonExistent     // Property doesn't exist
      this.message.toFixed() // Wrong method for string
    }
  }
})

All of the above errors compile successfully without strict mode because this is implicitly any.

Correct Configuration

Recommended tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "strict": true,
    "jsx": "preserve",
    "isolatedModules": true
  }
}

Minimum for Options API type safety:

{
  "compilerOptions": {
    "noImplicitThis": true
  }
}

What strict Mode Enables

The strict flag is a shorthand for enabling multiple type-checking options:

Option Effect
noImplicitThis Errors on this with implicit any type
noImplicitAny Errors on expressions with implicit any type
strictNullChecks null and undefined are distinct types
strictFunctionTypes Stricter function parameter checking
strictPropertyInitialization Class properties must be initialized
strictBindCallApply Stricter bind, call, apply typing
alwaysStrict Emits "use strict" in output

Correct Component with Proper Typing

import { defineComponent } from 'vue'

export default defineComponent({
  data() {
    return {
      count: 0,
      message: 'Hello'
    }
  },
  computed: {
    doubleCount(): number {
      return this.count * 2  // 'this.count' is typed as number
    }
  },
  methods: {
    increment() {
      this.count++           // Type-safe: count is number
      // this.cont++         // ERROR: Property 'cont' does not exist
    },
    greet(name: string) {
      return `${this.message}, ${name}!`  // Type-safe
    }
  }
})

Common Errors After Enabling Strict Mode

Error: Property 'xxx' does not exist

// Before: worked silently
this.unknownProp

// After: TypeScript error
// Property 'unknownProp' does not exist on type 'ComponentPublicInstance<...>'

Fix by adding the property to data() or declaring it properly.

Error: Object is possibly 'undefined'

methods: {
  getFirst() {
    const items = this.items
    // Error: Object is possibly 'undefined'
    return items[0].name
  }
}

Fix with proper null checks:

methods: {
  getFirst() {
    return this.items?.[0]?.name
  }
}

Important: defineComponent is Required

Even with strict mode, you must use defineComponent() to enable proper type inference:

// BAD - No type inference for 'this'
export default {
  data() {
    return { count: 0 }
  },
  methods: {
    increment() {
      this.count++  // 'this' is any even with strict mode!
    }
  }
}

// GOOD - Full type inference
import { defineComponent } from 'vue'

export default defineComponent({
  data() {
    return { count: 0 }
  },
  methods: {
    increment() {
      this.count++  // 'this.count' is properly typed as number
    }
  }
})

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.