Skip to content

Commit

Permalink
[flow][numeric-keys] Better support numeric keys in some contextual t…
Browse files Browse the repository at this point in the history
…yping scenarios

Summary:
Better support numeric keys in some contextual typing scenarios.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D66911504

fbshipit-source-id: 98db0f413746ba441843602d2f724321d21788c9
  • Loading branch information
gkz authored and facebook-github-bot committed Dec 9, 2024
1 parent 52a4a2c commit aa6212f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/analysis/env_builder/eq_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ module Make
) ->
literal_check_of_expr value
|> Base.Option.value_map ~default:acc ~f:(fun check -> SMap.add name check acc)
| ( _,
Init
{
key =
Ast.Expression.Object.Property.NumberLiteral
(_, { Ast.NumberLiteral.value = key_value; _ });
value;
shorthand = _;
}
)
when Js_number.is_float_safe_integer key_value ->
let name = Dtoa.ecma_string_of_float key_value in
literal_check_of_expr value
|> Base.Option.value_map ~default:acc ~f:(fun check -> SMap.add name check acc)
| _ -> acc)
| Ast.Expression.Object.SpreadProperty _ -> acc
)
Expand Down
6 changes: 6 additions & 0 deletions tests/contextual_typing/sentinel_refinements.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ function function_statics() {
...
} = x => {}; // okay `x` inferred as `mixed`
}

// Numeric literal key names
type B = {0: 'a', f: (string) => string} | {0: 'b', f: number => number};
{
const b: B = {f: (v) => (v as string), 0: 'a'}; // OK
}

0 comments on commit aa6212f

Please sign in to comment.