Svelte Custom Stores TypeScript Declaration - by David Dal Busco - Better Programming
Svelte Custom Stores TypeScript Declaration - by David Dal Busco - Better Programming
This is your last free member-only story this month. Upgrade for unlimited access.
Member-only story
Svelte offers some nice easy-to-use store concepts. One of those is “custom stores”
which makes handy the obfuscation of the reactive stored data and its access
through a set of custom-defined functions.
https://betterprogramming.pub/svelte-custom-stores-typescript-declaration-db34c0f7e7b8 1/14
5/22/23, 10:37 PM Svelte Custom Stores TypeScript Declaration | by David Dal Busco | Better Programming
For example, a counter that counts apples and bananas, I can create a custom store
that does not expose directly my data. Within a function, I can “hide” the store and
expose only the functions I want to use in my apps instead of exposing publicly set
and update . This to avoid misbehavior and unexpected changes, what can be really
useful as the code base grows.
return {
subscribe,
incBanana: () =>
update(({ bananas, ...rest }) => ({
...rest,
bananas: bananas + 1
})),
decBanana: () =>
update(({ bananas, ...rest }) => ({
...rest,
bananas: bananas - 1
})),
reset: () => set(initialCounter)
Open in app
};
};
Above code, snippet follows Svelte’s documentation example with the addition of a
type — Counter — for the object saved in my store and few custom functions —
incBanana , decBanana and reset .
https://betterprogramming.pub/svelte-custom-stores-typescript-declaration-db34c0f7e7b8 2/14
5/22/23, 10:37 PM Svelte Custom Stores TypeScript Declaration | by David Dal Busco | Better Programming
Note that I just did not implement the “apples” related functions to keep the snippet small.
In a real use case I might have even extracted the update() part to a function to avoid
double code but, I also skipped this part because it would have not much value in this blog
post context.
However, even if npm run dev|build|check are all fine, Webstorm is not being able to
derive the store values when used in a TypeScript script block.
https://betterprogramming.pub/svelte-custom-stores-typescript-declaration-db34c0f7e7b8 3/14
5/22/23, 10:37 PM Svelte Custom Stores TypeScript Declaration | by David Dal Busco | Better Programming
I do not know exactly if it is yet another issue of the Jetbrains Svelte plugin or if the
store is derived as any type by the bundler (which would ignore the strict option).
Nevertheless, I rather like to fix the issue by improving the type of declaration of
the store.
Because my custom store exposes my homemade functions but, also the root
subscribe feature, its declaration has to provide support for it as well.
Therefore the easiest way in my opinion is to inherit the existing Readable type of
Svelte.
By setting the generic types to my custom data type — Counter — I can instruct
which type is handled in the store and by declaring my functions, I can improve the
https://betterprogramming.pub/svelte-custom-stores-typescript-declaration-db34c0f7e7b8 4/14
5/22/23, 10:37 PM Svelte Custom Stores TypeScript Declaration | by David Dal Busco | Better Programming
typings.
I can use this new interface to set the return type of the initialization function —
initStore — and that’s already it.
return {
subscribe,
incBanana: () =>
update(({ bananas, ...rest }) => ({
...rest,
bananas: bananas + 1
})),
decBanana: () =>
update(({ bananas, ...rest }) => ({
...rest,
bananas: bananas - 1
})),
reset: () => set(initialCounter)
};
};
When I open my component, I notice that Webstorm now understands correctly the
type of my data.
https://betterprogramming.pub/svelte-custom-stores-typescript-declaration-db34c0f7e7b8 5/14
5/22/23, 10:37 PM Svelte Custom Stores TypeScript Declaration | by David Dal Busco | Better Programming
Follow
52 2
https://betterprogramming.pub/svelte-custom-stores-typescript-declaration-db34c0f7e7b8 7/14
5/22/23, 10:37 PM Svelte Custom Stores TypeScript Declaration | by David Dal Busco | Better Programming
How To Build Your Own Custom ChatGPT With Custom Knowledge Base
Feed your ChatGPT bot with custom data sources
4K 93
When discussing team organization, I am often asked: “Why don’t you have the tech lead
manage the team?” My response is to hiss like a…
1.4K 20
How to Create a Modal For Your Angular App Without Using a Library
Modals without dependencies
178 2
https://betterprogramming.pub/svelte-custom-stores-typescript-declaration-db34c0f7e7b8 9/14
5/22/23, 10:37 PM Svelte Custom Stores TypeScript Declaration | by David Dal Busco | Better Programming
61 2
https://betterprogramming.pub/svelte-custom-stores-typescript-declaration-db34c0f7e7b8 10/14
5/22/23, 10:37 PM Svelte Custom Stores TypeScript Declaration | by David Dal Busco | Better Programming
1 1
Lists
Leadership
30 stories · 16 saves
https://betterprogramming.pub/svelte-custom-stores-typescript-declaration-db34c0f7e7b8 11/14
5/22/23, 10:37 PM Svelte Custom Stores TypeScript Declaration | by David Dal Busco | Better Programming
David Li
17
How To Build Your Own Custom ChatGPT With Custom Knowledge Base
Feed your ChatGPT bot with custom data sources
https://betterprogramming.pub/svelte-custom-stores-typescript-declaration-db34c0f7e7b8 12/14
5/22/23, 10:37 PM Svelte Custom Stores TypeScript Declaration | by David Dal Busco | Better Programming
4K 93
1.6K 13
https://betterprogramming.pub/svelte-custom-stores-typescript-declaration-db34c0f7e7b8 13/14
5/22/23, 10:37 PM Svelte Custom Stores TypeScript Declaration | by David Dal Busco | Better Programming
263 7
https://betterprogramming.pub/svelte-custom-stores-typescript-declaration-db34c0f7e7b8 14/14