Skip to content
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

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

Closed
xLama opened this issue Aug 30, 2015 · 3 comments
Closed
Assignees
Labels
Duplicate An existing issue was already created

Comments

@xLama
Copy link

xLama commented Aug 30, 2015

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

@xLama xLama changed the title Destructuring object can´t be an optional type parameter Destructuring object can´t be an optional type parameter with default value Aug 30, 2015
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Aug 31, 2015
@ahejlsberg ahejlsberg self-assigned this Aug 31, 2015
@ahejlsberg ahejlsberg added this to the TypeScript 1.7 milestone Aug 31, 2015
@ahejlsberg ahejlsberg added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels Aug 31, 2015
@ahejlsberg
Copy link
Member

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

@ahejlsberg
Copy link
Member

Fix now available as #4598.

@xLama
Copy link
Author

xLama commented Sep 2, 2015

Thanks. 👍

@danquirk danquirk closed this as completed Sep 2, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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

3 participants