Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cuppachino/typed-create
zustand-like store with type inference
https://github.com/cuppachino/typed-create
store zustand
Last synced: 22 days ago
JSON representation
zustand-like store with type inference
- Host: GitHub
- URL: https://github.com/cuppachino/typed-create
- Owner: cuppachino
- Created: 2022-11-21T14:14:18.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-10T09:35:58.000Z (about 2 years ago)
- Last Synced: 2024-10-31T23:42:05.997Z (2 months ago)
- Topics: store, zustand
- Language: TypeScript
- Homepage: https://stackblitz.com/edit/scavdk
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# typed-create
```ts
export const create = (base: T) => {
let state = base
const api = {
_setState(newState: U | T): void {
state = { ...state, ...newState } as T & U
},
setState(newState: T | Partial): void {
state = { ...state, ...newState }
},
getState(): T {
return state
}
}return (actions: (set: typeof api.setState, get: typeof api.getState) => U) => {
api._setState({
...api,
...actions(api.setState, api.getState)
})
return {
getState: api.getState,
setState: api.setState
} as _InternalCreateApi
}
}
```
```ts
export interface CreateApi {
getState: () => T;
setState: (newState: T | Partial) => T;
}type _InternalCreateApi = CreateApi<{
[K in keyof T | keyof U]: K extends keyof T
? T[K]
: K extends keyof U
? U[K]
: never;
}> extends CreateApi
? CreateApi
: never;
```In response to [this](https://stackoverflow.com/questions/70758462/how-to-infer-a-functions-parameter-type-from-its-return-type) question on stackoverflow
[Edit on StackBlitz ⚡️](https://stackblitz.com/edit/scavdk)