Generated type `ActionData`, is missing optional properties when `fail()` returns different `data`-shapes
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
This is only an issue with the generated ActionData-type, not the hydration of the form-property.
When an action is calling fail() multiple places, with different shapes of data, it seems that the generated type will be narrowed down to the shape, that only shares the common properties of all the possible shapes.
This results to Property 'x' does not exist on type 'y'-errors, when trying to access those properties in the +page.svelte component
This seems like a bug. Though not of high severance, as it can be circumvented, by just adding the properties to data-objects where needed.
Related:
- #8865
- #8877
Reproduction
https://github.com/jeppech/incorrect-actiondata-multiple-fail-fn
- src/routes/+page.server.ts (named actions)
- src/routes/default/+page.server.ts (default action)
This is a plain create-svelte@latest-repo, that demonstrates how the generated ActionData-type is missing optional properties.
When the data-parameter of fail(), partially matches other shapes, in any other call to the fail-function, the generated type will be narrowed down to the "common" properties of all shapes.
Logs
No response
System Info
❯ pnpx envinfo --system --binaries --browsers --npmPackages "{svelte,@sveltejs/*,vite}"
.../../.node/pnpm/store/v3/tmp/dlx-55577 | +1 +
.../../.node/pnpm/store/v3/tmp/dlx-55577 | Progress: resolved 1, reused 1, downloaded 0, added 1, done
System:
OS: macOS 13.3.1
CPU: (8) arm64 Apple M1 Pro
Memory: 237.09 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.12.1 - ~/Library/pnpm/node
npm: 8.18.0 - /opt/homebrew/bin/npm
Browsers:
Brave Browser: 112.1.50.119
Firefox Developer Edition: 113.0
Safari: 16.4
npmPackages:
@sveltejs/adapter-auto: ^2.0.0 => 2.0.0
@sveltejs/kit: ^1.15.7 => 1.15.7
svelte: ^3.58.0 => 3.58.0
vite: ^4.2.2 => 4.2.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 with the reproduction files src/routes/+page.server.ts and src/routes/default/+page.server.ts, then inspect how their multiple fail() data shapes produce the generated ActionData type used by +page.svelte. Done means optional properties from the possible data shapes remain accessible without type errors in the page component.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- full-stack
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100