'Accessing an nested interface property with an index

The following (except updateXom()) is taken from https://ngneat.github.io/elf/docs/immer


import produce from "immer"
import { withProps, createStore } from '@ngneat/elf';
import { withEntities, selectAllEntities, updateEntities } from '@ngneat/elf-entities';

export function write<S>(updater: (state: S) => void): (state: S) => S {
  return function (state) {
    return produce(state, (draft) => {
      updater(draft as S);
    });
  };
}

export interface TodosProps {
  filter: 'ALL' | 'ACTIVE' | 'COMPLETED';
  nested: { address: {xom: 'k55'} }
}

const store = createStore(
  { name: 'todos' },

  withProps<TodosProps>({ filter: 'ALL' })
);

export const todos$ = store.pipe(selectAllEntities());

export function updateFilter(filter: TodosProps['filter']) {
  store.update(
    write((state) => {
      state.filter = filter;
    })
  );
}

export function updateXom(xom: TodosProps['nested']['address']['xom']) {
  store.update(
    write((state) => {
      state.nested.address.xom = xom;
    })
  );
}



The method...:

export function updateFilter(filter: TodosProps['filter'])

seems to be indexing the property 'filter'.

For the function updateXom (my attempt), is the signature correct? If so, is there a more concise way to do it?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source