Skip to content

Commit a39f395

Browse files
committed
getMessages types: Make messages in migrateMessages a read-only array.
There's no reason we'd want to mutate this array. Given that, and the fact that we're about to make `ServerMessage` a union of `ServerMessageOf<PmMessage>` and `ServerMessageOf<StreamMessage>`, we might as well be courteous to `migrateMessages`' callers by allowing them to pass an array of just one of those types. In other words, we might as well allow this argument, which is an array, to be treated covariantly in the type of its elements. This is only possible when we the array is read-only. See discussion at zulip#4222 (comment), and in particular the example in "What about other types" at the bottom of this article: https://www.stephanboyer.com/post/132/what-are-covariance-and-contravariance.
1 parent 1462777 commit a39f395

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/api/messages/getMessages.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ type ServerApiResponseMessages = {|
4545
|};
4646

4747
/** Exported for tests only. */
48-
export const migrateMessages = (messages: ServerMessage[], identity: Identity): Message[] =>
48+
export const migrateMessages = (
49+
messages: $ReadOnlyArray<ServerMessage>,
50+
identity: Identity,
51+
): Message[] =>
4952
messages.map(message => ({
5053
...message,
5154
avatar_url: AvatarURL.fromUserOrBotData({

0 commit comments

Comments
 (0)