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

Svelte 5: Type issues with derived store / page store #11415

Closed
rChaoz opened this issue May 1, 2024 · 1 comment · Fixed by #11426
Closed

Svelte 5: Type issues with derived store / page store #11415

rChaoz opened this issue May 1, 2024 · 1 comment · Fixed by #11426

Comments

@rChaoz
Copy link
Contributor

rChaoz commented May 1, 2024

Describe the bug

Just upgraded to Svelte 5 and I'm getting type errors for some stores-only code (.ts file).

Reproduction

import { derived, readable, writable, type Updater, type Writable } from "svelte/store"
import { page } from "$app/stores"
import { browser } from "$app/environment"
import deepEquals from "fast-deep-equal"

let oldState: App.PageState = {}

export const pageState = derived<typeof page, App.PageState>(
    page,
    (_, set) => {
        if (!browser) return
        setTimeout(() => {
            const newState = history.state["sveltekit:states"] ?? {}
            if (!deepEquals(oldState, newState)) {
                oldState = newState
                set(newState)
            }
        })
    },
    {},
)

Workaround

Manually type the arrow function - replace _, set with:

_: typeof page extends Readable<infer U> ? U : never, set: (value: App.PageState) => void)

Logs

$ tsc --noEmit true
src/lib/stores.ts:13:6 - error TS7006: Parameter '_' implicitly has an 'any' type.

13     (_, set) => {
        ~

src/lib/stores.ts:13:9 - error TS7006: Parameter 'set' implicitly has an 'any' type.

13     (_, set) => {

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700KF
    Memory: 13.10 GB / 31.86 GB
  Binaries:
    Node: 20.8.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 4.1.1 - C:\Program Files\nodejs\yarn.CMD
    npm: 10.2.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0)
    Internet Explorer: 11.0.19041.1566

Severity

annoyance

@CaptainCodeman
Copy link

CaptainCodeman commented May 1, 2024

I get it when using svelte-check: sveltejs/language-tools#2359

It could be a regression: #8699

@rChaoz rChaoz changed the title Svelte 5: issues with derived store / page store Svelte 5: Type issues with derived store / page store May 1, 2024
dummdidumm added a commit that referenced this issue May 2, 2024
Turns out the order is crucial for not getting a type error
fixes #11415
@dummdidumm dummdidumm added this to the 5.0 milestone May 2, 2024
dummdidumm added a commit that referenced this issue May 3, 2024
Turns out the order is crucial for not getting a type error
fixes #11415
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants