svelte
svelte copied to clipboard
Expose generic type which extracts type of a store's underlying value.
Describe the problem
In the same way that typescript offers Awaited
to extract the type from a promise it would be nice if we could provide an easy way to get the type of the underlying value of a store.
The reason I came up with this is because I wanted to extract to type from the page
store in sveltekit like below:
import type { page } from '$app/stores';
type PageType = StoreValue<typeof page>;
function acceptsPageStoreValue(value: PageType): ArbitraryType {
...
}
Of course I could manually copy the actual type which is:
Page<Record<string, string>, string | null>
But this is much less readable.
Describe the proposed solution
Add the below type to runtime/store/index.ts
type StoreValue<T extends Readable<unknown>> = Parameters<Parameters<T['subscribe']>[0]>[0];
I'm not sure if StoreValue
is a good name here. But if we decide on a good name I'd be happy to put in a PR. Especially as it's such a simple change.
Alternatives considered
The alternative is not simply to not bother with this. Devs can trivially implement this type themselves if they want.
Importance
nice to have