Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adjust order of derived function definition overloads #11426

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-bananas-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: adjust order of `derived` function definition overloads
4 changes: 2 additions & 2 deletions packages/svelte/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function writable(value, start = noop) {
* @template T
* @overload
* @param {S} stores
* @param {(values: import('./private.js').StoresValues<S>) => T} fn
* @param {(values: import('./private.js').StoresValues<S>, set: (value: T) => void, update: (fn: import('./public.js').Updater<T>) => void) => import('./public.js').Unsubscriber | void} fn
* @param {T} [initial_value]
* @returns {import('./public.js').Readable<T>}
*/
Expand All @@ -124,7 +124,7 @@ export function writable(value, start = noop) {
* @template T
* @overload
* @param {S} stores
* @param {(values: import('./private.js').StoresValues<S>, set: (value: T) => void, update: (fn: import('./public.js').Updater<T>) => void) => import('./public.js').Unsubscriber | void} fn
* @param {(values: import('./private.js').StoresValues<S>) => T} fn
* @param {T} [initial_value]
* @returns {import('./public.js').Readable<T>}
*/
Expand Down
6 changes: 0 additions & 6 deletions packages/svelte/tests/store/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ describe('derived', () => {
const number = writable(1);
const evens = derived(
number,
// @ts-expect-error TODO feels like inference should work here
(n, set) => {
if (n % 2 === 0) set(n);
},
Expand Down Expand Up @@ -303,10 +302,8 @@ describe('derived', () => {
const number = writable(1);
const evensAndSquaresOf4 = derived(
number,
// @ts-expect-error TODO feels like inference should work here
(n, set, update) => {
if (n % 2 === 0) set(n);
// @ts-expect-error TODO feels like inference should work here
if (n % 4 === 0) update((n) => n * n);
},
0
Expand Down Expand Up @@ -442,7 +439,6 @@ describe('derived', () => {
const values: number[] = [];
const cleaned_up: number[] = [];

// @ts-expect-error TODO feels like inference should work here
const d = derived(num, ($num, set) => {
set($num * 2);

Expand Down Expand Up @@ -516,7 +512,6 @@ describe('derived', () => {
const a = writable(true);
let b_started = false;

// @ts-expect-error TODO feels like inference should work here
const b = derived(a, (_, __) => {
b_started = true;
return () => {
Expand All @@ -525,7 +520,6 @@ describe('derived', () => {
};
});

// @ts-expect-error TODO feels like inference should work here
const c = derived(a, ($a, set) => {
if ($a) return b.subscribe(set);
});
Expand Down
14 changes: 14 additions & 0 deletions packages/svelte/tests/types/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@ derived([a], ([aVal]) => {
aVal === '';
return aVal === true;
});

derived(
a,
(value, set) => {
set('works');
// @ts-expect-error
set(true);

value === true;
// @ts-expect-error
value === '';
},
''
);
4 changes: 2 additions & 2 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2157,14 +2157,14 @@ declare module 'svelte/store' {
*
* https://svelte.dev/docs/svelte-store#derived
* */
export function derived<S extends Stores, T>(stores: S, fn: (values: StoresValues<S>) => T, initial_value?: T | undefined): Readable<T>;
export function derived<S extends Stores, T>(stores: S, fn: (values: StoresValues<S>, set: (value: T) => void, update: (fn: Updater<T>) => void) => Unsubscriber | void, initial_value?: T | undefined): Readable<T>;
/**
* Derived value store by synchronizing one or more readable stores and
* applying an aggregation function over its input values.
*
* https://svelte.dev/docs/svelte-store#derived
* */
export function derived<S extends Stores, T>(stores: S, fn: (values: StoresValues<S>, set: (value: T) => void, update: (fn: Updater<T>) => void) => Unsubscriber | void, initial_value?: T | undefined): Readable<T>;
export function derived<S extends Stores, T>(stores: S, fn: (values: StoresValues<S>) => T, initial_value?: T | undefined): Readable<T>;
/**
* Takes a store and returns a new one derived from the old one that is readable.
*
Expand Down