Skip to content

BindingRestElement should be inferred as tuple if possible #26007

@ajafff

Description

@ajafff

TypeScript Version: 3.1.0-dev.20180727

Search Terms: tuple rest

Code

declare let tuple: [string, number, boolean];
let [a, ...rest] = tuple;

Expected behavior:

rest is of type [number, boolean];

Actual behavior:

rest is inferred as (number | boolean)[] which loses type information.

Note that the behavior in @next is a bit better than 2.9.1, as it was (string | number | boolean)[] back then.

Playground Link: https://agentcooper.github.io/typescript-play/?target=5#code/CYUwxgNghgTiAEEQBd7IK4AckC54G0BnZGASwDsBzAGnnPQFsAjEGWpgew6SnIF0A3ACgkqfFFoA6aUzB94AXjRYkAoA

Related Issues:
#21519

When #6275 is fixed these two variants should have the same inference (although I don't know why someone would write something like that):

declare let tuple: [string, number, boolean];

let [a, b, c] = tuple;
// or
let [a, ...[b, ...{0: c}]] = tuple;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions