Skip to content

Instantly share code, notes, and snippets.

@mateuszsokola
Last active March 29, 2018 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mateuszsokola/a2a1bdcdaf66e3d0507f8f48eb4c4393 to your computer and use it in GitHub Desktop.
Save mateuszsokola/a2a1bdcdaf66e3d0507f8f48eb4c4393 to your computer and use it in GitHub Desktop.
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);
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);
@gagarin55
Copy link

You forgot cast types:

const newState: State = initialState.set('numbers', numbers.filter((num:number):boolean => {
    return num % 2 === 0;
})) as State;

Otherwise Typescript compiler produces error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment