-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Optional parameters should reject undefined
parameter values
#10014
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
I don't think this make sense:
(1) means that by definition, under If anything, |
Thanks for the explanation. I agree with you if it is TypeScript's policy. However, (2) has different behavior.
And it can be discriminated by overloads. Additionally, if optional parameters can reject |
@sandersn More, TypeScript shouldn't allow mixed parameter when calling. function f(a?: number, b?: number) {
}
f(undefined, 0); // bad style, should be an error It is not allowed as a type. // index.ts(1,24): error TS1016: A required parameter cannot follow an optional parameter.
function f(a?: number, b: number) {
} So optional parameters should reject |
I don't think the similarity between function definition and calling holds. You don't want to penalize a caller for calling an ugly function. So The fact remains that the type of optional parameters includes undefined, so it would be surprising if you couldn't pass undefined to them. |
I believe it should use a data structure and destructuring assignment, this is a basic practice to deal partial data. function f({a = 0, b}: { a?: number; b: number; }) {
}
f({a: undefined, b: 1}); related: #10030 |
I agree that |
I'm very glad if TypeScript supports this penalization by option like |
|
This is an actual problem but this solution is also unrealistic. This is a design limitation of optional parameters. |
For example, a following signature of function
f
should be defined like functiong
with strictNullChecks option.TypeScript Version: master
Code
Expected behavior:
Actual behavior:
The text was updated successfully, but these errors were encountered: