script/require-prop-type-constructor
要求 prop 的 type 值是构造器,而不是字符串字面量。
| 默认严重程度 | 预设 | 自动修复 | 选项 |
|---|---|---|---|
error |
essential, happy-path, ecosystem, nuxt, opinionated |
不支持 | 无 |
适用范围:Vue SFC 中的 JS/TS 脚本;示例展示对应的 Options API 或 script setup 形式。
错误示例
prop 声明将字符串 "String" 和 "Number" 用作运行时类型,包括构造器数组内的值。这些字符串不是构造函数。
<script lang="ts">
export default {
props: {
// The type should be the `String` constructor, not the string "String".
name: "String",
age: { type: "Number" },
id: { type: ["String", "Number"] }
}
}
</script>
正确示例
声明使用实际的 String 和 Number 标识符,包括联合数组 [String, Number]。
<script lang="ts">
export default {
props: {
name: String,
age: { type: Number },
id: { type: [String, Number] }
}
}
</script>
配置(Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: {
"preset": "incremental",
"rules": {
"script/require-prop-type-constructor": "error"
}
},
});
然后运行 vp run lint。