diff --git a/CHANGELOG.md b/CHANGELOG.md index 141d5661bc39..89249634bbd2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Svelte changelog +## Unreleased + +* Fix type signatures of `writable` and `readable`. It's possible to invoke them without arguments ([#6291](https://github.com/sveltejs/svelte/issues/6291), [#6345](https://github.com/sveltejs/svelte/issues/6345)) + ## 3.38.2 * Revert hydration optimisation for the time being ([#6279](https://github.com/sveltejs/svelte/issues/6279)) diff --git a/site/content/docs/03-run-time.md b/site/content/docs/03-run-time.md index 7fbc8850de86..c0bd14531610 100644 --- a/site/content/docs/03-run-time.md +++ b/site/content/docs/03-run-time.md @@ -245,10 +245,10 @@ This makes it possible to wrap almost any other reactive state handling library #### `writable` ```js -store = writable(value: any) +store = writable(value?: any) ``` ```js -store = writable(value: any, (set: (value: any) => void) => () => void) +store = writable(value?: any, start?: (set: (value: any) => void) => () => void) ``` --- @@ -297,14 +297,12 @@ unsubscribe(); // logs 'no more subscribers' #### `readable` ```js -store = readable(value: any, (set: (value: any) => void) => () => void) +store = readable(value?: any, start?: (set: (value: any) => void) => () => void) ``` --- -Creates a store whose value cannot be set from 'outside', the first argument is the store's initial value. - -The second argument to `readable` is the same as the second argument to `writable`, except that it is required with `readable` (since otherwise there would be no way to update the store value). +Creates a store whose value cannot be set from 'outside', the first argument is the store's initial value, and the second argument to `readable` is the same as the second argument to `writable`. ```js import { readable } from 'svelte/store'; diff --git a/src/runtime/store/index.ts b/src/runtime/store/index.ts index cf15db5250b9..213a4ff6fdc6 100644 --- a/src/runtime/store/index.ts +++ b/src/runtime/store/index.ts @@ -50,7 +50,7 @@ const subscriber_queue = []; * @param value initial value * @param {StartStopNotifier}start start and stop notifications for subscriptions */ -export function readable(value: T, start: StartStopNotifier): Readable { +export function readable(value?: T, start?: StartStopNotifier): Readable { return { subscribe: writable(value, start).subscribe }; @@ -61,7 +61,7 @@ export function readable(value: T, start: StartStopNotifier): Readable * @param {*=}value initial value * @param {StartStopNotifier=}start start and stop notifications for subscriptions */ -export function writable(value: T, start: StartStopNotifier = noop): Writable { +export function writable(value?: T, start: StartStopNotifier = noop): Writable { let stop: Unsubscriber; const subscribers: Array> = []; diff --git a/test/store/index.js b/test/store/index.ts similarity index 90% rename from test/store/index.js rename to test/store/index.ts index 2af4a6f35d85..b6fc5940e111 100644 --- a/test/store/index.js +++ b/test/store/index.ts @@ -22,6 +22,19 @@ describe('store', () => { assert.deepEqual(values, [0, 1, 2]); }); + it('creates an undefined writable store', () => { + const store = writable(); + const values = []; + + const unsubscribe = store.subscribe(value => { + values.push(value); + }); + + unsubscribe(); + + assert.deepEqual(values, [undefined]); + }); + it('calls provided subscribe handler', () => { let called = 0; @@ -114,6 +127,32 @@ describe('store', () => { assert.deepEqual(values, [0, 1, 2]); }); + + it('creates an undefined readable store', () => { + const store = readable(); + const values = []; + + const unsubscribe = store.subscribe(value => { + values.push(value); + }); + + unsubscribe(); + + assert.deepEqual(values, [undefined]); + }); + + it('creates a readable store without updater', () => { + const store = readable(100); + const values = []; + + const unsubscribe = store.subscribe(value => { + values.push(value); + }); + + unsubscribe(); + + assert.deepEqual(values, [100]); + }); }); const fake_observable = {