microsoft / microsoft/TypeScript
for-of loop with intersection of array types produces a union of element types
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 117
Mô tả
TypeScript Version: 3.9.2, 4.0.0-beta
Search Terms: intersection, for-of, union, iterable, iterator, iterate, array
Expected behavior:
When you use a for..of loop to iterate over the elements of an intersection of arrays (or maybe other iterables), what type should the elements be? I would expect either:
- you get the same type as when you index into the array: an intersection of the element types; or
- you get the same type as when you use the iterator method manually: the first element type because the iterator methods are overloads.
Actual behavior:
Iterating over an intersection of arrays with a for..of loop produces a union of their element types for some reason.
Related Issues:
#11961: intersection of array types results in overloaded methods (this would maybe imply overloaded iterators, but that's not happening here)
Aside:
I'm not sure why you'd want an intersection of array types in the first place; but the behavior showed up in a Stack Overflow question and I'm at a loss understanding why we get a union here.
Code
declare const arr: Array<{ a: string }> & Array<{ b: number }>;
for (const elemItr of arr) {
// { a: string } | {b : number } 😕
elemItr.a.toUpperCase(); // error!
elemItr.b.toFixed(); // error!
}
// I expected either this (intersection of element types)
const elemIdx = arr[0]; // { a: string; } & { b: number; }
elemIdx.a.toUpperCase(); // okay
elemIdx.b.toFixed(); // okay
// or this (overloaded iterators giving the first element type only)
const iter = arr[Symbol.iterator];
/* const iter: {
(): IterableIterator<{ a: string }>;
(): IterableIterator<{ a: string }>;
} & {
(): IterableIterator<{ b: number }>;
(): IterableIterator<{ b: number }>;
} */
const result = arr[Symbol.iterator]().next();
if (!result.done) {
result.value.a.toUpperCase(); // okay
result.value.b.toFixed(); // error, expected this
}
Output
"use strict";
for (const elemItr of arr) {
// { a: string } | {b : number } 😕
elemItr.a.toUpperCase(); // error!
elemItr.b.toFixed(); // error!
}
// I expected either this (intersection of element types)
const elemIdx = arr[0]; // { a: string; } & { b: number; }
elemIdx.a.toUpperCase(); // okay
elemIdx.b.toFixed(); // okay
// or this (overloaded iterators giving the first element type only)
const iter = arr[Symbol.iterator];
/* const iter: {
(): IterableIterator<{ a: string }>;
(): IterableIterator<{ a: string }>;
} & {
(): IterableIterator<{ b: number }>;
(): IterableIterator<{ b: number }>;
} */
const result = arr[Symbol.iterator]().next();
if (!result.done) {
result.value.a.toUpperCase(); // okay
result.value.b.toFixed(); // error, expected this
}
Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"useDefineForClassFields": false,
"alwaysStrict": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"downlevelIteration": false,
"noEmitHelpers": false,
"noLib": false,
"noStrictGenericChecks": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"esModuleInterop": true,
"preserveConstEnums": false,
"removeComments": false,
"skipLibCheck": false,
"checkJs": false,
"allowJs": false,
"declaration": true,
"experimentalDecorators": false,
"emitDecoratorMetadata": false,
"target": "ES2017",
"module": "ESNext"
}
}
Playground Link: Provided
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 liên kết TypeScript Playground đã cung cấp và rút gọn ví dụ về phép giao của các mảng để so sánh phép lặp for-of, truy cập theo chỉ mục và một lệnh gọi Symbol.iterator tường minh. Xác định kiểu phần tử mà for-of sẽ tạo ra, sau đó kiểm tra hành vi đã chọn với đầu ra trình biên dịch được báo cáo và bổ sung coverage cho reproducer.
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
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100