diff --git a/src/analysis/env_builder/name_resolver.ml b/src/analysis/env_builder/name_resolver.ml index 8d643f03096..940e730a996 100644 --- a/src/analysis/env_builder/name_resolver.ml +++ b/src/analysis/env_builder/name_resolver.ml @@ -2955,8 +2955,13 @@ module Make (Context : C) (FlowAPIUtils : F with type cx = Context.t) : ignore @@ this#expression arg; let completion_state = this#run_to_completion (fun () -> - Base.Option.iter guard ~f:(fun guard -> ignore @@ this#expression guard); - ignore @@ this#expression body + match guard with + | Some guard -> + this#push_refinement_scope empty_refinements; + ignore @@ this#expression_refinement guard; + ignore @@ this#expression body; + this#pop_refinement_scope () + | None -> ignore @@ this#expression body ) in completion_states := completion_state :: !completion_states; diff --git a/tests/match/body.js b/tests/match/body.js index 8cb293fafce..df8b7ed3e72 100644 --- a/tests/match/body.js +++ b/tests/match/body.js @@ -67,3 +67,14 @@ function f2() { out as number; // OK } + +// Guards can refine values which are not the argument +{ + declare const y: number | string; + + const out = match (x) { + 1 if typeof y === 'number': y as number, // OK + const a if a === 1: a as 1, + _: 0, + }; +}