Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Typing the DOM ref in the vue.js typescript

I'm just wondering why my ref to a simple button won't work in typescript. I created a simple button and a constant and then when component is mounted I check if the ref is null or not. otherwise make the button un-disabled...

Here is the example code:

<template>
 <input v-model="phone" type="text" />
 <button ref="phoneSubmitBtn" type="button" disabled>تایید</button>
</template>

<script lang="ts" setup>
import { Ref, ref, watch, onMounted } from 'vue';

const phoneSubmitBtn: Ref<HTMLButtonElement>|Ref<null> = ref(null);
let phone: Ref<string> | Ref<null> = ref(null);

onMounted(() => {
    if (phoneSubmitBtn.value === null) { 
        return false
    }

    watch(phone, () => {
        if (phone.value !== null && phone.value.length === 11) {
            phoneSubmitBtn.value.disabled = false;
        } else{
            phoneSubmitBtn.value.disabled = true;
        }
    })
})
</script>

Note: Logic works fine and everything works as well but the VSCODE tells me that phoneSubmitBtn.value.disabled: Property 'value' does not exist on type 'never'.ts(2339) inside of the watch section.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

phoneSubmitBtn is incorrectly typed, it's narrowed down to Ref<null> on assignment because it's a constant, so the type of phoneSubmitBtn.value is null.

It should have been:

const phoneSubmitBtn: Ref<HTMLButtonElement | null> = ref(null);

Since ref a generic, variable type can be inferred:

const phoneSubmitBtn = ref<HTMLButtonElement | null>(null);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!