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

232
Visualizações
Getting ID of clicked element using strict type

Let us assume we want to log the ID attribute of a clicked element with the following TypeScript code:

onClick(event) {
  console.log(event.target.attributes.id.nodeValue);
}

The above function receives the clicked object as a parameter, but the type in this case is any. If I check the result of typeof event I get object. So I would modify my code as follows:

onClick(event: object) {
  console.log(event.target.attributes.id.nodeValue);
}

In this case, I receive an error in VS Code as below:

Property 'target' does not exist on type 'object'.ts(2339)

So what is up with this? Using the type any as parameter type works, the console logs the desired ID without any error, but if I decide to use a strict object as parameter type, my code does not compile. Where is the issue?

The affected HTML code is the following;

<h1 id="foo" (click)="onClick($event)">...</h1>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If I check the result of typeof event I get object.

That's runtime code, so it's the JavaScript typeof operator, which says "object" for all objects (and null).

So I would modify my code as follows...I receive an error is VS Code like below

Plain objects don't have a target property.

The type of the event object for a click event is MouseEvent. (You've tagged jquery and angular. If you're using jQuery or the DOM to attach the event, it'll be the DOM's MouseEvent. If you're using Angular to attach the event, it looks like it'll also be the DOM's MouseEvent. Caveat: I don't use Angular.) You'd also probably want event.currentTarget.id or event.target.id rather than using the attributes collection and nodeValue, since the id attribute is a reflected property.

But, the DOM's MouseEvent interface inherits its currentTarget property from Event, which just defines it as an EventTarget. You know that it will be an Element, but unfortunately the interface just says it's EventTarget. (The same is true for target.)

As far as I'm aware, there's no predefined interface for a MouseEvent that defines currentTarget as Element instead. You can reliably (in this situation) use a type assertion:

onClick(event: MouseEvent) {
    console.log((event.target as Element).id); // Or `(event.currentTarget as Element).id`
}

I don't like type assertions, but they are sometimes necessary.

Sometimes you see people doing that same assertion slightly differently, like this:

onClick({target}: {target: Element}) {
    console.log(target.id);
}
// or
onClick({currentTarget}: {currentTarget: Element}) {
    console.log(currentTarget.id);
}

You might create a reusable type instead, like this:

interface ElementMouseEvent extends MouseEvent {
    currentTarget: Element;
    target: Element;
}

...and then use it (which is still making an assertion, but again, a safe one):

onClick(event: ElementMouseEvent) {
    console.log(event.target.id); // Or `event.currentTarget.id`
}
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