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 and optional properties on an interface #4647

Closed
nycdotnet opened this issue Sep 4, 2015 · 3 comments
Closed

Destructuring and optional properties on an interface #4647

nycdotnet opened this issue Sep 4, 2015 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@nycdotnet
Copy link

Hi,

I am trying to perform a destructuring assignment in a method signature using a variable that is declared as an interface that has an optional property. I'm getting an error when specifying multiple levels of defaults.

Please take a look at the following code. I expected test number 3 to compile cleanly, but it gives an error. Tests 2 and 5 give what I think is an appropriate error. The rest compile cleanly.

For test 3, since a default is provided for B, I don't think it should be considered mandatory.

This is with TypeScript 1.6 beta. Please let me know if I'm doing something wrong.

Thanks, team!

-Steve O

interface AB {
  A: string;
  B?: string;
}

const childObject: AB = {
  A: "test"
}

const parentObject = {
  childObject
};

function function1({childObject: {A, B = ""}}) {
  console.log(A);
  console.log(B);
}

function function2({childObject: {A, B = ""} = {A: "", B: ""}}) {
  console.log(A);
  console.log(B);
}

function function3({childObject: {A, B} = {A: "", B: ""}}) {
  console.log(A);
  console.log(B);
}

/* Test #1: Compiles cleanly */
function1(parentObject);

/*
  Test #2: Gives expected error.
  Error: Argument of type '{}' is not assignable to parameter of type '{ childObject: { A: any; B?: string; }; }'. Property 'childObject' is missing in type '{}'.
*/
function1({});

/*
  Test #3: Not sure why this one is giving an error.  A default is provided for B if the object exists, so
  technically B is not required.  I would have only expected to see this error on Test #5 below.
  Argument of type '{ childObject: AB; }' is not assignable to parameter of type '{ childObject?: { A: string; B: string; }; }'. Types of property 'childObject' are incompatible. Type 'AB' is not assignable to type '{ A: string; B: string; }'.
  Property 'B' is optional in type 'AB' but required in type '{ A: string; B: string; }'.
*/
function2(parentObject);

/* Test #4:  Compiles cleanly */
function2({});

/*
  Test #5: Gives expected error.  (Same as #3 but this one makes sense.)
  Argument of type '{ childObject: AB; }' is not assignable to parameter of type '{ childObject?: { A: string; B: string; }; }'. Types of property 'childObject' are incompatible. Type 'AB' is not assignable to type '{ A: string; B: string; }'.
  Property 'B' is optional in type 'AB' but required in type '{ A: string; B: string; }'.
*/
function3(parentObject);

/* Test #6: Compiles cleanly */
function3({});
@nycdotnet
Copy link
Author

By the way - using this code on babeljs.io logs "test" and "b1" which is what I would have expected from test number 3 above. Also, the TypeScript code works as expected - it's just the type error that I'm concerned with.

function function2({childObject: {A, B = "b1"} = {A: "", B: "b2"}}) {
  console.log(A);
  console.log(B);
}

const childObject = {
  A: "test"
}

const parentObject = {
  childObject
};

function2(parentObject);

@danquirk
Copy link
Member

danquirk commented Sep 4, 2015

This #4598 will fix your issue.

@danquirk danquirk closed this as completed Sep 4, 2015
@danquirk danquirk added the Duplicate An existing issue was already created label Sep 4, 2015
@nycdotnet
Copy link
Author

Thanks.

@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

2 participants