script/no-boolean-default
禁止为 Boolean prop 设置默认值。
| 默认严重程度 | 预设 | 自动修复 | 选项 |
|---|---|---|---|
warning |
opinionated, nuxt |
不支持 | 无 |
适用范围:Vue SFC 中的 JS/TS 脚本;示例展示对应的 Options API 或 script setup 形式。
错误示例
disabled 和 checked 都为唯一构造器为 Boolean 的 prop 声明了 default;规则连显式的 false 默认值也会拒绝。
<script lang="ts">
export default {
props: {
// Boolean props already default to false; an explicit default is confusing.
disabled: { type: Boolean, default: true },
checked: { type: Boolean, default: false }
}
}
</script>
正确示例
仅 Boolean 的 props 省略 default,使用 Vue 隐式的 false 值。[Boolean, String] 联合类型和 Number prop 说明此检查仅限唯一的 Boolean 构造器。
<script lang="ts">
export default {
props: {
// No explicit default: defaults to false.
disabled: { type: Boolean },
disabled2: Boolean,
// Union type may legitimately need a default.
value: { type: [Boolean, String], default: '' },
// Non-Boolean prop.
count: { type: Number, default: 0 }
}
}
</script>
配置(Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: {
"preset": "incremental",
"rules": {
"script/no-boolean-default": "warn"
}
},
});
然后运行 vp run lint。