Skip to content

Possible bug: Complex named function params with defaults and rest causes errors. #14026

@dsifford

Description

@dsifford

TypeScript Version: 2.1.1 & rc

Sorry if this has already been reported. I searched around and couldn't find exactly this.

This is sort of difficult to describe, so here's an example:

interface Foo {
    bar?: any;
    baz?: any;
}

function foobar({ bar = {}, ...opts }: Foo = {}) {
    console.log(bar);
    console.log(opts.baz);
}

foobar(); // Throws error
foobar({ baz: 'hello' }); // No error
foobar({ bar: { greeting: 'hello' } }); // No error

Playground example

Expected behavior:
I'd expect bar to still be initialized and available in the function call as an empty object when no args are given to the function.

Actual behavior:

The following error gets thrown (reproducible in the playground example above).

Uncaught TypeError: Cannot read property 'bar' of undefined
    at foobar (<anonymous>:11:16)
    at <anonymous>:16:1
    at HTMLButtonElement.excuteButton.onclick (http://www.typescriptlang.org/play/playground.js:242)

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions