This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deepCopy(obj) { | |
return JSON.parse(JSON.stringify(obj)); // serialize -> deserialize -> unique clone | |
} | |
const original = { | |
a: undefined, | |
b: { c: undefined }, | |
d: () => undefined | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deepCopy(obj) { | |
return JSON.parse(JSON.stringify(obj)); // serialize -> deserialize -> unique clone | |
} | |
const animals = { | |
zebra: { | |
food: ['leaves', 'bark'], | |
name: 'zebra' | |
}, | |
panda: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type Immutable<T> = T extends (infer R)[] | |
? ImmutableArray<R> | |
: T extends Function | |
? T | |
: T extends object | |
? ImmutableObject<T> | |
: T; | |
interface ImmutableArray<T> extends ReadonlyArray<Immutable<T>> {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const obj = { | |
a: 1, | |
b: 2, | |
c: { | |
d: 3 | |
} | |
}; | |
obj.newField = 5; // You can assign new field |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm install tslint-immutable --save-dev | |
// tslint.json | |
{ | |
"extends": ["tslint-immutable"], | |
"rules": { | |
// Recommended built-in rules | |
"no-var-keyword": true, | |
"no-parameter-reassignment": true, | |
"typedef": [true, "call-signature"], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
INCORRECT CHANGES (deep mutation) | |
*/ | |
const animals = { | |
zebra: { | |
food: ['leaves', 'bark'], | |
name: 'zebra' | |
}, | |
panda: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const animals = { | |
zebra: { | |
food: ['leaves', 'bark'], | |
name: 'zebra' | |
}, | |
panda: { | |
food: [], | |
name: 'panda' | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@NgModule({ | |
imports: [ | |
BrowserModule, | |
NgxsModule.forRoot([ AppState ], { developmentMode: !env.production }) | |
], | |
... | |
}) | |
export class AppModule { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<b>default</b>: {{ todos$ | async | json }} <br> | |
<b>immutable reverse</b>: {{ immutableTodos$ | async | json }} <br> | |
<b>mutable reverse</b>: {{ mutableTodos$ | async | json }} <br> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@State<number[]>({ | |
name: 'todos', | |
defaults: [1, 2, 3] | |
}) | |
export class AppState { | |
@Selector() public static mutableTodos(state: number[]): number[] { | |
return state.reverse(); | |
} |