Skip to content

Commit

Permalink
narrow types: Make Narrow an opaque type.
Browse files Browse the repository at this point in the history
In the preceding series of commits, we've taken all the places where
our code used to depend on the details of our internal representation
of narrows, and converted them so they use reasonable explicit
interfaces provided by the `narrow` module.

This commit recruits Flow to confirm for us that that job is complete.
Because `Narrow` is now an opaque type, code outside this module now
has to treat it as if it could mean anything: so it can't create a
value of this type any more than it could of type `empty`, and can't
consume one or break it down any more than it could a value of type
`mixed`, the supertype of all types.

Effectively this means that code outside `narrow` can only create a
`Narrow` by invoking something inside the module; can then pass it
around and store it however it pleases; but then can only inspect the
information inside it by passing it back to something within the
module, or to an operation like `===` or `JSON.stringify` that's
completely generic so that it actually accepts type `mixed` as input.

(The way this series was developed is that this commit came first,
immediately after distinguishing the `ApiNarrow` type and making
`Narrow` an alias of it in the first place.  That produced a long
list of Flow errors, which served as a to-do list to make the other
changes in the series.  Once complete, this commit was rebased to the
end so that, as always, no commit in the series breaks our tests.)
  • Loading branch information
gnprice authored and chrisbobbe committed Dec 17, 2020
1 parent ca745d9 commit 1ca184d
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils/narrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
* * `ApiNarrow` for the form we put a narrow in when talking to the
* server, and `apiNarrowOfNarrow` for converting to it.
*/
export type Narrow = ApiNarrow;
export opaque type Narrow = ApiNarrow;

export const isSameNarrow = (narrow1: Narrow, narrow2: Narrow): boolean =>
Array.isArray(narrow1) && Array.isArray(narrow2) && isEqual(narrow1, narrow2);
Expand Down

0 comments on commit 1ca184d

Please sign in to comment.