microsoft / microsoft/TypeScript
Optional chaining should always include `undefined` in its resolved type
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ả
TypeScript Version: 3.7.5
Search Terms: optional chaining return type
Code
I have a formatting function, where the input should always be a string, but in certain cases, from the backend, it's undefined for some reason. So to make sure the front-end at least doesn't crash when that happens, I added optional chaining, which fixed the issue.
But the types are kind of off now, and I didn't realize until now because Typescript didn't complain about anything.
export const formatAccountNumber = (account: string): string =>
account?.replace(/^(\d{4})(\d{2})(\d{5})/, '$1 $2 $3');
Expected behavior: Should get an error/warning about something here. Preferably, I guess optional chaining should always expand the type to include undefined to whatever it is added to? Then, in this example, Typescript could say that the return type is string, but I'm trying to return string | undefined? Either that, or maybe it shouldn't be possible to use optional chaining on a value that does not include either the type undefined and/or null?
Actual behavior: No warnings or errors.
Related Issues: no
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 bằng cách tái hiện ví dụ TypeScript 3.7.5 với optional chaining và kiểu trả về string tường minh. Xác định liệu cách sửa dự kiến là đưa undefined vào kiểu đã được phân giải hay từ chối optional chaining trên các giá trị không nullable; được coi là hoàn tất khi hành vi được chọn được thực thi nhất quán và được bao phủ bởi các bài kiểm thử trình biên dịch.
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
- Lỗi
- Độ 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