microsoft / microsoft/TypeScript
Support `!==` operator in `is` and `asserts` return type syntax
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.4k
- Merge trung bình
- 1 ngày 19 giờ
- Pull request đã merge (30 ngày)
- 117
Mô tả
🔍 Search Terms
unknown, assert, narrow, undefined, "is not"
✅ 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 feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
This looks like something that would have been asked before, but it's hard to search for.
Right now asserts is a pretty magical syntax that lets you do:
function assert(proposition: unknown): asserts proposition {
if (!proposition) {
throw new Error()
}
}
function foo(value: unknown): {} | null {
assert(value !== undefined);
return value;
}
However, trying to express a more specialized function doesn't work
function assertNotUndefined<T>(value: T): asserts value is Exclude<T, undefined> {
if (value === undefined) {
throw new Error();
}
}
function foo(value: unknown): {} | null {
assertNotUndefined(value);
return value; // Doesn't work
}
This is because Exclude<unknown, undefined> is still just unknown, since unknown is not just an alias for {} | null | undefined. Assuming that won't change, how else can one express assertNotUndefined? Well, if we take the original assert function and try to retroactively rationalize its syntax as this
function assert(proposition: unknown): asserts proposition;
when called as
assert(proposition !== undefined)
(hand-waving) just specializes to this
function assert(proposition: unknown): asserts proposition !== undefined;
I don't care to bikeshed over whether the syntax should be asserts proposition is not undefined, whether the LHS or RHS operands can be flipped, whether != or other operators should exist, or if any type expressions other than primitive literals like undefined should be supported, so I'll leave that to the comments.
📃 Motivating Example
function assertNotUndefined(proposition: unknown): asserts proposition !== undefined;
function foo(value: unknown): {} | null {
assertNotUndefined(value);
return value; // Works!
}
💻 Use Cases
- What do you want to use this for?
A function that can excludeundefinedcan be composed with other functions, e.g.array.filter(isNotUndefined) - What shortcomings exist with current approaches?
There's no clean way to define such a function without ugly conditionals to handleunknown. - What workarounds are you using in the meantime?
Rewrite code in more verbose style, e.g. manually construct a new array using aforloop instead ofarray.filter.
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 các ví dụ tạo động lực trong issue và so sánh cú pháp assertion !== được đề xuất với cú pháp kiểu trả về is và asserts hiện có. Phạm vi của các toán tử và biểu thức kiểu được hỗ trợ vẫn cố ý chưa được xác định, vì vậy để hoàn thành sẽ cần thống nhất cú pháp và ngữ nghĩa, triển khai việc thu hẹp kiểu, và bổ sung mức độ bao phủ phù hợp cho compiler.
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
- 25/100