Skip to content
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

Allow to set custom types from data-attributes #14859

Open
raythurnvoid opened this issue Dec 29, 2024 · 2 comments
Open

Allow to set custom types from data-attributes #14859

raythurnvoid opened this issue Dec 29, 2024 · 2 comments

Comments

@raythurnvoid
Copy link

raythurnvoid commented Dec 29, 2024

Describe the problem

I would like to restrict the values allowed in a data-attribute similarly to what is already possible with completely non-standard attributes using the svelteHTML namespace.

Today svelte will accept any value in any data-attribute and there seems to be no way to restrict it, even overriding the svelte's internal .d.ts (like non-ambient.d.ts and elements.d.ts) files seems to not have any effect.

Describe the proposed solution

Allow this:

// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
declare global {
	namespace App {
		// interface Error {}
		// interface Locals {}
		// interface PageData {}
		// interface PageState {}
		// interface Platform {}
	}

	namespace svelteHTML {
		// enhance elements
		interface IntrinsicElements {
			// 'my-custom-element': { someattribute: string; 'on:event': (e: CustomEvent<any>) => void };
		}
		// enhance attributes
		interface HTMLAttributes<T> {
			// If you want to use the beforeinstallprompt event
			// onbeforeinstallprompt?: (event: any) => any;
			// If you want to use myCustomAttribute={..} (note: all lowercase)
			// mycustomattribute?: any; // You can replace any with something more specific if you like

			'data-hello'?: 'hello';
		}
	}
}

export {};

Importance

nice to have

@webJose
Copy link
Contributor

webJose commented Dec 29, 2024

Are you trying to type props for a component? The following works for me:

<script lang="ts">
    import type { HTMLAttributes } from "svelte/elements";

    type Props = HTMLAttributes<HTMLDivElement> & {
        'data-hello'?: 'hello' | undefined;
    }

    let props: Props = $props();
</script>

<div {...props}></div>

Attribute prediction:
image

Possible value list:
image

Error when the value is not in the list:
image

@webJose
Copy link
Contributor

webJose commented Dec 29, 2024

Ah ok, I see. Not for components, just to extend the interface.

I tried this in src/x.d.ts:

declare namespace svelteHTML {
    interface HTMLAttributes<T> {
        'data-yellow'?: 'yellow' | undefined;
        'data-id'?: number;
    }
}

Both attributes start to appear in Intellisense, but Intellisense says they are of type Boolean. Weird.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants