Skip to content

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

Closed
@victornoel

Description

@victornoel

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions