fix types: allow writable/readable empty initialization (#6293)

Fixes #6291
Fixes #6345

Both writable and readable initialized without any arguments are already valid, but TS complains about it. This makes both allowed to be emptily initialized. It's also possible to invoke readable with one argument only.
pull/6347/head
Ignatius Bagus 4 years ago committed by GitHub
parent 3301b09619
commit b295d68ec6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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))

@ -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';

@ -50,7 +50,7 @@ const subscriber_queue = [];
* @param value initial value
* @param {StartStopNotifier}start start and stop notifications for subscriptions
*/
export function readable<T>(value: T, start: StartStopNotifier<T>): Readable<T> {
export function readable<T>(value?: T, start?: StartStopNotifier<T>): Readable<T> {
return {
subscribe: writable(value, start).subscribe
};
@ -61,7 +61,7 @@ export function readable<T>(value: T, start: StartStopNotifier<T>): Readable<T>
* @param {*=}value initial value
* @param {StartStopNotifier=}start start and stop notifications for subscriptions
*/
export function writable<T>(value: T, start: StartStopNotifier<T> = noop): Writable<T> {
export function writable<T>(value?: T, start: StartStopNotifier<T> = noop): Writable<T> {
let stop: Unsubscriber;
const subscribers: Array<SubscribeInvalidateTuple<T>> = [];

@ -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 = {
Loading…
Cancel
Save