-
-
Notifications
You must be signed in to change notification settings - Fork 205
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
Generic resolution issue with bind:this
/svelte:element
#1913
Comments
About the And about the generic, Your generic is inferred as <script lang="ts">
type Href = $$Generic<string | undefined>;
type Ref = Href extends string ? HTMLAnchorElement : HTMLButtonElement;
type $$Props = {
ref?: Ref;
type?: 'button' | 'submit' | 'reset';
disabled?: boolean;
href?: Href;
target?: string | undefined;
};
export let ref: HTMLAnchorElement | HTMLButtonElement = undefined as any;
export let type: 'button' | 'submit' | 'reset' = 'button';
export let disabled = false;
export let href: Href = undefined as any;
export let target: string | undefined = undefined;
let tag: 'button' | 'a';
$: tag = href == null ? 'button' : 'a';
</script>
<svelte:element this={tag} bind:this={ref}
type={tag === 'button' ? type : undefined}
href={disabled ? undefined : href}
target={disabled ? undefined : target}
disabled={tag === 'button' ? disabled : undefined}
data-disabled={tag === 'a' ? disabled : undefined}
{...$$restProps}
on:click on:focus on:blur >
<slot />
</svelte:element> |
I suspected that I could make the If there were an exported props type as I suggested here, that could be manipulated instead (e.g. with utility types like |
Find one that works with the generic. According to TypeScript docs this should allow the generic to be not distributive https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types type Ref = [Href] extends [string] ? HTMLAnchorElement : HTMLButtonElement Don't think the exported props type will help here. Using <script lang="ts">
export let ref: Ref = undefined as any
let refInner: HTMLAnchorElement | HTMLButtonElement
$: ref = refInner as Ref;
</script>
<svelte:element this={tag} bind:this={refInner} Since there is no TypeScript equivalent that can work, I don't think there is much we can do about it. |
I tried to create a // This would be generated
type $$ExportedProps = {
ref?: HTMLAnchorElement | HTMLButtonElement;
href?: string | undefined;
target?: string | undefined;
type?: 'button' | 'submit' | 'reset';
disabled?: boolean;
};
// This would have to added manually
type $$Props = Omit<$$ExportedProps, 'ref' | 'href'> & (
{ href: string; ref?: HTMLAnchorElement } |
{ href: undefined; ref?: HTMLButtonElement }
);
export let ref: HTMLAnchorElement | HTMLButtonElement = undefined as any;
export let type: 'button' | 'submit' | 'reset' = 'button';
export let disabled = false;
export let href: string | undefined = undefined;
export let target: string | undefined = undefined;
let tag: 'button' | 'a';
$: tag = href == null ? 'button' : 'a'; Error:
This is an issue I have noticed before, where the tooling tries to reconcile the exported props with the custom Using The other one (when using the component and binding |
Describe the bug
Ran into issues trying to create a generic component that is either a
button
or ana
.The type is supposed to switch based on whether a
href
property is set, but there are two problems:bind:this
will not resolve a type alias that switches betweenHTMLButtonElement
andHTMLAnchorElement
, giving an assignment error.href
is not defined, only if it is.Reproduction
Component definition:
Error on
bind:this={ref}
:Component usage:
For the components with
href
, the type ofref
is resolved correctly, but for the components without, it isHTMLButtonElement | HTMLAnchorElement | undefined
resulting in no error from a binding to a variable of the wrong type.Expected behaviour
bind:ref
in the component definitionhref
attribute, leading type errors where appropriateSystem Info
Which package is the issue about?
Additional Information, eg. Screenshots
No response
The text was updated successfully, but these errors were encountered: