sveltejs / sveltejs/kit

Missing property in type from ActionData

Open
#7,004 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

blocked by upstream types / typescript
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

This is to reopen issue #6823

First of all, thanks everyone for helping with this issue, but even after upgrading to the newest sveltekit version, the bug still persists. I have updated the example repo for clearer demonstration of the bug: https://github.com/harrylaulau/sveltekittest

First issue is that when one property exist in one invalid() but not another, the type of return object with that property does not exist.

Second issue is when a property is of object type, it seems to override all other type

+page.server.ts

import { invalid, redirect } from '@sveltejs/kit';
import type { Actions } from './$types';
export const actions: Actions = {
	default: async ({ request, url }) => {
		if (url.searchParams.get('v') === '0') return invalid(400, { common: '1' });
		if (url.searchParams.get('v') === '1') return invalid(400, { common: '2', something: '' });

		if (url.searchParams.get('v') === '2') return invalid(400, { common2: 1 });
		if (url.searchParams.get('v') === '3') return invalid(400, { common2: '1' });
		if (url.searchParams.get('v') === '4') return invalid(400, { common2: {} });
	}
};

CleanShot 2022-09-23 at 23 33 43

+page.svelte
CleanShot 2022-09-23 at 23 35 27

  1. return type with something does not exist
  2. return type with common2 having the string type or a number type does not exist
Reproduction

https://github.com/harrylaulau/sveltekittest

Logs

No response

System Info
System:
    OS: macOS 12.6
    CPU: (10) arm64 Apple M1 Max
    Memory: 7.60 GB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    Yarn: 1.22.19 - ~/Library/pnpm/yarn
    npm: 8.12.1 - ~/.nvm/versions/node/v16.15.0/bin/npm
    Watchman: 2022.09.12.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 105.0.5195.125
    Safari: 16.0
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.80 
    @sveltejs/kit: next => 1.0.0-next.502 
    svelte: ^3.44.0 => 3.50.1 
    vite: ^3.1.0 => 3.1.3
Severity

serious, but I can work around it

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction and compare the generated types used by +page.server.ts and +page.svelte when invalid() returns different properties and value types. Trace how ActionData is produced and verify that the completed types retain something, common2, and their string, number, and object variants.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend-api-design, developer-experience
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.