microsoft / microsoft/TypeScript
Permit functions that return a value to also serve as a type guard
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
Linear type, affine type, type guard
Suggestion
It would be very helpful to allow a function to serve as a type guard, but also return an unrelated value.
Use Cases
This can be used to express type changes as a result of mutating operations, covering some of the use cases of e.g. Rust's affine types. (See also #16148.)
Examples
Consider this example, compiled with --strictNullChecks:
type NonEmptyArray<T> = {
pop(): T;
} & Array<T>;
function isNonEmpty<T>(array: Array<T>): array is NonEmptyArray<T>;
function isNonEmpty(array: Array<unknown>): boolean {
return array.length > 0;
}
let array: string[] = ['element'];
if (isNonEmpty(array)) { // Guard gives 'array' type NonEmptyArray<string>.
const elem1: string = array.pop(); // Works. This is correct.
const elem2: string = array.pop(); // Also works, but elem2 will be undefined at runtime!
}
We could make this correct if pop() could both return a value and behave as a type guard. This isn't great syntax, but nonetheless consider if this was supported:
type NonEmptyArray<T> = {
pop(): T && this is Array<T>;
} & Array<T>;
function isNonEmpty<T>(array: Array<T>): array is NonEmptyArray<T>;
function isNonEmpty(array: Array<unknown>): boolean {
return array.length > 0;
}
let array: string[] = ['element'];
if (isNonEmpty(array)) { // Guard gives 'array' type NonEmptyArray<string>.
const elem1: string = array.pop(); // Returns a string and gives 'array' type Array<string>.
const elem2: string = array.pop(); // Doesn't compile; pop() returns 'string | undefined'!
}
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
This can use a new, previously invalid syntax to avoid affecting any existing program.
- 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.)
There's no change in the code that's emitted; this feature would exist purely at the level of the type system.
- This feature would agree with the rest of TypeScript's Design Goals.
I believe that it would. It seems to be aligned well, in particular, with "Statically identify constructs that are likely to be errors."
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
Issue không nêu tệp nguồn, bài kiểm thử hoặc điểm vào của trình biên dịch nào. Hãy bắt đầu bằng cách xác định cách TypeScript xử lý các vị từ kiểu và kiểu trả về, sau đó xác định cú pháp được chấp nhận và hành vi thu hẹp kiểu, bao gồm ví dụ pop() thực hiện việc đột biến, cùng với các bài kiểm thử trình biên dịch bao quát các trường hợp mới và không hợp lệ.
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