Skip to content

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

Closed
@billti

Description

@billti

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

Metadata

Metadata

Assignees

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