Skip to content

Commit 6ab521b

Browse files
committed
flow: Forbid object-as-map when constructing Immutable.Maps
1 parent c5eb3f5 commit 6ab521b

File tree

6 files changed

+156
-168
lines changed

6 files changed

+156
-168
lines changed

docs/style.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
initializer at `let`.
3939
* [[link](#immutable-provide-type)] Always provide a type when
4040
writing an empty `Immutable` value.
41+
* [[link](#immutable-no-object-as-map)] Don't construct an `Immutable.Map`
42+
with an object-as-map.
4143
* [[link](#react-function-prop-defaults)] Don't use React
4244
`defaultProps` for function components.
4345
* [Internal to Zulip and our codebase](#zulip)
@@ -542,6 +544,26 @@ in Flow, or a design limitation of Flow, or an issue in the Flow types
542544
provided by Immutable.js, or some combination.)
543545

544546

547+
<div id="immutable-no-object-as-map" />
548+
549+
**Don't construct an `Immutable.Map` with an object-as-map**: Whenever you
550+
create a non-empty `Immutable.Map`, pass an array of entries instead of an
551+
object-as-map. For example:
552+
```js
553+
Immutable.Map([[HOME_NARROW_STR, [1, 2]]]) // good
554+
555+
Immutable.Map({ [HOME_NARROW_STR]: [1, 2] }) // BAD -- don't do
556+
```
557+
558+
This is essential in order to get effective type-checking of the value
559+
passed to the `Immutable.Map` function when a key is computed (like
560+
`HOME_NARROW_STR`) instead of literal (like 'foo'). In the Immutable type
561+
definition, the object-as-map branch involves an object type with an indexer
562+
property, and in general [Flow seems to have trouble with those][].
563+
564+
[Flow seems to have trouble with those]: https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/.60Immutable.2EMap.28.7B.20.5BSTRING_CONST.5D.3A.20.E2.80.A6.20.7D.29.60.20busted.20by.20Flow.20bug/near/1481088
565+
566+
545567
<div id="react-function-prop-defaults" />
546568

547569
**Don't use React `defaultProps` for function components**:

0 commit comments

Comments
 (0)