Skip to content

Commit

Permalink
[flow] Update how typeof x === 'object' on `typeof x === 'function'…
Browse files Browse the repository at this point in the history
…` is empty

Summary: Changelog: [errors] If you have refiend a value based on `typeof x === 'function'`, and then do `typeof x === 'object'`, the result is now `empty` as that is impossible.

Reviewed By: SamChou19815

Differential Revision: D67031335

fbshipit-source-id: e0c11ddec6cc2dfc71000c600dab882fab6e3489
  • Loading branch information
gkz authored and facebook-github-bot committed Dec 10, 2024
1 parent facff9a commit 10d4393
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/typing/type_filter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ let rec object_ cx t =
| Mixed_non_maybe
| Mixed_non_null ->
obj |> changed_result
| Mixed_function
| Mixed_function -> DefT (reason_of_t t, EmptyT) |> changed_result
| Mixed_everything
| Mixed_non_void ->
let reason = replace_desc_new_reason RUnion (reason_of_t t) in
Expand Down
9 changes: 9 additions & 0 deletions tests/refinements/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,12 @@ const loop = (condition: boolean) => {
}
}
};

{
declare const x: mixed;
if (typeof x === 'function') {
if (typeof x === 'object') {
x as empty; // OK: Impossible to be both
}
}
}
33 changes: 32 additions & 1 deletion tests/refinements/refinements.exp
Original file line number Diff line number Diff line change
Expand Up @@ -9330,6 +9330,37 @@ References:
^^^^^^^^^^^^^^^^^^^ [1]


Error -------------------------------------------------------------------------------------------------- mixed.js:135:16

Refined at [1]

mixed.js:135:16
135| if (typeof x === 'object') {
^

References:
mixed.js:134:7
134| if (typeof x === 'function') {
^^^^^^^^^^^^^^^^^^^^^^^ [1]


Error --------------------------------------------------------------------------------------------------- mixed.js:136:7

Refined at [1] [2]

mixed.js:136:7
136| x as empty; // OK: Impossible to be both
^

References:
mixed.js:134:7
134| if (typeof x === 'function') {
^^^^^^^^^^^^^^^^^^^^^^^ [1]
mixed.js:135:9
135| if (typeof x === 'object') {
^^^^^^^^^^^^^^^^^^^^^ [2]


Error --------------------------------------------------------------------------------------------- mixed_object.js:2:32

Refined at [1]
Expand Down Expand Up @@ -17001,7 +17032,7 @@ References:



Found 1155 errors
Found 1157 errors

Only showing the most relevant union/intersection branches.
To see all branches, re-run Flow with --show-all-branches

0 comments on commit 10d4393

Please sign in to comment.