Repository: TypeScript
Issue: Event of type `"error"` incorrectly typed as ErrorEvent even when it's not actually ErrorEvent in practice
Labels: Suggestion,Awaiting More Feedback

# 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

<!--
  A link to a TypeScript Playground "Share" link which shows this behavior

  The TypeScript Workbench can be used for more complex setups, try
  https://www.typescriptlang.org/dev/bug-workbench/

  As a last resort, you can link to a repo, but these will be slower for us to investigate.
-->

[Playground link with relevant code](https://www.typescriptlang.org/play?#code/MYewdgzgLgBBwCcCWAHKBRANgUwLbbFgF4YATEYAV30IDpFsBDKbLPAqACgHJ5k1uASgDcAKD6oMOGlFqNSpdADcOAGSTQC2BD20IQCbgBoY2FYRhEAfDADeomDFCQQOWphABzTgAMAjLRwiJKm0hymCPoIpuawztAIlMBQBgBcMAAktmYc9OAJSSkItGCM+AC+PiKi5dUSaGwytBAIwJYw3AAWUFAoEKkA9APYAB5lKG6guANg4OgjGiyEAMrBaLQAVhDcYqLxsJhIYADWjeEk5FRNDMysYYQ8hydCYk+n97LyirHqmmDaukiBmMMXONnsjnirmw7i8vgATIE3qF2BY9AZQRZ9olkmlMtlYnlIFAcUUSmVsJVqrVXkd3qjZAhsJh2rwoABPHAQTrYbBQHaiN5nOidJkAM1Z3V6-SGo3GkxA01mYHmiw4yw5XJ5fPoEG2uwA7kdyAa5AplGo1f8dNx0YYTDkLNY7A4nPlobDvD4AMyBI1gE0RKKYuL5EmFPFZR2ybER4qlCpVMQ00Sif0ms3fS1-AG2oH2kOWcGuqFuDxegAsfuNIANQYxnCNUE6TkYaEoTMEhdjuIQ6SjhJ7ZITlKTNRMENb7aZ6XD2Bq1VEl2ouR58jkKBQBFIAGFOkhMKROPUpAzF8ummvSButwG9wej0KPovm-o6-86+h8zwAKpgYCMJQnjdDAX5RC8QA)

### 💻 Code

```ts
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

<!-- What happened, and why it was wrong -->

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

<!-- What you expected to happen instead, and why -->

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`