question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Export store types for typescript usage.

See original GitHub issue

I was trying to create a CRUD class that could take any type in, and create read write update delete, but in doing so, I was missing some of the types inside the store index file.

In my local svelte index.d.ts file I added the exports and everything was working great.

import {
    writable,
    Updater,
    Subscriber,
    Invalidator,
    Unsubscriber
} from "svelte/store";

class StoreModule<T> {
    public subscribe: (
        run: Subscriber<T>,
        invalidate?: Invalidator<T>
    ) => Unsubscriber;
    private update: (updater: Updater<T>) => void;

    constructor(initialValue: T) {
        const { update, subscribe, set } = writable(initialValue);
        this.subscribe = subscribe;
        this.update = update;
    }

    public deleteById(id: string) {}
    public updateById(valToUpdate: T, id: string) {}
    public addNew(newItem: T) {}
}

export type Todo = {
    id: string;
    title: string;
};

export const todos = new StoreModule<Todo[]>([
    { title: "initialTodo", id: Math.random().toString() },
]);

The types not exported are Updater, Subscriber, Invalidator, Unsubscriber.

I might be jumping the gun on this, as I know typescript support is still not merged into the production build of svelte, but in my opinon exporting the types is not destructive in any way.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
parker-codescommented, Feb 23, 2021

You can type-hint when creating a store, which will then pass it along to subscribers/etc.

image

3reactions
YogliBcommented, Nov 6, 2020

Any updates?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Usage With TypeScript - Redux
Since those are types, it's safe to export them directly from your store setup file such as app/store.ts and import them directly into...
Read more >
How I organize my Typescript types. | by Monarch Wadia
You can store interfaces directly on the main file that use them. ... you can explicitly export and import types from .ts files....
Read more >
Store a list of interface/type in typescript - Stack Overflow
Store a list of interface/type in typescript ; export interface SynchGroupSubject ; createNewSynchGroup(id?: string): Observable ; this.synchObs ...
Read more >
Documentation - Everyday Types - TypeScript
Always use string , number , or boolean for types. Arrays. To specify the type of an array like [1, 2, 3] ,...
Read more >
Where do you guys keep your types : r/typescript - Reddit
Anything local I do in a types.ts file in the same folder as where I will use the type. Anything "global" I import/export...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found