Skip to content

Is any way to type check style props which is declare in Props? #12036

Answered by brunnerh
yuki0418 asked this question in Q&A
Discussion options

You must be logged in to vote

These kinds of properties are not really properties of the component at all.
Adding them creates a wrapper div around the component and sets the style there.

A workaround would be to perform the same logic yourself, i.e. add a regular property, accept styles there and add the wrapper yourself (unless you already have a suitable element for it).

<script lang="ts">
	import type { Snippet } from 'svelte';

	type Props = {
		children?: Snippet;

		styles?: {
			'--mb'?: string;
			'--direction'?: 'row' | 'column';
		};
	};

	const { children, styles }: Props = $props();

	const style = $derived(
		Object.entries(styles ?? {})
			.map(([key, value]) => `${key}: ${value}`)
			.join(';')
	);
</s…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@yuki0418
Comment options

Answer selected by yuki0418
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants