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

527
Visualizações
Handling property does not exist when using JavaScript libraries in TypeScript

I'm using TypeScript for a Vue.js app and I'm trying to use the drop event to trigger uploading an image like this:

handleDOMEvents: {
    drop(view, event) {
        const hasFiles = event.dataTransfer
            && event.dataTransfer.files
            && event.dataTransfer.files.length;
        if (hasFiles) {
            upload(event.dataTransfer.files);
        }
    }
}

(The full guide I'm follow is: https://gist.github.com/slava-vishnyakov/16076dff1a77ddaca93c4bccd4ec4521)

The code works, but TypeScript is giving a warning on the .dataTransfer lines:

Property 'dataTransfer' does not exist on type 'Event'

The reason is that the event argument of the drop function is expecting an Event object, which doesn't have the dataTransfer property.

However, I would expect that drop() should always expect a DragEvent, but this is set in the external library so I don't have control over it.

How can I correctly handle this in TypeScript? Is is as simple as adding something like if (typeof(event) !== DragEvent)? Or is there a better way to get TypeScript to recognise this as a DragEvent? Is this just a case where it's best to ignore the warning?

I'm trying to understand what the best practice approach to handle this would be.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have the right idea with if (typeof(event) !== DragEvent), except for the fact that TS types don't exist at runtime, so you can't check for them like that.

The way to do this is to create a custom type guard on the field event.type:

function isDragEvent(e: Event): e is DragEvent {
  return e.type === "drag";
}

You can then use it as a condition:

drop(view, event) {
    if(isDragEvent(event)){
        // TS recognizes that event is a DragEvent and has a dataTransfer member
        const hasFiles = event.dataTransfer
            && event.dataTransfer.files
            && event.dataTransfer.files.length;
        if (hasFiles) {
            upload(event.dataTransfer.files);
        }
    
    } else {
        throw new Error("the passed event is not a DragEvent")
    }
}

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