Skip to content

Allow to set custom types from data-attributes #14859

@raythurnvoid

Description

@raythurnvoid

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions