Skip to content
← Back to rules

vue/require-typed-ref 风格

它的作用

要求 refshallowRef 函数必须具有强类型。

为什么这是不好的?

使用 TypeScript 时,可以通过 noImplicitAny 规则轻松防止使用 any。但遗憾的是,Vue 的 ref() 函数很容易绕过此规则。如果在调用 ref() 时不指定泛型参数或初始值,将会导致该 ref 的类型为 Ref<any>

示例

此规则的 错误 代码示例:

typescript
const count = ref();
const name = shallowRef();

此规则的 正确 代码示例:

typescript
const count = ref<number>();
const a = ref(0);

如何使用

要通过配置文件或 CLI 启用 此规则,可以使用:

json
{
  "plugins": ["vue"],
  "rules": {
    "vue/require-typed-ref": "error"
  }
}
bash
oxlint --deny vue/require-typed-ref --vue-plugin

参考资料