# Computed

Defines a computed property against your model.

# API

Computed<
  Model extends object = {},
  Result = any,
  StoreModel extends object = {}
>
  • Model

    The model against which the computed property is being defined. You need to provide this so that the state that will be provided to your computed property is correctly typed.

  • Result

    The type of the derived data that will be returned by your computed property.

  • StoreModel

    If you expect to using state resolvers within your computed property implementation which use the entire store state then you will need to provide your store's model interface so that the store state is correctly typed.

# Example

import { Computed, computed } from 'easy-peasy';

interface TodosModel {
  todos: string[];
  count: Computed<TodosModel, number>;
}

const todosModel: TodosModel = {
  todos: [],
  count: computed(state => state.todos.length)
}

# Example with state resolvers using store state

import { Computed, computed } from 'easy-peasy';
import { StoreModel } from './index';

interface BasketModel {
  productIds: string[];
  products: Computed<BasketModel, Product[], StoreModel>;
}

const basketModel: BasketModel = {
  productIds: [],
  products: computed(
    [
      state => state.productIds, 
      (state, storeState) => storeState.products.items
    ],
    (productIds, products) => productIds.map(id => products[id])
  )
}