-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Distinguish missing and undefined also in arguments #44548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This was discussed quite a bit when we were making |
@RyanCavanaugh Or should I suggest this to typescript-eslint? func("aaa"); // =>"aaa"
func(); // =>"bbb"
func(null); // => null
func(undefined); //=>"bbb" 🤔 unnatural behavior……
function func( a = "bbb" )
{
console.log(a);
} |
It was in a meeting. Passing |
I got it, thanks! |
Hello, I have a function, see playground And pass undefined make type inferring wrong. So how should I do to get it correct? |
I created the issue #47202 and share the view of @sabberworm @KageShiron and @zyf0330. What I don't get in this suggestion, though, is what's the point of an optional parameter without a default vale? If it's not specified, what's the value of it then if not undefined? |
The difference is at the invocation side, and it's exactly the same as your other issue. The difference is whether it should be allowed to invoke the method with an |
@MartinJohns Thanks, I get it now. Then I also think that it's good the way it currently is. Sorry for interruping.. 😓 |
Seems reasonable, but shouldn't be more obvious? There are definitely different intentions for |
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
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
undefined
to 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.The text was updated successfully, but these errors were encountered: