-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
"No overload matches this call" typescript error when passing an array of stores to derived() #6178
Comments
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
The problem comes down to const stores = [writable(0), writable(0)];
const test: Stores = stores The problem is that |
Hey, thanks for your answer. Now I see the point of using a variadic tuple instead of a typed array – which had me scratching my head. I toyed with the idea of exporting the Stores interface, which would allow casting: const stores = [writable(0), writable(0)];
const test = derived(stores as Stores, () => 'test'); … which is already starting to feel dirty. But then other errors kept piling up:
… which I guess could be fixed this way (🤯): const stores = [writable(0) as Readable<number>, writable(0)]; … which, yeah… let us stop there. And so, the more it goes, the more I'm thinking that typescript's inference is fundamentally wrong. Any thoughts on this? |
With this fix, doing const stores= [writable(0), writable(1)] derived(stores, ([a,b,c]) => {}); works without type errors. The propblem with the previous type signature was that it's too strict for TypeScript's suboptimal inference of the value stores in that example, which is Array<Readable<any>>, which does not convey the info "at least one element", which the old Stores signature required. Runtime-wise, it's no problem passing an empty array to derived. Fixes #6178
With this fix, doing ``` const stores= [writable(0), writable(1)] derived(stores, ([a,b,c]) => {}); ``` works without type errors. The propblem with the previous type signature was that it's too strict for TypeScript's suboptimal inference of the value `stores` in that example, which is `Array<Readable<any>>`, which does not convey the info "at least one element", which the old Stores signature required. Runtime-wise, it's no problem passing an empty array to derived. The new signature is only appended to, not replaced, because the old signature is able to correctly infer the values of each array entry: For `derived([writable(0), writable('foo')], ([a, b]) => {});` the parameters `a` and `b` are correctly inferred as `number` and `string`. If the type would be changed to `type Stores = Readable<any> | Array<Readable<any>>` that would be no longer the case. Fixes #6178
The types have been loosened in 3.39.0. |
Not sure if a typescript or a svelte bug…
I get a "No overload matches this call" typescript error on that kind of code in VSCode:
Weirdly, this works
Hovering above the
stores
constant shows that it's inferred to be an array of Writables, i.e.(Writable<string> | Writable<number>)[]
, whereas in thetest2
example, the array of stores is inferred to be a[Writable<string>, Writable<number>]
tuple.The
Stores
type declaration in types/runtime/store/index.d.ts (declare type Stores = Readable<any> | [Readable<any>, ...Array<Readable<any>>];
) is compatible with the second inference, but not with the first one. Adding| Readable<any>[]
to the possible types ofStores
seems to fix the issue.Here's the full typescript error:
Tested on VSCode 1.55 on macOS 11, svelte 3.37, typescript 4.2.4, rollup.
--
Edit: after some more digging, I think it's probably related to microsoft/TypeScript#39244
The text was updated successfully, but these errors were encountered: