microsoft / microsoft/TypeScript

Distinguish missing and undefined also in arguments

Đang mở
#44,548 9 bình luận 6 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ả

Suggestion

With the arrival of --strictOptionalProperties, it’s clear that the intent of ? as a modifier isn’t to add | undefined to the type but to distinguish presence from absence.

I propose the same should apply to arguments.

🔍 Search Terms

optional argument, optional arguments, optional parameter, optional parameters

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code (unless behind a flag)
  • 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

Given

function optionallyTakesAnArg(arg? : string) {
	return arguments.length;
}

calling optionallyTakesAnArg() is not the same as optionallyTakesAnArg(undefined). TypeScript should be able to model this. Specifically it should forbid the second form and only allow either optionallyTakesAnArg() or optionallyTakesAnArg(stringArg).

📃 Motivating Example

This is mostly an issue when the argument is a generic type that may or may not include an explicit undefined:

function optionallyTakesAnArg<T>(arg? : T) {
	if(arguments.length > 0) {
		// arg passed, do stuff
		doStuffWith(arg!);
	} else {
		// no arg passed, do something else
		doOtherStuff();
	}
}

💻 Use Cases

I want to model that calling optionallyTakesAnArg<string>() and optionallyTakesAnArg<string>(stringArg) both are valid but that optionallyTakesAnArg<string>(undefined) is not, whereas optionallyTakesAnArg<undefined>() and optionallyTakesAnArg<undefined>(undefined) are (but may not do the same thing).

Further, overloads should be able to distinguish between the latter two cases, e.g. for inference of return types.

This allows arguments whose type is a generic that may include undefined to be modeled correctly.

⚡️ Open issues

  • Arrow functions don’t have an arguments object in scope so the only way to implement this would be with rest params and tuples, which means they, too, would need new semantics for ?.
  • This proposal is at odds with default arguments which always treat absent and undefined the same ((function(a = 1) { return a })() and (function(a = 1) { return a })(undefined) both evaluate to 1).
  • It’s also possible to model optional args using optionallyTakesAnArg(arg : string | void), which, AFAICT currently has the same semantics as optionallyTakesAnArg(arg? : string). I’m not sure whether this should continue to be aligned or not.

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

Issue không nêu tệp hoặc bài kiểm thử nào. Hãy bắt đầu bằng cách xem xét các ví dụ về tham số tùy chọn và hành vi hiện tại đối với các lời gọi có tham số bị bỏ qua, undefined, generic, overloaded, default và rest. Công việc được xem là hoàn tất khi hệ thống kiểu có thể phân biệt sự hiện diện với sự vắng mặt như đề xuất, mà không thay đổi hành vi runtime của JavaScript, bao gồm các mối lo ngại đã nêu về hàm arrow và đối số default.

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

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.