Export store types for typescript usage.
See original GitHub issueI 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:
- Created 3 years ago
- Reactions:8
- Comments:7 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can type-hint when creating a store, which will then pass it along to subscribers/etc.
Any updates?