script/require-typed-ref
Require an explicit type argument on a ref() initialized with no value, null, or undefined.
| Default severity | Presets | Automatic fix | Options |
|---|---|---|---|
warning |
None | No | None |
Applies to: JS/TS scripts in Vue SFCs; examples show the relevant Options API or script setup form.
Bad
The imported ref calls have neither a type argument nor a useful initial value: no argument, null, and undefined cannot infer the intended future value type.
<script setup lang="ts">
import { ref } from 'vue'
const a = ref() // Ref<undefined>
const b = ref(null) // Ref<null>
const c = ref(undefined) // Ref<undefined>
</script>
Good
Explicit type arguments describe the string and nullable User refs. ref(0) already has a concrete numeric initializer and can rely on inference.
<script setup lang="ts">
import { ref } from 'vue'
const a = ref<string>()
const b = ref<User | null>(null)
const c = ref(0) // inferred Ref<number>
</script>
Configuration (Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: {
"preset": "incremental",
"rules": {
"script/require-typed-ref": "warn"
}
},
});
Then run vp run lint.