microsoft / microsoft/TypeScript
Unexpected circularity error when variable is initialized from a class member
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
loss of type, for loop, for-loop, class field
🕗 Version & Regression Information
- This changed between versions ______ and _______
- This changed in commit or PR _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
⏯ Playground Link
💻 Code
interface Application {
id: string;
category: {
id: string;
}
}
class Applications {
public FindApplication(query: string): Application | null {
return null;
}
}
class Test {
SelectedApplications: Set<string> = new Set();
Applications: Applications | null = null;
get SelectedApplicationsByCategory(): Partial<Record<string, Set<string>>> | null {
// Workaround 1: move this block inside for loop
// Workaround 2: Save this.Applications to a const then use that
if (this.Applications === null) {
return null;
}
const result: Partial<Record<string, Set<string>>> = {};
for (const applicationID of this.SelectedApplications) {
const application = this.Applications.FindApplication(applicationID);
if (application === null) {
continue;
}
const categoryID = application.category.id;
const applications = result[categoryID] ?? new Set<string>();
applications.add(application.id);
// Comment/uncomment the following line to see it break/unbreak
result[categoryID] = applications;
}
return result;
}
}
🙁 Actual behavior
application has type: any. FindApplication returns Application | null, but that gets lost somewhere.
🙂 Expected behavior
type for application should be Application | null.
Additional information about the issue
See comments in code for workarounds and to see another line break type inference.
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 bản tái hiện được liên kết trong TypeScript Playground và so sánh hai workaround đã được ghi lại trong getter. Truy tìm lý do phép gán cho result[categoryID] làm thay đổi kiểu được suy luận của application; hoàn thành khi application vẫn là Application | null mà không cần bất kỳ workaround nào trong hai workaround đó.
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
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 42/100