ESLint Plugin for Vue Types
eslint-plugin-vue-types integrates `vue-types` with `eslint-plugin-vue`, specifically addressing the `vue/require-default-prop` rule. It prevents ESLint from reporting errors for props defined using `vue-types` validators (e.g., `VueTypes.string` or `string().isRequired`). The plugin's current stable version is 2.1.0. It primarily serves environments using `eslint-plugin-vue` versions older than 7.0.0. For `eslint-plugin-vue@7` and later, this plugin is generally not needed as the upstream `vue/require-default-prop` rule inherently ignores call expressions and object properties, negating the problem this plugin solves. Its release cadence is sporadic, reacting to changes in its peer dependencies, especially `eslint-plugin-vue` and `vue-types`. A key differentiator is its focused scope on a single `vue-types`-specific rule extension.
Common errors
-
Error: Failed to load parser '@typescript-eslint/parser' declared in '.eslintrc.js'.
cause Early versions (prior to 1.0.1) of `eslint-plugin-vue-types` had compatibility issues when used alongside `@typescript-eslint/parser`.fixUpgrade `eslint-plugin-vue-types` to version 1.0.1 or newer to resolve the `@typescript-eslint/parser` compatibility bug. -
Missing required prop: "myProp" vue/require-default-prop
cause The `vue/require-default-prop` rule from `eslint-plugin-vue` is active and not correctly suppressed by `eslint-plugin-vue-types` for `vue-types` definitions, often due to incorrect configuration or a too-old `eslint-plugin-vue` version.fixEnsure `plugin:vue-types/recommended` is included in your `extends` array *after* any `plugin:vue/*` presets. Verify that `eslint-plugin-vue` is at least version `^6.0.0` as required by peer dependencies. Also, check `.isRequired` placement and custom settings.
Warnings
- breaking Version 1.0.0 dropped support for Node.js 4. Users on older Node.js versions must remain on `eslint-plugin-vue-types` < 1.0.0.
- deprecated This plugin is largely obsolete for users running `eslint-plugin-vue@7` or higher. The upstream `vue/require-default-prop` rule in newer `eslint-plugin-vue` versions inherently ignores prop assignments by call expressions or object properties, making this plugin's functionality redundant.
- gotcha The `.isRequired` flag must always be the last call in the chain of `vue-types` validators for this plugin to correctly suppress `vue/require-default-prop` errors. Incorrect ordering, such as `.isRequired` followed by other modifiers (e.g., `.loose`), will result in an ESLint error.
- gotcha If using custom `vue-types` namespaces (e.g., `AppTypes.theme`), you must explicitly configure these namespaces in the plugin's `settings` via `vue-types/namespace` in your `.eslintrc.json` file. Without this, the plugin will not recognize them and will report `vue/require-default-prop` errors.
- gotcha When importing individual `vue-types` validators from a custom module (not `vue-types` itself), you need to specify these custom import sources in the plugin's `settings` via `vue-types/sources` in your `.eslintrc.json`. Otherwise, `vue/require-default-prop` errors will be reported.
Install
-
npm install eslint-plugin-vue-types -
yarn add eslint-plugin-vue-types -
pnpm add eslint-plugin-vue-types
Imports
- plugin configuration
{ "plugins": ["vue-types"] }{ "extends": ["plugin:vue/recommended", "plugin:vue-types/recommended"] } - settings.vue-types/namespace
{ "rules": { "vue-types/require-default-prop": ["error", { "namespace": ["AppTypes"] }] } }{ "settings": { "vue-types/namespace": ["AppTypes"] } } - settings.vue-types/sources
{ "rules": { "vue-types/require-default-prop": ["error", { "sources": ["~/utils/prop-types"] }] } }{ "settings": { "vue-types/sources": ["~/utils/prop-types"] } }
Quickstart
{
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/recommended",
"plugin:vue-types/recommended"
],
"parserOptions": {
"ecmaVersion": 2020
},
"rules": {
"vue/require-default-prop": "error" // Ensure the base rule is active
},
"settings": {
"vue-types/namespace": ["AppTypes"],
"vue-types/sources": ["./src/prop-validators"]
}
}
// src/App.vue
<template>
<div>{{ msg1 }} {{ msg2 }}</div>
</template>
<script>
import VueTypes, { string } from 'vue-types';
import { customProp } from './prop-validators';
const AppTypes = {
theme: VueTypes.oneOf(['dark', 'light'])
};
export default {
props: {
msg1: string().isRequired, // No error with plugin
msg2: VueTypes.string, // No error with plugin
msg3: AppTypes.theme, // No error with custom namespace
msg4: customProp().isRequired // No error with custom source
}
};
</script>
// src/prop-validators.js
import VueTypes from 'vue-types';
export const customProp = () => VueTypes.bool;