Skip to content

Instantly share code, notes, and snippets.

@LironHazan
Last active March 24, 2020 08:22
Show Gist options
  • Save LironHazan/10ba83735865885678bcbfef55b81586 to your computer and use it in GitHub Desktop.
Save LironHazan/10ba83735865885678bcbfef55b81586 to your computer and use it in GitHub Desktop.
Get new ref of your entity to achieve immutability (reduces bugs) and pass entity by ref when using onPush when working in Angular e.g.
class updateEditorExample {
private queryEditorState: Readonly<QueryEditorState>;
static updateEditorState (editorState: Readonly<QueryEditorState>, sliceToUpdate: Partial<Readonly<QueryEditorState>>) {
return { ...editorState, ...sliceToUpdate };
}
// Get new state:
doSomthingGood(newText: string) {
this.queryEditorState = updateEditorExample.updateEditorState(this.queryEditorState, { text: newText });
}
// Will throw compilation err:
doSomthingBad(newText: string) {
this.queryEditorState.text = newText;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment