Skip to content

Typing of PageData doesn't work with Promise returns from load functions #11065

@bertmad3400

Description

@bertmad3400

Describe the bug

Only tested using Typescript

As a convenience, SvelteKit automatically awaits top-level promises in load functions, giving the return of the load functions and the actual data available to the pages a slightly different type-interface. Svelte handles this by using different types for load functions and page data.

Unfortunately, this produces type errors when attempting to strongly type the PageData interface. If the load function returns a promise with a number, the page will only receive the number, and typing the PageData interface with either a promise with a number or just a number will give of an error, either in the load function or in the page.

Reproduction

https://github.com/bertmad3400/sveltekit-issue

Logs

Errors:

If typing without promise:
"Type 'Promise<number>' is not assignable to type 'number'"

If typing with promise:
"Operator '+' cannot be applied to types '2' and 'Promise<number>'"

System Info

System:
    OS: Linux 6.5 Arch Linux
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700H
    Memory: 11.00 GB / 15.35 GB
    Container: Yes
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.8.1 - /usr/bin/node
    npm: 10.2.1 - /usr/bin/npm
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.1.1 
    @sveltejs/kit: ^1.27.4 => 1.27.6 
    svelte: ^4.0.5 => 4.2.4 
    vite: ^4.4.2 => 4.5.0

Severity

serious, but I can work around it

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions