diff --git a/packages/svelte/src/store/index-client.js b/packages/svelte/src/store/index-client.js index ae6806ec763f..c49215990595 100644 --- a/packages/svelte/src/store/index-client.js +++ b/packages/svelte/src/store/index-client.js @@ -10,6 +10,16 @@ import { createSubscriber } from '../reactivity/create-subscriber.js'; export { derived, get, readable, readonly, writable } from './shared/index.js'; /** + * Create a store from a function that returns state, and (to make a writable store), an + * optional second function that sets state. + * + * ```ts + * import { toStore } from 'svelte/store'; + * + * let count = $state(0); + * + * const store = toStore(() => count, (v) => (count = v)); + * ``` * @template V * @overload * @param {() => V} get @@ -23,16 +33,6 @@ export { derived, get, readable, readonly, writable } from './shared/index.js'; * @returns {Readable} */ /** - * Create a store from a function that returns state, and (to make a writable store), an - * optional second function that sets state. - * - * ```ts - * import { toStore } from 'svelte/store'; - * - * let count = $state(0); - * - * const store = toStore(() => count, (v) => (count = v)); - * ``` * @template V * @param {() => V} get * @param {(v: V) => void} [set] @@ -71,18 +71,6 @@ export function toStore(get, set) { }; } -/** - * @template V - * @overload - * @param {Writable} store - * @returns {{ current: V }} - */ -/** - * @template V - * @overload - * @param {Readable} store - * @returns {{ readonly current: V }} - */ /** * Convert a store to an object with a reactive `current` property. If `store` * is a readable store, `current` will be a readonly property. @@ -101,6 +89,18 @@ export function toStore(get, set) { * count.current += 1; * get(store); // 2 * ``` + * @template V + * @overload + * @param {Writable} store + * @returns {{ current: V }} + */ +/** + * @template V + * @overload + * @param {Readable} store + * @returns {{ readonly current: V }} + */ +/** * @template V * @param {Writable | Readable} store */ diff --git a/packages/svelte/src/store/index-server.js b/packages/svelte/src/store/index-server.js index 3eddd781f228..660f79eb1e1b 100644 --- a/packages/svelte/src/store/index-server.js +++ b/packages/svelte/src/store/index-server.js @@ -4,6 +4,16 @@ import { get, writable } from './shared/index.js'; export { derived, get, readable, readonly, writable } from './shared/index.js'; /** + * Create a store from a function that returns state, and (to make a writable store), an + * optional second function that sets state. + * + * ```ts + * import { toStore } from 'svelte/store'; + * + * let count = $state(0); + * + * const store = toStore(() => count, (v) => (count = v)); + * ``` * @template V * @overload * @param {() => V} get @@ -17,16 +27,6 @@ export { derived, get, readable, readonly, writable } from './shared/index.js'; * @returns {Readable} */ /** - * Create a store from a function that returns state, and (to make a writable store), an - * optional second function that sets state. - * - * ```ts - * import { toStore } from 'svelte/store'; - * - * let count = $state(0); - * - * const store = toStore(() => count, (v) => (count = v)); - * ``` * @template V * @param {() => V} get * @param {(v: V) => void} [set] @@ -48,18 +48,6 @@ export function toStore(get, set) { }; } -/** - * @template V - * @overload - * @param {Writable} store - * @returns {{ current: V }} - */ -/** - * @template V - * @overload - * @param {Readable} store - * @returns {{ readonly current: V }} - */ /** * Convert a store to an object with a reactive `current` property. If `store` * is a readable store, `current` will be a readonly property. @@ -78,6 +66,18 @@ export function toStore(get, set) { * count.current += 1; * get(store); // 2 * ``` + * @template V + * @overload + * @param {Writable} store + * @returns {{ current: V }} + */ +/** + * @template V + * @overload + * @param {Readable} store + * @returns {{ readonly current: V }} + */ +/** * @template V * @param {Writable | Readable} store */ diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index d422abebbc0f..5d36ff818550 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -2148,10 +2148,42 @@ declare module 'svelte/store' { */ update(this: void, updater: Updater): void; } + + /** + * Create a store from a function that returns state, and (to make a writable store), an + * optional second function that sets state. + * + * ```ts + * import { toStore } from 'svelte/store'; + * + * let count = $state(0); + * + * const store = toStore(() => count, (v) => (count = v)); + * ``` + */ export function toStore(get: () => V, set: (v: V) => void): Writable; export function toStore(get: () => V): Readable; + /** + * Convert a store to an object with a reactive `current` property. If `store` + * is a readable store, `current` will be a readonly property. + * + * ```ts + * import { fromStore, get, writable } from 'svelte/store'; + * + * const store = writable(0); + * + * const count = fromStore(store); + * + * count.current; // 0; + * store.set(1); + * count.current; // 1 + * + * count.current += 1; + * get(store); // 2 + * ``` + */ export function fromStore(store: Writable): { current: V; };