Add getter api to @xstate/store #5184
Add getter api to @xstate/store #5184expelledboy wants to merge 1 commit intostatelyai:davidkpiano/store-v3from
Conversation
|
|
Obv you get a circular reference between the getters type and the second arg of its definition. Depending on how many second pass getters you have there are two approaches Define the type of const store = createStore({
context: { price: 10, quantity: 2 },
getters: {
subtotal: (ctx: { price: number; quantity: number }) =>
ctx.price * ctx.quantity,
tax: (_, getters) => getters.subtotal * 0.1,
total: (_, getters) => getters.subtotal + getters.tax
},
on: {
updatePrice: (ctx, ev: { value: number }) => ({ price: ev.value })
}
});Or just define the type of const store = createStore({
context: { price: 10, quantity: 2 },
getters: {
subtotal: (ctx) => ctx.price * ctx.quantity,
tax: (_, getters: { subtotal: number }): number =>
getters.subtotal * 0.1,
total: (_, getters: { subtotal: number; tax: number }): number =>
getters.subtotal + getters.tax
},
on: {
updatePrice: (ctx, ev: { value: number }) => ({ price: ev.value })
}
});Probably would want to include this in the docs. Unless anyone has a better idea on the API here. |
|
@davidkpiano Please can you assist me merging this? I dont want there to be massive merge conflicts in 2-4 months with the v3 branch. Mostly I need assistance getting the types working in the surrounding utils. And / or if you agree to the API itself. |
|
Guessing this was closed because of the merge. |
Merge getters implementation from #5183 onto #5175