From 447a2529bca8e3c635333f723137a20bd8a0c450 Mon Sep 17 00:00:00 2001 From: George Zahariev Date: Tue, 7 Jan 2025 14:29:50 -0800 Subject: [PATCH] [flow][match] Fix match refinement of arrays with tuple views Summary: One day natural inference will mean we don't need to think about this, but until then we need to handle it. Array literals initialized with some values act as a tuple. Handle them like we handle tuples. Changelog: [internal] Reviewed By: SamChou19815 Differential Revision: D67778713 fbshipit-source-id: a2b7bad3c8a02874027f82e2712cf797adc19275 --- src/typing/type_filter.ml | 7 ++++++- tests/match/patterns.js | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/typing/type_filter.ml b/src/typing/type_filter.ml index 3fcffa773c9..102ba3de8e8 100644 --- a/src/typing/type_filter.ml +++ b/src/typing/type_filter.ml @@ -599,7 +599,12 @@ let not_array t = let array_length ~sense ~op ~n t = match t with - | DefT (_, ArrT (TupleAT { arity = (num_req, num_total); inexact; _ })) -> + | DefT (_, ArrT (TupleAT { arity = (num_req, num_total); inexact; _ })) + | DefT + ( _, + ArrT + (ArrayAT { tuple_view = Some (TupleView { arity = (num_req, num_total); inexact; _ }); _ }) + ) -> (* `None` represents "maybe" a match *) let matches = match op with diff --git a/tests/match/patterns.js b/tests/match/patterns.js index 51a6caffa62..b745aa79571 100644 --- a/tests/match/patterns.js +++ b/tests/match/patterns.js @@ -160,3 +160,12 @@ _: 0, }; } + +// Array pattern applied to array literal +{ + const x = [1, 'foo']; + + const out = match (x) { + [_, const b]: b as string, // OK + }; +}