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

Reorder the type parameters of derived #9084

Closed
JeanJPNM opened this issue Aug 8, 2023 · 4 comments
Closed

Reorder the type parameters of derived #9084

JeanJPNM opened this issue Aug 8, 2023 · 4 comments

Comments

@JeanJPNM
Copy link

JeanJPNM commented Aug 8, 2023

Describe the problem

When declaring a derived store that has null as the initial value and has two or more stores as dependencies, I can't specify
the type that the derived store contains without first having so specify the type of the dependency array, which is somewhat frustrating.

// ideal situation (does not currently work)
// the type of the the store's value is `DerivedData | null`
// and the type of the dependecies is inferred
const myData = derived<DerivedData | null>([a, b, c], ([$a, $b, $c], set) => {
  if(!$a || !$b || !$c) {
    set(null);
    return;
  }
  const { data, dispose } = generateData($a, $b, $c);
  set(data);
  return dispose;
});

// reality

// one way to work around the problem
// manually typing the `set` parameter will yield the same result, but it's more verbose
const myData = derived([a, b, c], ([$a, $b, $c], set: (value: DerivedData | null) => void) => {
  if(!$a || !$b || !$c) {
    set(null);
    return;
  }
  const { data, dispose } = generateData($a, $b, $c);
  set(data);
  return dispose;
});

// another workaround
// like before, it yields the same result, but it's also more verbose than it should be
const deps = [a, b, c] as const
const myData = derived<typeof deps, DerivedData | null>(deps, ([$a, $b, $c], set) => {
  if(!$a || !$b || !$c) {
    set(null);
    return;
  }
  const { data, dispose } = generateData($a, $b, $c);
  set(data);
  return dispose;
});

Describe the proposed solution

Swapping the order of the generic parameters of the derived function would solve the problem.

/**
* @template {import('./private.js').Stores} S
* @template T
* @param {S} stores
* @param {Function} fn
* @param {T} [initial_value]
* @returns {import('./public.js').Readable<T>}
*/
export function derived(stores, fn, initial_value) {

Alternatives considered

The alternatives were listed above.

Importance

would make my life easier

@gtm-nayan
Copy link
Contributor

This would be a breaking change, but even if we swapped the order I don't think it would help because you can't just omit the second parameter to let it be inferred. Probably blocked by microsoft/TypeScript#26349

@Rich-Harris
Copy link
Member

Since $state(...) in Svelte 5 supersedes stores, we're very unlikely to make any changes to store APIs, so I'll close this

@Rich-Harris Rich-Harris closed this as not planned Won't fix, can't repro, duplicate, stale Apr 3, 2024
@harrisi
Copy link

harrisi commented Apr 3, 2024

Sorry, does this mean stores are effectively deprecated? If they're not being updated and are replaced by $state, it sure seems that way.

Or are they just considered stable and not being removed, but not being updated? I don't know why they'd need to stay, but the docs say they're staying so I just wanted to clear that up.

@dummdidumm
Copy link
Member

Stable, not deprecated, and we're waiting on how things turn out (how much store usage declines etc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants