-
Notifications
You must be signed in to change notification settings - Fork 12.8k
required function parameters with default values #47202
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
For the record, a parameter with a default value can't be C:\Users\fatce>node
Welcome to Node.js v17.3.0.
Type ".help" for more information.
> function foo(x = 812) { console.log(x); }
undefined
> foo()
812
undefined
> foo(undefined)
812
undefined
> |
@fatcerberus wow, I didn't realize that. That makes it even more obvious that we need to change something! Probably even without new syntax. |
@VanCoding It’s actually useful because it lets you omit arguments in the middle of a call if they have default values while still providing explicit values for later ones. If it were an error to pass |
oooohhh! Now I understand, the type is different inside the function from outside the function. Yes, this makes sense! I only now realise that my initial example would compile, because inside the function the printer cannot be undefined. I think what initially led me to believe that there's a problem is that We can close this now. Thanks guys! |
Well strictly speaking it can be undefined, right?
Nevertheless, going back to the original point that @VanCoding made, what can one do in strict mode for default values of parameters? Say I have the following code
then this would result in a compiler error like
And it was made clear that y is always going to be a number because of either the type of So why is this an error? PS: Sorry if I should have opened a new issue or put this anywhere else, I am new here! |
It would not. It works just fine. |
Suggestion
🔍 Search Terms
required parameter default value non-optional function
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
It should be possible to mark a function parameter with a default value as required, so that it can't be undefined.
📃 Motivating Example
Consider the following function:
It's clear that it needs a printer to work. But in this form, TypeScript would allow the following call to the function:
Which is also why it would complain that printer is possibly undefined in strict mode. Even when the type of the parameter is explicitly defined, it can still be undefined when it has a default value. This is because an argument being optional is tightly coupled with it having a default value.
But just because an argument has a default value, it shouldn't mean that it can be undefined. It should just mean, that it's automatically assigned, if it's not provided in the function call.
In a perfect world, I'd propose to make this the default behavior, and add a feature to mark arguments that are truly optional with a "?" after the name. But since that would break existing code, I propose the opposite: Marking required properties with a "!".
Example:
💻 Use Cases
I like to make function dependencies overridable by optional parameters that default to the main implementation, like in the example above, or grouped to an object like this:
This makes the function unit testable in a very easy way, and doesn't need a dependency injection framework for the real application.
But this currently only works with strictNullChecks turned off, because otherwise typescript would complain that the dependencies could be undefined.
The text was updated successfully, but these errors were encountered: