microsoft / microsoft/TypeScript
Event of type `"error"` incorrectly typed as ErrorEvent even when it's not actually ErrorEvent in practice
Nessuno ha ancora preso questa issue.
- 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:
EventEventErrorEvent(assumingwindownon-capture event dispatches of type"error"are always ErrorEvent, which I don't know)ErrorEvent | Event
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- 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