Skip to content

Add additional function overload for derived#6172

Merged
lukeed merged 1 commit intosveltejs:masterfrom
fielding:fix/derived-store-type-overloads
Apr 9, 2021
Merged

Add additional function overload for derived#6172
lukeed merged 1 commit intosveltejs:masterfrom
fielding:fix/derived-store-type-overloads

Conversation

@fielding
Copy link
Copy Markdown
Contributor

@fielding fielding commented Apr 7, 2021

Summary

Add an additional overload for derived to allow explicitly setting an initial value for non-async derived stores.

Details

After a long overdue svelte version update, I started getting some type errors on any non-async derived store that I had explicitly set an initial value. For example,

export const isSlow = derived(delta, ($delta) => $delta > 300, false);

Perhaps the correct way to handle the initial value is through the initial values of the stores that it is derived from, but I guess it feels safer to me to also explicitly set it on the derived store.

Assuming that this usage is acceptable then the additional overload keeps typescript happy. 😄

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with npm test and lint the project with npm run lint

Copy link
Copy Markdown
Member

@lukeed lukeed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, and much needed. Ran into it a number of times personally & I know it was a definition that Svelte used to include at some earlier point in the 3.x cycle.

Thanks!

@dummdidumm
Copy link
Copy Markdown
Member

dummdidumm commented Apr 8, 2021

Is there a reason why this new signature isn't just the only signature? Are there reasons why this was split out?
(Agree with adding that signature)

@lukeed
Copy link
Copy Markdown
Member

lukeed commented Apr 9, 2021

@dummdidumm This new signature has no $set usage. There's nothing wrong with having an overload pattern, and in derived's case, it is an overloaded function because there are different avenues for using/holding it.

@dummdidumm
Copy link
Copy Markdown
Member

Silly me, didn't read that other definition closely enough. All good then!

@lukeed lukeed merged commit 3b4d53e into sveltejs:master Apr 9, 2021
@lukeed
Copy link
Copy Markdown
Member

lukeed commented Apr 9, 2021

Thanks @fielding !

@Conduitry
Copy link
Copy Markdown
Member

These types should be published in 3.38.0. Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants