microsoft / microsoft/TypeScript

All types of ECMAScript NativeErrors are always subtype reduced between themselves

Open
#62,134 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### ⚙ Compilation target

ESNext

### ⚙ Library

es5

### Missing / Incorrect Definition

All error instance types defined in es5 are equivalent in eyes of TypeScript, but are distinguishable at runtime with `instanceof`.
Here is how each error is defined right now:
```ts
interface XError extends Error { // <- Same as Error!
}

interface XErrorConstructor extends ErrorConstructor {
new (message?: string): XError
(message?: string): XError
readonly prototype: XError
}

declare var XError: XErrorConstructor
```
As you can see, any error is structurally equivalent to another, including `Error` itself. This makes the issue propagate to error constructors as well.

On a somewhat related note, child classes of `Error` (`class MyError extends Error {}`) do not have this issue.

###### This affects libraries that attempt making error-handling typed (for example, [Effect.ts](https://effect.website/), [NeverThrow 🙅](https://www.npmjs.com/package/neverthrow), etc.), making them unreliable when using built-in errors. (Which are, arguably, the most commonly used type of errors.)

### Sample Code

```TypeScript
// Should be (SyntaxError | TypeError)[]
const twoErrors0 /*

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ES5 NativeError declarations and the sample type-inference cases in the issue. Check how the current declarations make the built-in error types structurally equivalent and how subtype reduction handles their constructors. Done means the native error types remain distinguishable in the shown array and return-type examples, with regression coverage for those cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.