facebook / facebook/flow

Rest inside tuples

未關閉
#2,443 8 則留言 9 個 reaction 已指派 0 人 在 GitHub 檢視
feature request Typing: spread Typing: tuple
主要語言
Rust
星號
22.3k
分支
1.9k
PR 合併指標
30 天內沒有已合併 PR

描述

``` 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

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。