microsoft / microsoft/TypeScript
Narrowing of generic this is inconsistent with variable narrowing
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
generic union constraint narrowing this
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
type Type = I32 | Str;
interface Str {
group: "none";
kind: "str";
}
interface I32 {
group: "scalar";
kind: "i32";
}
declare function takeI32(value: I32): void;
declare function inferKind<T>(value: { kind: T }): T;
function testThis<T extends Type>(this: T) {
switch (this.group) {
case "none":
return;
case "scalar":
switch (this.kind) {
case "str": // no error?
case "i32":
}
}
}
function testParameter<T extends Type>(o: T) {
switch (o.group) {
case "none":
return;
case "scalar":
switch (o.kind) {
case "str": // error!
case "i32":
}
}
}
function testAliasedThis<T extends Type>(this: T) {
const o = this;
switch (o.group) {
case "none":
return;
case "scalar":
switch (o.kind) {
case "str": // error!
case "i32":
}
}
}
function testThis2<T extends Type>(this: T): I32 | undefined {
if (this.group === "scalar") {
const value: I32 = this; // error
takeI32(this); // error
this satisfies I32; // error
const arrow = (): I32 => this; // error
return this; // error
}
}
function testParameter2<T extends Type>(o: T): I32 | undefined {
if (o.group === "scalar") {
const value: I32 = o; // ok
takeI32(o); // ok
o satisfies I32; // ok
const arrow = (): I32 => o; // ok
return o; // ok
}
}
function testAliasedThis2<T extends Type>(this: T): I32 | undefined {
const o = this;
if (o.group === "scalar") {
const value: I32 = o; // ok
takeI32(o); // ok
o satisfies I32; // ok
const arrow = (): I32 => o; // ok
return o; // ok
}
}
🙁 Actual behavior
The errors (or lack of them) are inconsistent between narrowing this directly vs narrowing an "aliased" this (const alias = this;)
🙂 Expected behavior
They should be consistent
Additional information about the issue
No response
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à rút gọn các ví dụ generic-union được cung cấp cho direct this, aliased this và parameter narrowing. Issue không xác định file nào trong repository hoặc test nào, vì vậy hãy tìm entry point của compiler narrowing và thêm một regression test bao phủ các trường hợp được trình bày. Hoàn tất khi việc narrowing direct this và aliased this tạo ra các diagnostics nhất quán.
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
- Sôi nổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100