Skip to content

Destructuring object can´t be an optional type parameter with default value #4546

Description

@xLama

TS 1.7.0

This is valid ES6 from Mozilla but invalid TS:

function drawES6Chart({size = 'big', cords = { x: 0, y: 0 }, radius = 25} = {}) 
{
  console.log(size, cords, radius);
  // do some chart drawing
}

drawES6Chart({
  cords: { x: 18, y: 30 },
  radius: 30
});

To get it works, we must remove "= {}", so we can`t invoke function without arguments:

function drawES6Chart({size = 'big', cords = { x: 0, y: 0 }, radius = 25} ) 
{
  console.log(size, cords, radius);
  // do some chart drawing
}

drawES6Chart( { })  // ok
drawES6Chart()  // Error


But It works with array destructuring :

function drawES6Chart( [size = "big" ,cords = { x: 0, y: 0 }, radius = 25 ] = [] ) 
{
  console.log(size, cords, radius);
  // do some chart drawing
}
drawES6Chart([]); // Ok
drawES6Chart(); // Ok

I think empty object should work with default values:

var {size = 'big', cords = { x: 0, y: 0 } } = {} // Error

Should it work with no default values ?

var {p,q } = {} 

Firefox screenshoot:
Image

Activity

  1. changed the title [-]Destructuring object can´t be an optional type parameter[/-] [+]Destructuring object can´t be an optional type parameter with default value[/+] on Aug 30, 2015
  2. added
    DuplicateAn existing issue was already created
    and removed
    BugA bug in TypeScript
    on Aug 31, 2015
  3. ahejlsberg commented on Aug 31, 2015

    @ahejlsberg
    Member

    This looks like a duplicate of #2469. I agree we need a fix for this.

  4. ahejlsberg commented on Sep 1, 2015

    @ahejlsberg
    Member

    Fix now available as #4598.

  5. xLama commented on Sep 2, 2015

    @xLama
    Author

    Thanks. 👍

  6. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

DuplicateAn existing issue was already created

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions