microsoft / microsoft/TypeScript
Suggestion: type narrowing based on user-defined type guards against properties
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
user-defined type guards, type predicates, type narrowing, object properties
Suggestion
TS can narrow object types in if statements and other conditional control flows by various means. Particularly, if the object's type is a union type distinguishable by a "tag" (like Option<T> in the below example), we can narrow the type by checking the value of the tag. However, if the check for the tag is done by user-defined type guards against the property (but not the object itself), the object's type is not narrowed.
The suggestion is to make it work even if user-defined type guards are applied to object properties
Use Cases
The "tagged union" pattern is widely used in TS codebases. If tags theirselves are so complicated, we want to utilize user-defined type guards to check them.
Examples
type Option<T> = {
type: "some"
value: T
} | {
type: "none"
}
const isSome = (type: "some" | "none"): type is "some" => type === "some"
declare const option: Option<number>
// Good: option is narrowed in the if block
if (option.type === "some") {
option.value
}
// Bad: option isn't narrowed
if (isSome(option.type)) {
// Error: Property 'value' does not exist on type 'Option<number>'.
option.value
}
// Available workaround
const isSomeObject = <T>(option: Option<T>): option is Extract<Option<T>, { type: "some" }> => option.type === "some"
if (isSomeObject(option)) {
option.value
}
Checklist
My suggestion meets these guidelines:
- 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, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
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 playground được liên kết và các ví dụ về Option để tái hiện sự khác biệt giữa việc kiểm tra thuộc tính trực tiếp và các type guard do người dùng định nghĩa. Issue không nêu tệp triển khai hay bài kiểm thử nào, vì vậy hãy xác định logic control-flow thực hiện type narrowing và các bài kiểm thử hiện có trước khi xác định thay đổi. Được xem là hoàn thành khi option.value được chấp nhận sau isSome(option.type), trong khi hành vi narrowing hiện có vẫn không thay đổi.
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