microsoft / microsoft/TypeScript
Narrow object property indexed by another object's property of unique symbol type (like `Symbol.iterator`)
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
type guard, narrowing, control flow analysis, property, index, known type, symbol type, bracket notation, unique symbol, known symbol, Symbol.iterator, #10530
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
This is yet another version of #10530 that doesn't seem to be covered by cases in #56389 or #61176. It might even be covered by #62247 and/or #62314 but I'm not sure. TypeScript already narrows by bracket notation when the key is a unique symbol variable:
declare const s: unique symbol;
declare const x: {}
if ((s in x) && (typeof x[s] === "string")) { x[s].toUpperCase(); }; // okay
but this no longer works if the key is a unique symbol property of another object:
declare const obj: { readonly s: unique symbol }
declare const x: {}
if ((obj.s in x) && (typeof x[obj.s] === "string")) { x[obj.s].toUpperCase(); } // error
The suggestion here is to make the latter work like the former.
📃 Motivating Example
See https://stackoverflow.com/questions/79877994/why-does-my-test-for-an-iterableiterator-fail
Consider
function f(x: object) {
if ((Symbol.iterator in x) && (typeof x[Symbol.iterator] === "function")) {
x[Symbol.iterator].length; // error
//~~~~~~~~~~~~~~~~ <-- Object is of type 'unknown'.(2571)
}
}
This just looks like a somewhat unfortunate gap in type guarding, where TS knows Symbol.iterator is a unique symbol but won't actually do the narrowing unless you put that unique symbol in its own variable directly:
function f(x: object) {
const symbolIterator: typeof Symbol.iterator = Symbol.iterator;
if ((symbolIterator in x) && (typeof x[symbolIterator] === "function")) {
x[symbolIterator].length; // okay
}
}
💻 Use Cases
- What do you want to use this for? To allow people to type guard on
Symbol.XXXproperties - What shortcomings exist with current approaches? People apparently don't like to copy things to new variables just for narrowing.
- What workarounds are you using in the meantime? Copy things to new variables. The obvious approach is the old standby, where we replace
if (f(obj[k])) g(obj[k])withconst objK = obj[k]; if (f(objK)) g(objK)
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 bằng việc đọc phần phân tích luồng điều khiển và xử lý type guard của TypeScript đối với cú pháp ngoặc vuông, đồng thời so sánh ví dụ biến unique-symbol đang hoạt động với các trường hợp obj.s và Symbol.iterator bị lỗi. Xem lại các issue liên quan #10530, #56389, #61176, #62247 và #62314, sau đó dùng ví dụ Playground được cung cấp để xác minh rằng việc truy cập thuộc tính narrowing một cách nhất quán khi thay đổi hoàn tất.
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
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- 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