microsoft / microsoft/TypeScript
Regression of `this is` type predicate for intersection of unions from 4.7 to 4.8
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ả
🔎 Search Terms
"this is" "type predicate" "intersection" "union"
🕗 Version & Regression Information
- This changed between versions 4.7.4 and 4.8.4
⏯ Playground Link
💻 Code
// === SETUP
type Update = { update_id: number };
class Context {
constructor(public update: Update) {}
check(): this is Checked {
return true;
}
}
type Checked = { update: { REMOVE_THIS_TO_FIX?: never } };
// === TYPE MAGIC
type FilterContext<C extends Context, Q extends string> = PerformQuery<
C,
RunQuery<Q>
>;
// apply a query result by intersecting it with Update, and then injecting into C
type PerformQuery<C extends Context, U extends object> = U extends unknown
? C & { update: Update & U }
: never;
type RunQuery<Q extends string> = Combine<AssertKey<Q>, Q>;
type AssertKey<Q extends string> = Q extends unknown
? Record<Q, NonNullable<unknown>>
: never;
// define additional fields on U with value `undefined`
type Combine<U, K extends string> = U extends unknown
? U & Partial<Record<Exclude<K, keyof U>, undefined>>
: never;
// === HOW IT BREAKS
declare const ctx: FilterContext<
FilterContext<Context, "one" | "two">,
"two"
>;
const works = ctx.update.update_id;
type Works = (typeof ctx & Checked)["update"]["update_id"];
if (ctx.check()) {
const breaks = ctx.update.update_id;
}
🙁 Actual behavior
In the line where the variable breaks is defined, ctx.update is never
🙂 Expected behavior
The variable breaks can be defined and it has type number
Additional information about the issue
If you change a seemingly unrelated piece in the code, the error goes away!
// Replace
type Checked = { update: { REMOVE_THIS_TO_FIX?: never } };
// by
type Checked = { update: { } };
and then everything works as expected.
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 TypeScript Playground được liên kết và so sánh việc tái hiện trên 4.7.4 và 4.8.4. Theo dõi quá trình thu hẹp kiểu do Context.check() thực hiện đối với ví dụ FilterContext<Context, "one" | "two">. Được xem là hoàn tất khi việc truy cập breaks được chấp nhận và được suy luận là number, đồng thời hồi quy được bao phủ bằng một test.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- 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
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 38/100