Skip to content

Commit bc78a46

Browse files
committed
fix: type definition #11
1 parent 159af67 commit bc78a46

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

.npmignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ npm-debug.log
44
yarn-error.log
55
yarn.lock
66

7-
*.spec.js
7+
*.test.js
88
.travis.yml
99

1010
node_modules/

README.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,33 @@ Use `createSvelteStore` from the `@storeon/svelte` package instead of using `cre
3434
import { createSvelteStore } from "@storeon/svelte";
3535

3636
let counter = store => {
37-
// Initial state
3837
store.on("@init", () => ({ count: 0 }));
39-
// Reducers returns only changed part of the state
4038
store.on("inc", ({ count }) => ({ count: count + 1 }));
4139
};
4240

4341
export const connect = createSvelteStore([counter]);
4442
```
4543

44+
Using TypeScript you can pass `State` interface to the `createSvelteStore` function
45+
46+
#### `store.ts`
47+
48+
```typescript
49+
import { Store } from 'storeon';
50+
import { createSvelteStore } from "@storeon/svelte";
51+
52+
interface State {
53+
count: number;
54+
}
55+
56+
let counter = (store: Store<State>) => {
57+
store.on("@init", () => ({ count: 0 }));
58+
store.on("inc", ({ count }) => ({ count: count + 1 }));
59+
};
60+
61+
export const connect = createSvelteStore<State>([counter]);
62+
```
63+
4664
Import `connect` function from our `./store` and use it for getting state and dispatching new events
4765

4866
#### `App.svelte`

index.d.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Dispatch } from "storeon";
1+
import { Module, Dispatch } from 'storeon';
22

3-
declare function createSvelteStore<T>(
4-
modules: any[]
5-
): (key: string) => [Dispatch, any];
3+
interface Changes<State, K extends keyof State> {
4+
subscribe: (run: (state: State[K]) => void) => () => void;
5+
}
66

7-
export { createSvelteStore };
7+
export declare function createSvelteStore<State>(modules: Module<State>[]): <K extends keyof State>(key: K) => [Dispatch, Changes<State, K>];

0 commit comments

Comments
 (0)