Skip to content

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

Closed
@xLama

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

Metadata

Metadata

Assignees

Labels

DuplicateAn existing issue was already created

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions