microsoft / microsoft/TypeScript

Allow composite values to be used in type predicates

Đang mở
#43,640 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Awaiting More Feedback Suggestion
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ả

Allow composite values to be used in type predicates

🔍 Search Terms

Is, composite type, complex type, composite value, complex value, type predicate, type guard

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

⭐ Suggestion

Type predicates allow you to define functions that can tell the TypeScript compiler if a parameter is of a certain type.
I suggest that this be extended to allow type predicates to narrow the type of composite objects involving parameters.
That is, the left-hand-side of is types should be allowed to include object literals.

📃 Example

Say you have a type that involves a subset of another type:

type MyType = {property1: "static text", property2: "Hello" | "World"};

A type predicate for this would always require passing in the property1 property, even though it’s unnecessary.
Now, you can create a type predicate that only takes the value of property2, but still gurantees the result type:

function foo(text: string): {property1: "static text", property2: text} is MyType {
    return ["Hello", "World"].includes(text);
};

I’ve run into the problem of being unable to do this while developing my website.
I had a tagged union:

type A = {t: "A", value: "X" | "Y"};
type B = {t: "B", value: string};
type U = A | B;

And I wanted to get a value of type A from user input, but default back to a B type if the value wasn’t
To do this, I hoped to be able to do this:

function zit(x: string): {t: "A", value: x} is A {
    return ["X", "Y"].includes(x);
};
function gob(x: string): U {
    return zit(x) ? {t: "A", value: x} : {t: "B", value: x};
};

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Không có tệp, bài kiểm thử hoặc điểm vào nào được nêu tên. Hãy bắt đầu bằng cách xem lại các ví dụ về type-predicate của issue và hành vi type-guard hiện có của trình biên dịch; thay đổi sẽ hoàn tất khi các kiểu đối tượng tổng hợp được chấp nhận trong các predicate và các kịch bản narrowing được nêu hoạt động mà không thay đổi JavaScript được phát ra.

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

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.