diff --git a/src/typing/flow_js.ml b/src/typing/flow_js.ml index 09d8856836e..6df064da74f 100644 --- a/src/typing/flow_js.ml +++ b/src/typing/flow_js.ml @@ -2238,7 +2238,7 @@ struct | DefT (_, ArrT arrtype) -> (* Arrays *) (match (rrt_resolve_to, arrtype) with - | (ResolveSpreadsToTupleType _, (ArrayAT _ | ROArrayAT _)) -> + | (ResolveSpreadsToTupleType _, (ArrayAT { tuple_view = None; _ } | ROArrayAT _)) -> (* Only tuples can be spread into tuple types. *) add_output cx diff --git a/tests/tuples/spread.js b/tests/tuples/spread.js index 991f7bd7424..f1304d8e6d8 100644 --- a/tests/tuples/spread.js +++ b/tests/tuples/spread.js @@ -54,6 +54,11 @@ type ArraySpread = [...Array]; // ERROR declare const x: ArraySpread; x as empty; // OK } +type ROArraySpread = [...$ReadOnlyArray]; // ERROR +{ + declare const x: ROArraySpread; + x as empty; // OK +} // Spreading `any` type AnySpread = [1, 2, ...any]; // OK @@ -69,6 +74,13 @@ type EmptySpread = [1, 2, ...empty]; // OK x as empty; // OK } +// Tuple-like array spread +{ + const x = [1, 2]; + type TupleArrSpread = [0, ...typeof x]; // OK + [0, 1, 2] as TupleArrSpread; // OK +} + // Generics type WithTail = [1, ...T]; [1, 2, 3] as WithTail<[2, 3]>; // OK diff --git a/tests/tuples/tuples.exp b/tests/tuples/tuples.exp index f38325bf0ec..7785512627a 100644 --- a/tests/tuples/tuples.exp +++ b/tests/tuples/tuples.exp @@ -1182,36 +1182,50 @@ References: ^^^^^^^^^^^^ [1] -Error --------------------------------------------------------------------------------------------------- spread.js:75:8 +Error -------------------------------------------------------------------------------------------------- spread.js:57:22 + +Cannot spread non-tuple (read-only array type [1]) into tuple type. [tuple-invalid-type-spread] + + spread.js:57:22 + 57| type ROArraySpread = [...$ReadOnlyArray]; // ERROR + ^^^^^^^^^^^^^^^^^^^^^^^^^^ + +References: + spread.js:57:26 + 57| type ROArraySpread = [...$ReadOnlyArray]; // ERROR + ^^^^^^^^^^^^^^^^^^^^^ [1] + + +Error --------------------------------------------------------------------------------------------------- spread.js:87:8 Cannot cast array literal to `WithTail` because number [1] is incompatible with number literal `3` [2] in index 2. [incompatible-cast] - spread.js:75:8 - 75| [1, 2, 66] as WithTail<[2, 3]>; // ERROR + spread.js:87:8 + 87| [1, 2, 66] as WithTail<[2, 3]>; // ERROR ^^ [1] References: - spread.js:75:28 - 75| [1, 2, 66] as WithTail<[2, 3]>; // ERROR + spread.js:87:28 + 87| [1, 2, 66] as WithTail<[2, 3]>; // ERROR ^ [2] -Error --------------------------------------------------------------------------------------------------- spread.js:78:1 +Error --------------------------------------------------------------------------------------------------- spread.js:90:1 Cannot call `f` because `T` [1] is underconstrained by call of `f` [2]. Either add explicit type arguments or cast the expression to your expected type. [underconstrained-implicit-instantiation] - spread.js:78:1 - 78| f([1, 2, 3]); // ERROR: annotation required - reversal not yet implemented + spread.js:90:1 + 90| f([1, 2, 3]); // ERROR: annotation required - reversal not yet implemented ^ References: - spread.js:77:20 - 77| declare function f(xs: [1, ...T]): void; + spread.js:89:20 + 89| declare function f(xs: [1, ...T]): void; ^ [1] - spread.js:78:1 - 78| f([1, 2, 3]); // ERROR: annotation required - reversal not yet implemented + spread.js:90:1 + 90| f([1, 2, 3]); // ERROR: annotation required - reversal not yet implemented ^^^^^^^^^^^^ [2] @@ -1984,4 +1998,4 @@ References: -Found 124 errors +Found 125 errors