-
-
Notifications
You must be signed in to change notification settings - Fork 651
/
jsonable.js
39 lines (37 loc) · 1.3 KB
/
jsonable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* @flow strict */
/**
* Type of a round-trip-capable JSONable object.
*
* Note that this is not the same as the type of objects which `JSON.stringify`
* will accept, nor even the type of objects which `JSON.stringify` will accept
* without returning `undefined`. For example,
*
* JSON.stringify({f: 2, g: () => 3, h: undefined}) === '{"f":2}'
*
* which of course will not be equal to the original object after parsing.
*/
// eslint-disable-next-line flowtype/type-id-match
export type JSONable =
| null
| string
| number
| boolean
| { +[string]: JSONable } /* [α] */
| $ReadOnlyArray<JSONable>;
// [α]: This should really be an exact type, `{| +[string]: JSONable |}`.
// Unfortunately, it can't yet be.
//
// Prior to Flow v0.111.0 (i.e., prior to React Native v0.62.0), exact object
// types with indexer properties are unusably broken. The following trivial
// example fails to typecheck:
//
// const val: {| [string]: number |} = { foo: 3 };
//
// On the other hand, inexact indexer-property types don't actually have their
// properties typechecked at their point of use -- that is, the following passes
// typechecking (even after v0.111.0):
//
// const val: { [string]: number } = { foo: 3, bar: 'baz' };
//
// ... which is consistent with the definition of inexact types, if
// inconvenient.