Skip to content

Commit 2fd9e51

Browse files
gkzfacebook-github-bot
authored andcommitted
[flow][tuples] Allow tuple type spread of tuple-like arrays
Summary: Changelog: [internal] Allow tuple type spread of tuple-like arrays. This is required for the next diff to work - reversal of tuple type spread. Reviewed By: SamChou19815 Differential Revision: D55721534 fbshipit-source-id: e856bf2965ade26bc201782fe8da5d62cacc4d8b
1 parent 4cd054e commit 2fd9e51

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

src/typing/flow_js.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2238,7 +2238,7 @@ struct
22382238
| DefT (_, ArrT arrtype) ->
22392239
(* Arrays *)
22402240
(match (rrt_resolve_to, arrtype) with
2241-
| (ResolveSpreadsToTupleType _, (ArrayAT _ | ROArrayAT _)) ->
2241+
| (ResolveSpreadsToTupleType _, (ArrayAT { tuple_view = None; _ } | ROArrayAT _)) ->
22422242
(* Only tuples can be spread into tuple types. *)
22432243
add_output
22442244
cx

tests/tuples/spread.js

+12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ type ArraySpread = [...Array<mixed>]; // ERROR
5454
declare const x: ArraySpread;
5555
x as empty; // OK
5656
}
57+
type ROArraySpread = [...$ReadOnlyArray<mixed>]; // ERROR
58+
{
59+
declare const x: ROArraySpread;
60+
x as empty; // OK
61+
}
5762

5863
// Spreading `any`
5964
type AnySpread = [1, 2, ...any]; // OK
@@ -69,6 +74,13 @@ type EmptySpread = [1, 2, ...empty]; // OK
6974
x as empty; // OK
7075
}
7176

77+
// Tuple-like array spread
78+
{
79+
const x = [1, 2];
80+
type TupleArrSpread = [0, ...typeof x]; // OK
81+
[0, 1, 2] as TupleArrSpread; // OK
82+
}
83+
7284
// Generics
7385
type WithTail<T> = [1, ...T];
7486
[1, 2, 3] as WithTail<[2, 3]>; // OK

tests/tuples/tuples.exp

+27-13
Original file line numberDiff line numberDiff line change
@@ -1182,36 +1182,50 @@ References:
11821182
^^^^^^^^^^^^ [1]
11831183

11841184

1185-
Error --------------------------------------------------------------------------------------------------- spread.js:75:8
1185+
Error -------------------------------------------------------------------------------------------------- spread.js:57:22
1186+
1187+
Cannot spread non-tuple (read-only array type [1]) into tuple type. [tuple-invalid-type-spread]
1188+
1189+
spread.js:57:22
1190+
57| type ROArraySpread = [...$ReadOnlyArray<mixed>]; // ERROR
1191+
^^^^^^^^^^^^^^^^^^^^^^^^^^
1192+
1193+
References:
1194+
spread.js:57:26
1195+
57| type ROArraySpread = [...$ReadOnlyArray<mixed>]; // ERROR
1196+
^^^^^^^^^^^^^^^^^^^^^ [1]
1197+
1198+
1199+
Error --------------------------------------------------------------------------------------------------- spread.js:87:8
11861200

11871201
Cannot cast array literal to `WithTail` because number [1] is incompatible with number literal `3` [2] in index 2.
11881202
[incompatible-cast]
11891203

1190-
spread.js:75:8
1191-
75| [1, 2, 66] as WithTail<[2, 3]>; // ERROR
1204+
spread.js:87:8
1205+
87| [1, 2, 66] as WithTail<[2, 3]>; // ERROR
11921206
^^ [1]
11931207

11941208
References:
1195-
spread.js:75:28
1196-
75| [1, 2, 66] as WithTail<[2, 3]>; // ERROR
1209+
spread.js:87:28
1210+
87| [1, 2, 66] as WithTail<[2, 3]>; // ERROR
11971211
^ [2]
11981212

11991213

1200-
Error --------------------------------------------------------------------------------------------------- spread.js:78:1
1214+
Error --------------------------------------------------------------------------------------------------- spread.js:90:1
12011215

12021216
Cannot call `f` because `T` [1] is underconstrained by call of `f` [2]. Either add explicit type arguments or cast the
12031217
expression to your expected type. [underconstrained-implicit-instantiation]
12041218

1205-
spread.js:78:1
1206-
78| f([1, 2, 3]); // ERROR: annotation required - reversal not yet implemented
1219+
spread.js:90:1
1220+
90| f([1, 2, 3]); // ERROR: annotation required - reversal not yet implemented
12071221
^
12081222

12091223
References:
1210-
spread.js:77:20
1211-
77| declare function f<T>(xs: [1, ...T]): void;
1224+
spread.js:89:20
1225+
89| declare function f<T>(xs: [1, ...T]): void;
12121226
^ [1]
1213-
spread.js:78:1
1214-
78| f([1, 2, 3]); // ERROR: annotation required - reversal not yet implemented
1227+
spread.js:90:1
1228+
90| f([1, 2, 3]); // ERROR: annotation required - reversal not yet implemented
12151229
^^^^^^^^^^^^ [2]
12161230

12171231

@@ -1984,4 +1998,4 @@ References:
19841998

19851999

19862000

1987-
Found 124 errors
2001+
Found 125 errors

0 commit comments

Comments
 (0)