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

Type inference corner case in combination of object spread with destructuring #12532

Closed
Jessidhia opened this issue Nov 28, 2016 · 4 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@Jessidhia
Copy link

Jessidhia commented Nov 28, 2016

TypeScript Version: 2.2.0-dev.20161127

Code

declare var keyPrototype: any
declare function getKeyData(): Promise<{ key: any }>

async function getKey () {
  const { key } = {
    key: {
      ...keyPrototype
    },
    ...await getKeyData()
  }
  return key
}

Expected behavior:

Code to typecheck. The resulting key const should be a shallow copy of keyPrototype, with certain keys overridden by the result of getKeyData.

Actual behavior:

Error on the key declaration inside const { key }: "Initializer provides no value for this binding element and the binding element has no default value", despite the RHS being guaranteed to always have a key key.

sandersn added a commit that referenced this issue Apr 5, 2017
Extending symbols from untyped modules is no longer an error, so #12532
didn't get us anything except slightly better quick info.
@mhegazy mhegazy added the Bug A bug in TypeScript label Apr 27, 2017
@DarylCantrell
Copy link
Member

This is probably a duplicate. In TSX file, this works fine:

const { children, ...rest} = this.props;  // OK

When I try to do this, I get the same error you're seeing:

const { children, ...rest} = {    // Error
    ...this.props,
    someProp: forcedValue,
}

Works if I supply a default value, but I don't think it should require one:

const { children = null, ...rest} = {
    ...this.props,
    prop: forcedValue,
}

@robyoder
Copy link

robyoder commented Oct 5, 2017

I'm having this issue as well.

@robyoder
Copy link

This appears to be fixed in version 2.7.0-dev.20171230. Perhaps by #19513? Recommend for closing.

@sandersn
Copy link
Member

sandersn commented Jan 2, 2018

Yep, original repro works now.

@sandersn sandersn closed this as completed Jan 2, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 11, 2018
@mhegazy mhegazy added this to the TypeScript 2.7 milestone Jan 11, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants