Skip to content

Commit a74129a

Browse files
committed
updated reducer examples
1 parent e042a02 commit a74129a

File tree

2 files changed

+40
-46
lines changed

2 files changed

+40
-46
lines changed

README.md

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -722,42 +722,39 @@ initialState.counter = 3; // Error, cannot be mutated
722722
```
723723

724724
#### Caveat: Readonly does not provide recursive immutability on objects
725-
> This means that readonly modifier will not be propagated on nested properties of objects or arrays of objects.
725+
> This means that readonly modifier does not propagate immutability on nested properties of objects or arrays of objects. You'll need to set it explicitly on each nested property.
726726
727727
```ts
728728
export type State = {
729-
readonly counterContainer: { mutableCounter: number }
729+
readonly counterContainer: {
730+
readonly readonlyCounter: number,
731+
mutableCounter: number,
732+
}
730733
};
731734

732-
const state: State = {
733-
counterContainer: { mutableCounter: 0 },
734-
};
735+
state.counterContainer = { mutableCounter: 1 }; // Error, cannot be mutated
736+
state.counterContainer.readonlyCounter = 1; // Error, cannot be mutated
735737

736738
state.counterContainer.mutableCounter = 1; // No error, can be mutated
737739
```
738740

739-
> You can still achieve nested immutability but you'll need to explicitly mark every nested property as readonly. You can do this quite easily by using convenient `Readonly` or `ReadonlyArray` mapped types.
741+
> There are few utilities to help you achieve nested immutability. e.g. you can do it quite easily by using convenient `Readonly` or `ReadonlyArray` mapped types.
740742
741743
```ts
742-
type State = Readonly<{
743-
countersCollection: ReadonlyArray<Readonly<{
744-
readonlyCounter1: number,
745-
readonlyCounter2: number,
746-
}>>,
747-
}>;
748-
749-
const state: State = {
750-
countersCollection: [{
751-
readonlyCounter1: 0,
752-
readonlyCounter2: 0,
753-
}],
754-
};
755-
756-
state.countersCollection[0] = { readonlyCounter1: 0, readonlyCounter2: 0 }; // Error, cannot be mutated
757-
state.countersCollection[0].readonlyCounter1 = 1; // Error, cannot be mutated
758-
state.countersCollection[0].readonlyCounter2 = 1; // Error, cannot be mutated
744+
export type State = Readonly<{
745+
countersCollection: ReadonlyArray<Readonly<{
746+
readonlyCounter1: number,
747+
readonlyCounter2: number,
748+
}>>,
749+
}>;
750+
751+
state.countersCollection[0] = { readonlyCounter1: 1, readonlyCounter2: 1 }; // Error, cannot be mutated
752+
state.countersCollection[0].readonlyCounter1 = 1; // Error, cannot be mutated
753+
state.countersCollection[0].readonlyCounter2 = 1; // Error, cannot be mutated
759754
```
760755

756+
> _There are some experiments in the community to make a `ReadonlyRecursive` mapped type, but I'll need to investigate if they really works_
757+
761758
### Reducer with classic `const types`
762759

763760
```tsx

generator/content/2_redux.md

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,42 +65,39 @@ initialState.counter = 3; // Error, cannot be mutated
6565
```
6666

6767
#### Caveat: Readonly does not provide recursive immutability on objects
68-
> This means that readonly modifier will not be propagated on nested properties of objects or arrays of objects.
68+
> This means that readonly modifier does not propagate immutability on nested properties of objects or arrays of objects. You'll need to set it explicitly on each nested property.
6969
7070
```ts
7171
export type State = {
72-
readonly counterContainer: { mutableCounter: number }
72+
readonly counterContainer: {
73+
readonly readonlyCounter: number,
74+
mutableCounter: number,
75+
}
7376
};
7477

75-
const state: State = {
76-
counterContainer: { mutableCounter: 0 },
77-
};
78+
state.counterContainer = { mutableCounter: 1 }; // Error, cannot be mutated
79+
state.counterContainer.readonlyCounter = 1; // Error, cannot be mutated
7880

7981
state.counterContainer.mutableCounter = 1; // No error, can be mutated
8082
```
8183

82-
> You can still achieve nested immutability but you'll need to explicitly mark every nested property as readonly. You can do this quite easily by using convenient `Readonly` or `ReadonlyArray` mapped types.
84+
> There are few utilities to help you achieve nested immutability. e.g. you can do it quite easily by using convenient `Readonly` or `ReadonlyArray` mapped types.
8385
8486
```ts
85-
type State = Readonly<{
86-
countersCollection: ReadonlyArray<Readonly<{
87-
readonlyCounter1: number,
88-
readonlyCounter2: number,
89-
}>>,
90-
}>;
91-
92-
const state: State = {
93-
countersCollection: [{
94-
readonlyCounter1: 0,
95-
readonlyCounter2: 0,
96-
}],
97-
};
98-
99-
state.countersCollection[0] = { readonlyCounter1: 0, readonlyCounter2: 0 }; // Error, cannot be mutated
100-
state.countersCollection[0].readonlyCounter1 = 1; // Error, cannot be mutated
101-
state.countersCollection[0].readonlyCounter2 = 1; // Error, cannot be mutated
87+
export type State = Readonly<{
88+
countersCollection: ReadonlyArray<Readonly<{
89+
readonlyCounter1: number,
90+
readonlyCounter2: number,
91+
}>>,
92+
}>;
93+
94+
state.countersCollection[0] = { readonlyCounter1: 1, readonlyCounter2: 1 }; // Error, cannot be mutated
95+
state.countersCollection[0].readonlyCounter1 = 1; // Error, cannot be mutated
96+
state.countersCollection[0].readonlyCounter2 = 1; // Error, cannot be mutated
10297
```
10398

99+
> _There are some experiments in the community to make a `ReadonlyRecursive` mapped type, but I'll need to investigate if they really works_
100+
104101
### Reducer with classic `const types`
105102

106103
::example='../../playground/src/redux/counters/reducer.ts'::

0 commit comments

Comments
 (0)