-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Types for enhance function callback #7161
Comments
I think it would be nice if the success and invalid data are separated and exported from // src/lib/form.ts
import { type SubmitFunction, enhance } from '$app/forms';
type SuccessData<T> = T extends Record<string, unknown> ? T extends { invalid: boolean } ? never : T : never;
type InvalidData<T> = T extends Record<string, unknown> ? T extends { invalid: boolean} ? T : never : never;
export type TypedSubmitFunction<T> = SubmitFunction<SuccessData<T>, InvalidData<T>> If you add // src/routes/+page.server.ts
import { invalid } from '@sveltejs/kit';
import type { Actions } from './$types';
export const actions: Actions = {
async default() {
if (somecheck()) {
return invalid(400, {
someError: 'There was an error!',
invalid: true
});
}
return {
success: true
};
}
}; <!-- src/routes/+page.svelte -->
<script lang="ts">
import { enhance } from '$app/forms';
import type { TypedSubmitFunction } from '$lib/form';
import type { ActionData } from './$types';
export let form: ActionData;
const handler: TypedSubmitFunction<ActionData> = () => {
return ({ update,result }) => {
if (result.type === 'invalid') {
result.data
}
if (result.type === 'success') {
result.data
}
update();
};
};
</script>
<form method="post" use:enhance={handler}>
<button type="submit">Submit</button>
</form> |
hi @david-plugge thank you! I was working on a similar solution, declaring a new SubmitFunction passing the correct types. So for example if I write an action that
with, in my case, FormResult from
Then the submit function would infer Success and Invalid types, passing type The type of
|
If you mean automagically inferring the correct types by the current path + action name, then probably no, that is way to complicated. |
partially implements #7161 --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
Could someone clarify how can I type enhance function now? I saw commit on March 30 but I am not sure how to get benefits from it and how to type it correctly now.
UPD: This is what I am currently using
|
@ZerdoX-x As of now, the generated <script lang="ts">
import type { SubmitFunction } from './$types';
const submitFunction: SubmitFunction = () => {
return async ({ result }) => {
if (result.type === 'success' && result.data) {
result.data; // typed
}
if (result.type === 'failure' && result.data) {
result.data; // typed
}
};
};
</script> Hope this was part of the zero-effort type safety. |
Yes @hyunbinseo you are right, I think we can close this now :) |
Note that this issue is not completely resolved. Maybe it should stay open in the whenever (non-urgent) milestone? |
Describe the problem
I would like to have the result object parameter of enhance function to be typed.
Example:
+page.server.ts
+page.svelte
So in +page.svelte i have the correct types on form object so that in input value
<input type="text" name="name" placeholder="Name" value={form?.name || ''} />
the editor suggest me that form object has a name property.Insted in enhance callback the result.data is a
Record<string, any> | undefined
and I don't know what kind of data the+page.server.ts
sent.Describe the proposed solution
I'm trying to find a solution but for now the only workaround that is working for me is to not use enhance but instead use a custom listener like the docs says (https://kit.svelte.dev/docs/form-actions#progressive-enhancement-custom-event-listener) but I'm afraid that without the use of the enhance function I lose the ability to make the form work in the absence of javascript.
Alternatives considered
No response
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: