microsoft / microsoft/TypeScript

All types of ECMAScript NativeErrors are always subtype reduced between themselves

未关闭
#62,134 6 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Awaiting More Feedback Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### ⚙ 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 /*

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 ES5 NativeError 声明和 issue 中的类型推断示例开始。检查当前声明如何使内置错误类型在结构上等价,以及子类型约简如何处理它们的构造函数。完成的标准是:在所示的数组和返回类型示例中,原生错误类型仍然可以区分,并且这些案例有回归覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。