Skip to content

False negatives when destructuring an array of explicit length #21220

Closed

Description

TypeScript Version: typescript@2.7.0-dev.20180116

Code

function takes4(a: number, b: number, c: number, d: number) {}

const has4 = [1, 2, 3, 4];
const has2 = [1, 2];

const allDestructured = takes4(...has4);
const midDestructured = takes4(1, ...has2, 3);

Expected behavior:
TypeScript knows that has4 has 4 members and has2 has 2 members; it can infer that takes4's signature will be satisfied by either call.

Actual behavior:

Expected 4 arguments, but got a minimum of { number of explicit arguments } for each call to takes4.

Related:
I discovered this when I was trying to pass a deconstruction to bezier-js.Bezier's constructor. The actual error I'm getting is that my first explicit argument ({ x: 0, y: 0 }) is not assignable to the type number. I'm presuming that TS is choosing the wrong overload because it can't deduce that destructuring fulfills one of the overloads.

Activity

  1. mhegazy commented on Jan 17, 2018

    @mhegazy
    Contributor

    duplicate of #4130

    also note that fixed length arrays (or tuples) are not inferred by default, an explicit type annotation is still required.

  2. appsforartists commented on Jan 17, 2018

    @appsforartists
    Author

    The code I wrote in my codebase was destructuring an argument of the type [Point2D, Point2D]. I used literals in the reduction for simplicity, but I saw this bug with an explicit type too.

  3. 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

Assignees

No one assigned

    Labels

    DuplicateAn 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