vue/define-props-declaration Style
它的作用
此规则强制 defineProps 的类型声明风格,你应该使用 type-based 或 runtime 声明。 此规则仅在带有 lang="ts" 的 <script setup> 中生效。
这为什么不好?
不一致的代码风格会令人困惑,并使代码更难 阅读。
示例
以下是此规则的不正确代码示例:
vue
// "vue/define-props-declaration": ["error", "type-based"]
<script setup lang="ts">
const props = defineProps({
kind: { type: String },
});
</script>
// "vue/define-props-declaration": ["error", "runtime"]
<script setup lang="ts">
const props = defineProps<{
kind: string;
}>();
</script>以下是此规则的正确代码示例:
vue
// "vue/define-props-declaration": ["error", "type-based"]
<script setup lang="ts">
const props = defineProps<{
kind: string;
}>();
</script>
// "vue/define-props-declaration": ["error", "runtime"]
<script setup lang="ts">
const props = defineProps({
kind: { type: String },
});
</script>配置
此规则接受以下字符串值之一:
"type-based"
强制使用基于类型的声明。
"runtime"
强制使用运行时声明。
如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["vue"],
"rules": {
"vue/define-props-declaration": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["vue"],
rules: {
"vue/define-props-declaration": "error",
},
});bash
oxlint --deny vue/define-props-declaration --vue-plugin版本
此规则于 v1.15.0 中新增。
