-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Optional destructured parameters do not work with default values #17080
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
Milestone
Comments
@sandersn we talked about this one before, any thoughts here? |
Here's another example of this that I wrote up before searching for an existing issue: interface Foo {
readonly bar?: number;
}
function performFoo({ bar }: Foo = {}) {
useBar(bar); // `bar` should have type `number | undefined`, so this shouldn't compile.
}
function useBar(bar: number) {
alert('The number is: ' + bar);
}
performFoo(); We've started using this pattern in our code, so I'm also interested in a fix for this. |
If someone takes up fixing this, here is another potentially useful test case: interface Foo {
readonly bar?: number;
}
function performFoo({ bar = null }: Foo = {}) {
useBar(bar); // `bar` should have type `number | null`, so this call shouldn't compile.
}
function useBar(bar: number | undefined) {
alert('The number is: ' + bar);
}
performFoo(); |
Lazarus535
added a commit
to Lazarus535/TypeScript
that referenced
this issue
Jan 22, 2018
microsoft#17080 Added testcases from the Github bugreport (all working as intended now). Signed CLA.
Lazarus535
added a commit
to Lazarus535/TypeScript
that referenced
this issue
Jan 22, 2018
Fixed the two requested changes. 1) Deleting the file "pull_request_template.md" 2) Declaring functions in tests, instead of defining
Lazarus535
added a commit
to Lazarus535/TypeScript
that referenced
this issue
Jan 22, 2018
Readded untouched pull_request_template.md
sandersn
added a commit
that referenced
this issue
Jan 22, 2018
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
TypeScript Version: 2.4.0
Code
The following code incorrectly type checks:
Expected behavior:
This should fail to type check since
foo
andbar
may both be of typeundefined
Actual behavior:
Match behaviour of omitting default value:
Or behaviour of omitting destructuring:
The text was updated successfully, but these errors were encountered: