-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
accept union type in generic component #429
Comments
Does it work to put the union in the schemas, or in Infer? SuperForm<Infer<typeof eventSchema | typeof admissionSchema>>
SuperForm<Infer<typeof eventSchema> | Infer<typeof admissionSchema>> |
Unfortunately not. I tried all variations 😕 |
I updated the demo with your suggestion but it still fails
|
I though maybe a way to solve this would be to use generics but it also doesn't work. This time the <script lang="ts" generics="T extends { name: string }">
import type { SuperForm } from 'sveltekit-superforms/client';
import Input from './input.svelte';
export let superForm: SuperForm<T>;
$: ({ form } = superForm);
</script>
<Input {superForm} field="name" />
|
You can use either export const schema = z.union([eventTemplateSchema, eventSchema, admissionSchema]);
export type UnionSchema =
| Infer<typeof eventSchema>
| Infer<typeof admissionSchema>
| Infer<typeof eventTemplateSchema>; Then use the superForm type parameter to get the correct type for the schema: +page.svelte <script lang="ts">
import Union from './union.svelte';
import { superForm as _superForm } from '$lib/index.js';
import type { UnionSchema } from './schemas.js';
export let data;
const superForm = _superForm<UnionSchema>(data.form);
</script>
<Union {superForm} /> union.svelte <script lang="ts">
import type { SuperForm } from '$lib/index.js';
import { type UnionSchema } from './schemas.js';
import Input from './input.svelte';
export let superForm: SuperForm<UnionSchema>;
</script>
<Input {superForm} field="name" /> |
Is your feature request related to a problem? Please describe.
To have generic components it's sometimes neccessary to pass down union types where only an intersection of the type is equal.
Demo:
https://www.sveltelab.dev/y7gmqoire0bl3u4?files=.%2Fsrc%2Flib%2Funion.svelte
Union component:
Schemas:
Generic input component
But TypeScript complains if the schema types differ even slightly from each other.
Describe the solution you'd like
It would be helpful to make the generics work with unions somehow
The text was updated successfully, but these errors were encountered: