GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Destructuring and optional properties on an interface · Issue #4647 · microsoft/TypeScript

Destructuring and optional properties on an interface #4647

New issue
Copy link
New issue
Copy link
Closed
Closed
Destructuring and optional properties on an interface#4647
Copy link
Labels
DuplicateAn existing issue was already createdAn existing issue was already created

Description

@nycdotnet
Steve Ognibene (nycdotnet)
opened on Sep 4, 2015
Issue body actions

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({});

Activity

  1. nycdotnet commented on Sep 4, 2015

    @nycdotnet
    Steve Ognibene (nycdotnet)
    on Sep 4, 2015
    Author
    More actions

    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);
  2. danquirk commented on Sep 4, 2015

    @danquirk
    Dan Quirk (danquirk)
    on Sep 4, 2015
    Member
    More actions

    This #4598 will fix your issue.

  3. Dan Quirk (danquirk)
    closed this as completedon Sep 4, 2015
  4. Dan Quirk (danquirk)
    added
    DuplicateAn existing issue was already createdAn existing issue was already created
    on Sep 4, 2015
  5. nycdotnet commented on Sep 4, 2015

    @nycdotnet
    Steve Ognibene (nycdotnet)
    on Sep 4, 2015
    Author
    More actions

    Thanks.

  6. microsoft
    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

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.