Skip to content

Destructured Typing with variables after a rest elements #1818

@tlgreg

Description

@tlgreg

When destructured typing is used with rest elements in the middle, the last variable is not typed currently (0.11.0)

This for example can cause a TS error when the number of elements does not match in tuple types.

Input

[first:: string, ...rest:: number[], last:: string] := list

Expected Output

const [first, ...rest]: [string, ...number[], string] =
    list,
  [last] = rest.splice(-1);

Current Output

const [first, ...rest]: [string, ...number[]] =
    list,
  [last] = rest.splice(-1);

Notes about types

In the above example rest becomes [...number[], string] — essentially equals to (string | number)[] instead of number[] — and last will be string | number | undefined.

Meaning typing last will not just be incorrect itself, but also screw up the types for rest too.

TS itself is problematic with extracting the types from tuples like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions