adding types to stores
MODIFY
packages/kit/src/runtime/stores/index.ts
packages/kit/src/runtime/stores/index.ts
import { getContext } from 'svelte';
+import { Readable, Writable } from 'svelte/store';
+import { Page } from '../navigation/types';
+
+// these Svelte types are not exported, so repeating them here.
+type Subscriber<T> = (value: T) => void;
+type Updater<T> = (value: T) => T;
+
+export type PageContext = Page & { error?: Error };
// const ssr = (import.meta as any).env.SSR;
const ssr = typeof window === 'undefined'; // TODO why doesn't previous line work in build?
export const getStores: () => {
- page: any // TODO
- preloading: any // TODO
- session: any // TODO
+ page: Readable<PageContext>
+ preloading: Readable<boolean>
+ session: Writable<any>
} = () => getContext('__svelte__');
export const page = {
- subscribe(fn) {
+ subscribe(fn: Subscriber<PageContext>) {
const store = getStores().page;
return store.subscribe(fn);
}
};
export const preloading = {
- subscribe(fn) {
+ subscribe(fn: Subscriber<boolean>) {
const store = getStores().preloading;
return store.subscribe(fn);
}
@@ -28,7 +36,7 @@ const error = verb => {
};
export const session = {
- subscribe(fn) {
+ subscribe(fn: Subscriber<any>) {
const store = getStores().session;
if (!ssr) {
@@ -38,6 +46,6 @@ export const session = {
return store.subscribe(fn);
},
- set: () => error('set'),
- update: () => error('update')
+ set: (updater: Updater<any>) => { error('set') },
+ update: (updater: Updater<any>) => { error('update') }
};
\ No newline at end of file

adding types to stores