Ngxs patch<T> change the type to Object

18 views Asked by At

I'm specifying model just in case :

class Zoo {
    //things
    enclosures: Array<Enclosure>;
}

class Enclosure {
    //things
    animals: Array<Animal>;
}

class Animal {
    //things
}

I have this state:

export interface ZooStateModel {
    //things here
    
    selectedZoo: Zoo | undefined

    //things there
}

And this action:

@Action(CreateEnclosure)
createEnclosure(context: StateContext<ZooStateModel>, action: CreateEnclosure): void {
    // some logic
    
    this.enclosureService.createEnclosure(action.payload).pipe(
        tap((enclosure: Enclosure) => {
            console.log('before', context.getState())
            
            context.setState(patch<ZooStateModel>({
                selectedZoo: patch<Zoo>(
                    enclosures: append<Enclosure>([enclosure])
                )
            }));

            console.log('before', context.getState())
        })
    )
}

My problem is that the type of selectedZoo in the state changes from [_Zoo] to {...} [Object] when I console.log it, even if I specified the type in the patch<Zoo>.

I simplified the problem in the post and tried to keep it "animal" like the ngxs.io documentation seems to like it.

The more specific problem is something more like that (Where both Zoo and the updated enclosure lost their type but not the Animal either the other enclosures from this zoo):

@Action(AddAnimal)
addAnimal(context: StateContext<ZooStateModel>, action: AddAnimal): void {
    this.pieceService.createPiece(action.id, action.payload).pipe(
        tap((animal: Animal) => {
            console.log('before', context.getState())
            context.setState(patch<ZooStateModel>({
                selectedZoo: patch<Zoo>({
                    enclosures: updateItem<Enclosure>(
                        (enclosure => enclosure.id === action.id),
                        patch<Enclosure>({
                            animals: append<Animal>([animal])
                        })
                    ),
                })
            }))
            console.log('after', context.getState())
        })
    )
}
0

There are 0 answers