Skip to content

Commit 8f82254

Browse files
committed
🏷️ Better typings
1 parent c7ad86d commit 8f82254

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

README.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,18 @@ Using TypeScript you can pass `State` and `Events` interface to the `createStore
5353
#### `store.ts`
5454

5555
```typescript
56-
import { Store, StoreonEvents, createStoreon } from 'storeon'
56+
import { StoreonModule, createStoreon } from 'storeon'
5757

58-
// State structure
5958
interface State {
6059
count: number
6160
}
6261

63-
// Events declaration: map of event names to type of event data
64-
interface Events extends StoreonEvents<State> {
65-
// `inc` event which do not goes with any data
62+
interface Events {
6663
'inc': undefined
67-
// `set` event which goes with number as data
6864
'set': number
6965
}
7066

71-
let counter = (store: Store<State>) => {
67+
let counter = (store: StoreonModule<State, Events>) => {
7268
store.on('@init', () => ({ count: 0 }))
7369
store.on('inc', ({ count }) => ({ count: count + 1 }))
7470
store.on('set', (_, event) => ({ count: event}))

index.d.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import { Store, StoreonEvents, Dispatch } from 'storeon';
1+
import { StoreonStore, StoreonDispatch, createStoreon } from 'storeon';
2+
3+
type Subscriber<T> = (value: T) => void;
4+
5+
type Unsubscriber = () => void;
26

37
type Subscribable<State> = {
4-
[K in keyof State]: { subscribe: (run: (state: State[K]) => void) => () => void; };
8+
[K in keyof State]: {
9+
subscribe: (run: Subscriber<State[K]>) => Unsubscriber;
10+
};
511
}
612

7-
export declare function setStore(store: Store): void
8-
export declare function getStore<State, EventsMap extends StoreonEvents<State> = any>(...keys: (keyof State)[]): Subscribable<State> & {
9-
dispatch: Dispatch<EventsMap>
13+
export declare function setStore(store: StoreonStore): void
14+
export declare function getStore<State, Events = any>(...keys: (keyof State)[]): Subscribable<State> & {
15+
dispatch: StoreonDispatch<Events & createStoreon.DispatchableEvents<State>>
1016
}

0 commit comments

Comments
 (0)