Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda