Skip to content
← Back to rules

vue/no-required-prop-with-default Suspicious

💡 A suggestion is available for this rule.

作用

强制带有默认值的 prop 必须是可选的。

为什么这不好?

如果一个 prop 声明了默认值,无论它是否是必需的, 在实际使用中我们总是可以省略它。在这种情况下,就会应用默认值。 因此,带有默认值的必需 prop 本质上与可选 prop 没有区别。

示例

此规则的错误代码示例:

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>

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["vue"],
  "rules": {
    "vue/no-required-prop-with-default": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["vue"],
  rules: {
    "vue/no-required-prop-with-default": "error",
  },
});
bash
oxlint --deny vue/no-required-prop-with-default --vue-plugin

版本

此规则是在 v1.17.0 中添加的。

参考资料