Currently, I am using the Composition API (with <script setup lang='ts'>
) to create a ref that is utilized in my template:
const searchRef = ref(null)
onMounted(() => { searchRef.value.focus() })
Although it works and my code compiles without any errors, my IDE (either JetBrains Goland or Webstorm) displays a warning with
TS2531: Object is possibly 'null'.
.
The more drastic solution involves using
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Object is possibly 'null'.
However, I'm interested in finding a better approach if possible.
I had initially hoped that utilizing optional chaining would resolve the issue:
const searchRef = ref(null)
onMounted(() => { searchRef.value?.focus() })
Despite still compiling and functioning correctly, this implementation now triggers an error message of
TS2339: Property 'focus' does not exist on type 'never'.
What would be the proper way to tackle this particular dilemma?