vue/no-required-prop-with-default 可疑
它的作用
强制要求带有默认值的属性为可选。
为什么这是个问题?
如果一个属性被声明为带有默认值,无论是否标记为必填,在实际使用中都可以省略它。在这种情况下,将应用默认值。因此,带有默认值的必填属性本质上与可选属性相同。
示例
此规则的错误代码示例:
vue
<script setup lang="ts">
const props = withDefaults(
defineProps<{
name: string | number;
age?: number;
}>(),
{
name: "Foo",
},
);
</script>此规则的正确代码示例:
vue
<script setup lang="ts">
const props = withDefaults(
defineProps<{
name?: string | number;
age?: number;
}>(),
{
name: "Foo",
},
);
</script>如何使用
要通过配置文件或 CLI 启用此规则,可以使用:
json
{
"plugins": ["vue"],
"rules": {
"vue/no-required-prop-with-default": "error"
}
}bash
oxlint --deny vue/no-required-prop-with-default --vue-plugin