microsoft / microsoft/TypeScript

Easier use of indexed access types on nullable/optional types

Đang mở
#59,293 3 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.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

🔍 Search Terms

index access types optional nullable

✅ Viability Checklist
⭐ Suggestion

I would like a more succint way to access types from a nested object that contains nullable fields.

Possibly by allowing ! in a type-level position as a shorthand for NonNullable<T>

📃 Motivating Example

Generated types representing API responses often contain useful types that you'd prefer to extract rather than repeating them elsewhere. This comes up quite often for me when using graphql codegen on a query with a deeply nested response payload.

eg.

type APIResponse = {
	items: {
		id: string;
		author?: {
            id: string;
			name: string;
            status: "owner" | "admin" | "contributor" | "freeloader";
		}
	}[];
	pagination: {
		next_page: string;
	}
}

This example is a bit contrived, but is fairly representative of the general problem.

If I try and extract the status property, this doesn't work:

type APIAuthor = APIResponse['items'][0]['author']['status'];
//     Property 'status' does not exist on type '{ id: string; name: string; status: "owner" | "admin" | "contributor" | "freeloader"; } | undefined'.

Unless I wrap with NotNullable, and then it does.

type APIAuthor = NonNullable<APIResponse['items'][0]['author']>['status'];

Conceptually, I think the ! operator could work nicely here, giving us

type APIAuthor =APIResponse['items'][0]['author']!['status'];
💻 Use Cases

I've covered this a bit under the "motivating example" section, so maybe I've misused the headers a bit.

This comes up quite often for me when using graphql codegen on a query with a deeply nested response payload.

My current workaround is use of NonNullable, but when there's multiple layers of optionality it gets very difficult to follow.

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

Bắt đầu với ví dụ APIResponse mang tính định hướng và so sánh cú pháp ! ở cấp độ kiểu được đề xuất với workaround NonNullable hiện có. Xác định hành vi dự kiến đối với các truy cập được lập chỉ mục tùy chọn và nullable lồng nhau, sau đó định nghĩa các trường hợp chấp nhận cho thấy cú pháp rút gọn tạo ra cùng kiểu được trích xuất mà không thay đổi đầu ra JavaScript.

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.