Last active
March 29, 2018 11:07
-
-
Save mateuszsokola/a2a1bdcdaf66e3d0507f8f48eb4c4393 to your computer and use it in GitHub Desktop.
This file contains 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
type State = { | |
numbers: Array<number>; | |
} | |
const initialState:State = { | |
numbers: [1, 2, 3, 4, 5, 6] | |
}; | |
const { numbers } = initialState; | |
// let's print the initial state | |
console.log('initial state', initialState); | |
// now, i want to retrieve a new state with only even numbers | |
const newState = { | |
...state, | |
numbers: numbers.filter((num:number):boolean => { | |
return num % 2 === 0; | |
}) | |
} | |
// and let's print the new state | |
console.log('new state', newState); |
This file contains 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
import { Record } from "immutable"; | |
type StateParams = { | |
numbers: Array<number>; | |
} | |
const defaultValues:StateParams = { | |
numbers: [] | |
} | |
// https://facebook.github.io/immutable-js/docs/#/Record | |
class State extends Record(defaultValues) { | |
numbers: Array<number>; | |
constructor(params: StateParams) { | |
super(params); | |
} | |
} | |
const initialState:State = new State(<StateParams>{ | |
numbers: [1, 2, 3, 4, 5, 6] | |
}) | |
const { numbers } = initialState; | |
// let's print the initial state | |
console.log('initial state', initialState); | |
// now, i want to retrieve a new state with only even numbers | |
const newState = initialState.set('numbers', numbers.filter((num:number):boolean => { | |
return num % 2 === 0; | |
})); | |
console.log('new state', newState); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You forgot cast types:
Otherwise Typescript compiler produces error