|
| 1 | +--- |
| 2 | +pageClass: rule-details |
| 3 | +sidebarDepth: 0 |
| 4 | +title: vue/define-props-destructuring |
| 5 | +description: enforce consistent style for props destructuring |
| 6 | +--- |
| 7 | + |
| 8 | +# vue/define-props-destructuring |
| 9 | + |
| 10 | +> enforce consistent style for props destructuring |
| 11 | +
|
| 12 | +- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> _**This rule has not been released yet.**_ </badge> |
| 13 | + |
| 14 | +## :book: Rule Details |
| 15 | + |
| 16 | +This rule enforces a consistent style for handling Vue 3 Composition API props, allowing you to choose between requiring destructuring or prohibiting it. |
| 17 | + |
| 18 | +By default, the rule requires you to use destructuring syntax when using `defineProps` instead of storing props in a variable and warns against combining `withDefaults` with destructuring. |
| 19 | + |
| 20 | +<eslint-code-block :rules="{'vue/define-props-destructuring': ['error']}"> |
| 21 | + |
| 22 | +```vue |
| 23 | +<script setup> |
| 24 | + // ✓ GOOD |
| 25 | + const { foo } = defineProps(['foo']) |
| 26 | + const { bar = 'default' } = defineProps(['bar']) |
| 27 | +
|
| 28 | + // ✗ BAD |
| 29 | + const props = defineProps(['foo']) |
| 30 | + const propsWithDefaults = withDefaults(defineProps(['foo']), { foo: 'default' }) |
| 31 | +
|
| 32 | + // ✗ BAD |
| 33 | + const { baz } = withDefaults(defineProps(['baz']), { baz: 'default' }) |
| 34 | +</script> |
| 35 | +``` |
| 36 | + |
| 37 | +</eslint-code-block> |
| 38 | + |
| 39 | +The rule applies to both JavaScript and TypeScript props: |
| 40 | + |
| 41 | +<eslint-code-block :rules="{'vue/define-props-destructuring': ['error']}"> |
| 42 | + |
| 43 | +```vue |
| 44 | +<script setup lang="ts"> |
| 45 | + // ✓ GOOD |
| 46 | + const { foo } = defineProps<{ foo?: string }>() |
| 47 | + const { bar = 'default' } = defineProps<{ bar?: string }>() |
| 48 | +
|
| 49 | + // ✗ BAD |
| 50 | + const props = defineProps<{ foo?: string }>() |
| 51 | + const propsWithDefaults = withDefaults(defineProps<{ foo?: string }>(), { foo: 'default' }) |
| 52 | +</script> |
| 53 | +``` |
| 54 | + |
| 55 | +</eslint-code-block> |
| 56 | + |
| 57 | +## :wrench: Options |
| 58 | + |
| 59 | +```js |
| 60 | +{ |
| 61 | + "vue/define-props-destructuring": ["error", { |
| 62 | + "destructure": "always" | "never" |
| 63 | + }] |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +- `destructure` - Sets the destructuring preference for props |
| 68 | + - `"always"` (default) - Requires destructuring when using `defineProps` and warns against using `withDefaults` with destructuring |
| 69 | + - `"never"` - Requires using a variable to store props and prohibits destructuring |
| 70 | + |
| 71 | +### `"destructure": "never"` |
| 72 | + |
| 73 | +<eslint-code-block :rules="{'vue/define-props-destructuring': ['error', { destructure: 'never' }]}"> |
| 74 | + |
| 75 | +```vue |
| 76 | +<script setup> |
| 77 | + // ✓ GOOD |
| 78 | + const props = defineProps(['foo']) |
| 79 | + const propsWithDefaults = withDefaults(defineProps(['foo']), { foo: 'default' }) |
| 80 | +
|
| 81 | + // ✗ BAD |
| 82 | + const { foo } = defineProps(['foo']) |
| 83 | +</script> |
| 84 | +``` |
| 85 | + |
| 86 | +</eslint-code-block> |
| 87 | + |
| 88 | +## :books: Further Reading |
| 89 | + |
| 90 | +- [Reactive Props Destructure](https://vuejs.org/guide/components/props.html#reactive-props-destructure) |
| 91 | + |
| 92 | +## :mag: Implementation |
| 93 | + |
| 94 | +- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/define-props-destructuring.js) |
| 95 | +- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/define-props-destructuring.js) |
0 commit comments