facebook / facebook/flow

Rest inside tuples

Open
#2,443 8 comments 9 reactions 0 assignees View on GitHub
feature request Typing: spread Typing: tuple
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

``` js
type restStartTuple = [...number, string];

(["2"]: restStartTuple);
([1, "2"]: restStartTuple);
([1, 2, "3"]: restStartTuple);

([1, 2, 3]: restStartTuple); // FlowError
([1, "2", 3]: restStartTuple); // FlowError

type restMiddleTuple = [string, ...number, string];

(["1", "2"]: restMiddleTuple);
(["1", 2, "3"]: restMiddleTuple);
(["1", 2, 3, "4"]: restMiddleTuple);

(["1"]: restMiddleTuple); // FlowError
(["1", 2]: restMiddleTuple); // FlowError
([1, 2, 3]: restMiddleTuple); // FlowError
(["1", "2", "3"]: restMiddleTuple); // FlowError
```

The primary use case is to support rest arguments in functions in other positions (i.e. `func(...a, b)`)

This syntax would be most natural to people:

``` js
function restStart(...a: Array, b: string) {...}
function restMiddle(a: string, ...b: Array, c: string) {...}
```

However, since JavaScript does not support that kind of syntax we need a workaround. I would suggest this:

``` js
function restStart(...args: [...Array, string]) {...}
function restStart(...args: [string, ...Array, string]) {...}
```

So really there's two feature requests here: Support for rest inside tuples and for them to work with function arguments.

The current work around is to create libdefs with a bunch of possible combinations like here: https://github.com/flowtype/flow-typed/pull/260

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.