mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-07 12:30:12 +02:00
16 lines
509 B
TypeScript
16 lines
509 B
TypeScript
import type { StoreApi, UseBoundStore } from 'zustand';
|
|
|
|
type WithSelectors<S> = S extends { getState: () => infer T }
|
|
? S & { use: { [K in keyof T]: () => T[K] } }
|
|
: never;
|
|
|
|
export const createSelectors = <S extends UseBoundStore<StoreApi<object>>>(_store: S) => {
|
|
const store = _store as WithSelectors<typeof _store>;
|
|
store.use = {};
|
|
for (const k of Object.keys(store.getState())) {
|
|
(store.use as any)[k] = () => store((s) => s[k as keyof typeof s]);
|
|
}
|
|
|
|
return store;
|
|
};
|