typing_status: Mark recipient array as read-only for Flow.

This type means that code consuming this value promises not to
mutate it.  It's useful partly for the sake of simply controlling
mutation, so that arrays can be passed around without making
defensive copies; and partly because it makes the type covariant
in the elements, rather than invariant.

That is, if a function takes a plain Array<number | null>, then you
can't pass it an Array<number>, because it might add a `null` to it.
But if it takes $ReadOnlyArray<number | null>, then you can.
In general, Array<S> <: $ReadOnlyArray<S> <: $ReadOnlyArray<T>
for any S <: T, where `<:` means "is a subtype of".

Marking this type as read-only means we can pass in a read-only
array without adding a fixme (equivalent to a mypy type-ignore) to
locally disable the type-checker, nor a redundant defensive copy.
This commit is contained in:
Greg Price
2020-12-29 14:16:37 -08:00
parent 93101c953f
commit 9a15301f4a

View File

@@ -4,7 +4,7 @@
"use strict";
type RecipientUserIds = number[];
type RecipientUserIds = $ReadOnlyArray<number>;
type Worker = {|
get_current_time: () => number, // as ms since epoch