Skip to content

Commit

Permalink
getMessages types: Make messages in migrateMessages a read-only a…
Browse files Browse the repository at this point in the history
…rray.

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'd like to 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.
  • Loading branch information
chrisbobbe committed Mar 5, 2021
1 parent d465b25 commit e403cd4
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/api/messages/getMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ type ServerApiResponseMessages = {|
|};

/** Exported for tests only. */
export const migrateMessages = (messages: ServerMessage[], identity: Identity): Message[] =>
export const migrateMessages = (
messages: $ReadOnlyArray<ServerMessage>,
identity: Identity,
): Message[] =>
messages.map(message => ({
...message,
avatar_url: AvatarURL.fromUserOrBotData({
Expand Down

0 comments on commit e403cd4

Please sign in to comment.