microsoft / microsoft/TypeScript

Introduce the opposite of the non-null assertion

Đang mở
#28,375 3 bình luận 3 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

non-null assertion opposite

Suggestion

The opposite of the "non-null assertion", used to force the compiler to believe that something may be undefined.

The most obvious syntax would be a "?" postfix operator, similar to how the non-null assertion is a "!" postfix operator. This would be consistent with the use of "?" for optional function params and object properties (which adds "undefined" to the type).

Use Cases

Indexing into arrays and "dictionary" objects with indexes/keys that are expected to potentially not exist.

Arrays and dictionary objects (an object type with the string index defined) are typically defined as having items whose type does NOT include undefined, even though it is technically possible (and valid) to use a non-existent index/key and get an undefined value as a result. In most situations, this avoids unnecessary/tedious non-null assertions, because arrays are often iterated (indexes guaranteed to exist), and dictionary objects are often used only to lookup keys that are guaranteed to exist.

However, in situations where an "untrusted" index/key is used on an array/dictionary, there is no convenient way to alter the type of the expression to indicate that the programmer is aware that the result may be undefined:

Examples

declare const array: Date[];
declare const untrustedIndex: number;

// Type is Date, but it may actually be undefined at runtime.
const item = array[untrustedIndex];

// To get useful "correct" typing, I must explicitly cast, duplicating the type 
// of the item and adding "| undefined" 
const item2 = array[untrustedIndex] as Date | undefined;

// Using the new proposed "possibly undefined assertion"
// Type is Date | undefined
const item3 = array[untrustedIndex]?;

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. new expression-level syntax)

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 repository, kiểm thử hoặc entry point nào được nêu. Hãy bắt đầu bằng cách xem xét cú pháp postfix được đề xuất cùng các ví dụ về array và dictionary, sau đó xác định các khu vực của compiler xử lý postfix assertion và truy cập theo chỉ mục. Công việc được xem là hoàn tất khi có một thiết kế đã được thống nhất và một triển khai được xác thực dựa trên hành vi kiểu được mô tả.

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
Cần làm rõ
Mức phù hợp với người mới
20/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.