Skip to content

accept union type in generic component #429

@macmillen

Description

@macmillen

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:

  export let superForm:
    | SuperForm<Infer<typeof eventSchema>>
    | SuperForm<Infer<typeof admissionSchema>>
    | SuperForm<Infer<typeof eventTemplateSchema>>;

Schemas:

export const eventSchema = z.object({
	name: z.string()
});
export const eventTemplateSchema = z.object({
	name: z.string(),
	id: z.string()
});
export const admissionSchema = z.object({
	name: z.string()
});

Generic input component

  export let superForm: SuperForm<T> | undefined = undefined;

But TypeScript complains if the schema types differ even slightly from each other.

Type 'SuperForm<{ name: string; }> | SuperForm<{ name: string; }> | SuperForm<{ name: string; id: string; }>' is not assignable to type 'SuperForm<{ name: string; }> | undefined'.
  Type 'SuperForm<{ name: string; id: string; }>' is not assignable to type 'SuperForm<{ name: string; }>'.
    The types of 'options.onUpdate' are incompatible between these types.
      Type '((event: { form: SuperValidated<{ name: string; id: string; }, any, { name: string; id: string; }>; formEl: HTMLFormElement; formElement: HTMLFormElement; cancel: () => void; result: Required<...>; }) => unknown) | undefined' is not assignable to type '((event: { form: SuperValidated<{ name: string; }, any, { name: string; }>; formEl: HTMLFormElement; formElement: HTMLFormElement; cancel: () => void; result: Required<...>; }) => unknown) | undefined'.
        Type '(event: { form: SuperValidated<{ name: string; id: string; }, any, { name: string; id: string; }>; formEl: HTMLFormElement; formElement: HTMLFormElement; cancel: () => void; result: Required<...>; }) => unknown' is not assignable to type '(event: { form: SuperValidated<{ name: string; }, any, { name: string; }>; formEl: HTMLFormElement; formElement: HTMLFormElement; cancel: () => void; result: Required<...>; }) => unknown'.

Describe the solution you'd like
It would be helpful to make the generics work with unions somehow

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions