Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

Struggling with successfully typing ActionResults for form actions in SvelteKit 2

$
0
0

I'm trying to return the created object from a POST request to the page. Everything works, but the Svelte Vite dev server (which is running) seems to not be comprehending the types properly, resulting in me having to go through any to get the data I need.

The action has the correct inferred return type when I hover it, but result in the enhancer callback is stuck with the type: ActionResult<Record<string, unknown> | undefined, Record<string, unknown> | undefined>.

Here's the relevant action in +page.server.ts:

export const actions: Actions = {    newPage: async ({ locals }) => {        if (locals.user == null) return fail(403);        const project = await getUserDefaultProject(locals.dbclient, locals.user.id);        if (project == null) return fail(500);        const newPage = await createNewPage(locals.dbclient, locals.user, project.id);        if (newPage) return newPage;        return fail(400);    },};

(newPage has the type <PageMetadata|null>)

and the enhanced form code in +page.svelte:

<form    action="?/newPage"    method="POST"    use:enhance={({ cancel }) => {        if (!loggedIn.state || offlineMode.state) {            cancel();            createNewLocalPage();            return;        }        return async ({ result }) => {            if (result.status == 200) {                const newPage = (result as any).data as PageMetadata;                pages.state = [...pages.state, newPage];            }        };    }}><button type="submit" title="create a new page" id="create-new-page"><Fa icon={faPlus} color="var(--background)" /><div>new page</div></button></form>

I've been searching for a while for how to properly type this and have come up with nothing, so any help would be appreciated.


Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>