import type { StoreApi, UseBoundStore } from 'zustand' type WithSelectors = S extends { getState: () => infer T } ? S & { use: { [K in keyof T]: () => T[K] } } : never export function createSelectors>> ( baseStore: S ) { const store = baseStore as WithSelectors const selectors = {} as WithSelectors['use'] const mutableSelectors = selectors as unknown as Record unknown> const stateKeys = Object.keys(store.getState()) as Array> stateKeys.forEach((key) => { mutableSelectors[String(key)] = () => store((state) => state[key as keyof typeof state]) }) store.use = selectors return store }