microsoft / microsoft/TypeScript

Event of type `"error"` incorrectly typed as ErrorEvent even when it's not actually ErrorEvent in practice

Aperta
#54,040 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms

ErrorEvent error capture

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about events

⏯ Playground Link

Playground link with relevant code

💻 Code
const scriptElement = document.createElement('script');
scriptElement.addEventListener('error', event => {
  console.log(`1. script element error event constructor: ${event.constructor.name}`);
});
scriptElement.src = 'https://example.com/nonExistentScript.js';

const linkElement = document.createElement('link');
linkElement.addEventListener('error', event => {
  console.log(`2. link element error event constructor: ${event.constructor.name}`);
});
linkElement.rel = 'stylesheet';
linkElement.href = 'https://example.com/nonExistentStylesheet.css';

window.addEventListener('error', event => {
  console.log(`3. window error event constructor: ${event.constructor.name}`);
});

window.addEventListener('error', event => {
  console.log(`4. window error (with capture) event constructor: ${event.constructor.name}`);
}, {
  capture: true
});

document.head.appendChild(scriptElement);

document.head.appendChild(linkElement);

throw new Error('Uncaught Error');
🙁 Actual behavior

For the above code, the TypeScript compiler types each event callback parameter as ErrorEvent, even though that's not always the type at runtime

Note that the linked playground doesn't show the log messages for the uncaught Error case; here's the full log output:

3. window error event constructor: ErrorEvent
4. window error (with capture) event constructor: ErrorEvent
4. window error (with capture) event constructor: Event
2. link element error event constructor: Event
4. window error (with capture) event constructor: Event
1. script element error event constructor: Event
🙂 Expected behavior

I don't know enough about the DOM "error" event to know what a complete solution would be, but ideally, in the code above, I guess the type for event would be:

  1. Event
  2. Event
  3. ErrorEvent (assuming window non-capture event dispatches of type "error" are always ErrorEvent, which I don't know)
  4. ErrorEvent | Event

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dal TypeScript Playground collegato e dalle tipizzazioni dei listener di eventi DOM "error" coinvolte negli esempi. Confronta i tipi inferiti dei parametri di callback con i costruttori registrati a runtime per gli eventi script, link e window, quindi assicurati che i tipi risultanti rappresentino accuratamente questi casi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, typescript
Ambito
frontend, web-dev
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.