microsoft / microsoft/TypeScript
All types of ECMAScript NativeErrors are always subtype reduced between themselves
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 2 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 132
Mô tả
⚙ 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:
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, NeverThrow 🙅, etc.), making them unreliable when using built-in errors. (Which are, arguably, the most commonly used type of errors.)
Sample Code
// Should be (SyntaxError | TypeError)[]
const twoErrors0 /* <? SyntaxError[] */ = [
new SyntaxError(),
new TypeError(),
]
// Should be (TypeError | SyntaxError)[]
const twoErrors1 /* <? TypeError[] */ = [
new TypeError(),
new SyntaxError(),
]
// --------------------------------------------------------------------
// Should return RangeError | URIError
function makeError0(flag: boolean) /* : RangeError is inferred */ {
if (flag)
return new RangeError()
return new URIError()
}
// Should return URIError | RangeError
function makeError1(flag: boolean) /* : URIError is inferred */ {
if (flag)
return new URIError()
return new RangeError()
}
const madeError = makeError1(true)
if (madeError instanceof URIError) {
// handle URIError
} else {
console.log(madeError)
// ^? madeError: never
// Programmer thinks they handled all cases, because the error is of type never here.
// madeError is actually an instance of RangeError at runtime.
// This happened due to TypeScript wrongly inferring makeError1's return type.
}
Documentation Link
ECMAScript 5.1: 15.11.7 NativeError Object Structure
ECMAScript 5.1: 15.3.5.3 [[HasInstance]] (V)
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với các khai báo ES5 NativeError và các trường hợp mẫu về suy luận kiểu trong issue. Kiểm tra cách các khai báo hiện tại khiến các kiểu lỗi tích hợp tương đương về mặt cấu trúc và cách quá trình rút gọn kiểu con xử lý các hàm dựng của chúng. Hoàn thành khi các kiểu lỗi native vẫn có thể phân biệt được trong các ví dụ về array và kiểu trả về được nêu, với kiểm thử hồi quy bao phủ các trường hợp đó.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- javascript, typescript
- Lĩnh vực
- compilers
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100