Type Inference Issue in Svelte 5
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 20.8k
- Forks
- 2.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 156
Description
Describe the bug
Description: In Svelte 5, I've encountered an issue with type inference when using the load function in +page.server.ts.
When I pass a direct object as an argument to the load function, the type inference fails on the corresponding +page.svelte file. However, if I encapsulate the object inside another object (e.g., { obj }), the type inference works correctly.
Steps to Reproduce:
Create a +page.server.ts file.
Implement a load function that returns a direct object with the type or null, e.g., return { key: value }.]
Attempt to use the returned data in the +page.svelte file and observe the lack of type inference.
Modify the load function to return an encapsulated object, e.g., return { obj: { key: value } }.
Observe that the type inference now works correctly.
Expected Behavior: The type inference should work correctly even when passing a direct object in the load function.
Actual Behavior: Type inference fails when passing a direct object, requiring encapsulation to work properly.
Reproduction
Direct object:
With encapsulation:
Logs
No response
System Info
System:
OS: Windows 10 10.0.19045
CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
Memory: 21.66 GB / 63.91 GB
Binaries:
Node: 20.12.1 - C:\Program Files\nodejs\node.EXE
npm: 10.8.2 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Chromium (127.0.2651.74)
Internet Explorer: 11.0.19041.4355
npmPackages:
@sveltejs/adapter-auto: ^3.0.0 => 3.2.4
@sveltejs/kit: ^2.0.0 => 2.5.24
@sveltejs/vite-plugin-svelte: ^4.0.0-next.6 => 4.0.0-next.6
svelte: ^5.0.0-next.238 => 5.0.0-next.238
vite: ^5.0.3 => 5.4.2
Severity
annoyance
Additional Information
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the behavior in +page.server.ts and consuming the result in +page.svelte, comparing a direct returned object with the encapsulated form shown in the issue. Trace how the load result is inferred between these entry points and verify that direct objects provide the same type inference without requiring encapsulation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- full-stack, web-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100