Skip to content
← Back to rules

vue/require-typed-ref Style

作用

要求 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);

如何使用

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

json
{
  "plugins": ["vue"],
  "rules": {
    "vue/require-typed-ref": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["vue"],
  rules: {
    "vue/require-typed-ref": "error",
  },
});
bash
oxlint --deny vue/require-typed-ref --vue-plugin

版本

此规则在 v1.17.0 中添加。

参考资料