From e403cd4236cdd2aecd201e234d7c12101985891a Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Thu, 4 Mar 2021 17:14:58 -0500 Subject: [PATCH] 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` and `ServerMessageOf`, 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 https://github.com/zulip/zulip-mobile/pull/4222#discussion_r468263756, 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. --- src/api/messages/getMessages.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/api/messages/getMessages.js b/src/api/messages/getMessages.js index 14091b41f02..03cb198e07b 100644 --- a/src/api/messages/getMessages.js +++ b/src/api/messages/getMessages.js @@ -45,7 +45,10 @@ type ServerApiResponseMessages = {| |}; /** Exported for tests only. */ -export const migrateMessages = (messages: ServerMessage[], identity: Identity): Message[] => +export const migrateMessages = ( + messages: $ReadOnlyArray, + identity: Identity, +): Message[] => messages.map(message => ({ ...message, avatar_url: AvatarURL.fromUserOrBotData({