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:
⭐ 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.
Suggestion
With the arrival of
--strictOptionalProperties, it’s clear that the intent of?as a modifier isn’t to add| undefinedto 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:
⭐ Suggestion
Given
calling
optionallyTakesAnArg()is not the same asoptionallyTakesAnArg(undefined). TypeScript should be able to model this. Specifically it should forbid the second form and only allow eitheroptionallyTakesAnArg()oroptionallyTakesAnArg(stringArg).📃 Motivating Example
This is mostly an issue when the argument is a generic type that may or may not include an explicit
undefined:💻 Use Cases
I want to model that calling
optionallyTakesAnArg<string>()andoptionallyTakesAnArg<string>(stringArg)both are valid but thatoptionallyTakesAnArg<string>(undefined)is not, whereasoptionallyTakesAnArg<undefined>()andoptionallyTakesAnArg<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
undefinedto be modeled correctly.⚡️ Open issues
?.(function(a = 1) { return a })()and(function(a = 1) { return a })(undefined)both evaluate to1).optionallyTakesAnArg(arg : string | void), which, AFAICT currently has the same semantics asoptionallyTakesAnArg(arg? : string). I’m not sure whether this should continue to be aligned or not.