Skip to content

Compiler does not detect impossible situation with default function parameters #15797

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

Closed
victornoel opened this issue May 12, 2017 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@victornoel
Copy link

TypeScript Version: 2.3.2

Code

// A *self-contained* demonstration of the problem follows...
function f({ a = false, b = '/' } = {}) {
}

f(true);
f('lol');
f(5);

Expected behavior:

The 3 calls to f should fail at compile time.

Actual behavior:

The calls do not fail.

Comment:

I suppose that typescript consider that the argument to the function is an object. And thus the parameter available inside the function body will simply be this same object but with two extra properties a and b set to their default values.
It makes sense, but it seems completely counter-intuitive to the programmer expectations :)

@mhegazy
Copy link
Contributor

mhegazy commented May 12, 2017

The signature of this function is f(arg: { a?: boolean, b?: string }): void;, since this type only has optional members, it is technically assignable from any type (as long as it does not have an a or a b with conflicting properties). This is another manifestation of #7485.

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 12, 2017
@victornoel
Copy link
Author

Closing this in favour of #7485. Thanks :)

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants