Skip to content

Commit

Permalink
mixedArray and mixedDict now return readonly (upgrade Flow)
Browse files Browse the repository at this point in the history
See these Flow issues for explanation:

- facebook/flow#7684
- facebook/flow#7685

This is a breaking change.

Fixes #3.
  • Loading branch information
lydell committed Jun 7, 2019
1 parent 0e785e2 commit 25e7cf6
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ const personDecoder: (mixed) => Person = record({

#### `mixedArray`

`(value: mixed) => Array<mixed>`
`(value: mixed) => $ReadOnlyArray<mixed>`

Usually you want to use [array] instead. `array` actually uses this decoder
behind the scenes, to verify that `value` is an array (before proceeding to
Expand All @@ -663,7 +663,7 @@ undefined, null and missing values][example-missing-values].

#### `mixedDict`

`(value: mixed) => { [string]: mixed }`
`(value: mixed) => { +[string]: mixed }`

Usually you want to use [dict] or [record] instead. `dict` and `record` actually
use this decoder behind the scenes, to verify that `value` is an object (before
Expand Down
2 changes: 1 addition & 1 deletion flow/rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ verifyCamel(
verifyCamel(
// $ExpectError
map(
// $ExpectError
record({
first_name: string,
age: number,
}),
// $ExpectError
({ fist_name: firstName, ...rest }) => ({ firstName, ...rest })
)
);
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"eslint-plugin-jest": "22.6.4",
"eslint-plugin-prettier": "3.1.0",
"eslint-plugin-simple-import-sort": "3.1.1",
"flow-bin": "0.95.1",
"flow-bin": "0.100.0",
"jest": "24.8.0",
"npm-run-all": "4.1.5",
"prettier": "1.18.0",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export function string(value: mixed): string {
return value;
}

export function mixedArray(value: mixed): Array<mixed> {
export function mixedArray(value: mixed): $ReadOnlyArray<mixed> {
if (!Array.isArray(value)) {
throw new TypeError(`Expected an array, but got: ${repr(value)}`);
}
return value;
}

export function mixedDict(value: mixed): { [string]: mixed } {
export function mixedDict(value: mixed): { +[string]: mixed } {
if (typeof value !== "object" || value == null || Array.isArray(value)) {
throw new TypeError(`Expected an object, but got: ${repr(value)}`);
}
Expand Down
6 changes: 3 additions & 3 deletions typescript/decoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ boolean(undefined);
number(undefined);
// $ExpectType string
string(undefined);
// $ExpectType unknown[]
// $ExpectType readonly unknown[]
mixedArray(undefined);
// $ExpectType { [key: string]: unknown; }
// $ExpectType { readonly [key: string]: unknown; }
mixedDict(undefined);
// $ExpectType true
constant(true)(undefined);
Expand Down Expand Up @@ -88,7 +88,7 @@ fieldAndThen("", string, () => string)(undefined);
fieldAndThen(0, string, () => string)(undefined);
// $ExpectType string | number
either(string, number)(undefined);
// $ExpectType string | number | boolean | { [key: string]: unknown; }
// $ExpectType string | number | boolean | { readonly [key: string]: unknown; }
either(either(boolean, string), either(number, mixedDict))(undefined);
// $ExpectType string
lazy(() => string)(undefined);
Expand Down
4 changes: 2 additions & 2 deletions typescript/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export function number(value: unknown): number;

export function string(value: unknown): string;

export function mixedArray(value: unknown): Array<unknown>;
export function mixedArray(value: unknown): ReadonlyArray<unknown>;

export function mixedDict(value: unknown): { [key: string]: unknown };
export function mixedDict(value: unknown): { readonly [key: string]: unknown };

export function constant<
T extends boolean | number | string | undefined | null
Expand Down

0 comments on commit 25e7cf6

Please sign in to comment.