镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

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.

Activity

  1. DarylCantrell commented on Jun 12, 2017

    @DarylCantrell
    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,
    }
    
  2. robyoder commented on Oct 5, 2017

    @robyoder

    I'm having this issue as well.

  3. robyoder commented on Dec 31, 2017

    @robyoder

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

  4. sandersn commented on Jan 2, 2018

    @sandersn
    Member

    Yep, original repro works now.

  5. locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions