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

Object rest destructuring parameter defaults to empty object, causing errors when untyped #12519

Closed
billti opened this issue Nov 27, 2016 · 1 comment · Fixed by #12564
Closed
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@billti
Copy link
Member

billti commented Nov 27, 2016

Using the latest release-2.1 branch, I'm seeing the following behavior.

I'm writing some React/JSX code, and using a pattern often seen in code in the wild, namely:

var fails = ({myId, ...others}) => <div id={myId}>{others.anything}</div>;

However, the above is in error, as the type inferred for ...others is the empty object ({}). Thus the expression within the DIV tags gives the error Property 'anything' does not exist on type '{}'.

If I explicitly type the parameter then all is OK. Namely:

interface Sample {
    myId: string;
    anything: number;
}

var works = ({myId, ...others}: Sample) => <div id={myId}>{others.anything}</div>;

It seems to me that for the ease of writing (and porting existing code), ...others above should default to any, as other untyped parameters do.

This may have already been discussed in some of the other rest/spread discussions. I did trying to find a definitive explanation, but those threads were quite verbose. Apologies if already covered.

Ping @sandersn , @RyanCavanaugh , @mhegazy

@sandersn
Copy link
Member

sandersn commented Nov 27, 2016 via email

@sandersn sandersn added the Bug A bug in TypeScript label Nov 27, 2016
@mhegazy mhegazy added this to the TypeScript 2.1.3 milestone Nov 28, 2016
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Dec 1, 2016
@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
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants