Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Expose generic type which extracts type of a store's underlying value. #8291

@joeally

Description

@joeally

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions