I’ve been trying to get the <svelte:component /> working with Typescript with no success. This is what I have right now.
Presentation.svelte
<script lang="ts">
export let slides;
</script>
{#each slides as slide}
<svelte:component this={slide.component} {...slide} />
{/each}
+page.svelte
<script lang="ts">
import TitleSlide from '$lib/components/Slides/TitleSlide.svelte';
import Presentation from '$lib/components/Presentation.svelte';
import HeaderSlide from '$lib/components/Slides/HeaderSlide.svelte';
const slides = [
{
component: TitleSlide,
//
},
{
component: HeaderSlide,
}
]
</script>
<Presentation {slides} />
TitleSlide.svelte
<script lang="ts">
import BaseSlide from './BaseSlide.svelte';
export let title: string;
export let subtitle: string;
</script>
<BaseSlide>
<div class="flex flex-col items-center">
<h1 class="text-8xl font-bold">{title}</h1>
<h2 class="text-4xl">{subtitle}</h2>
</div>
</BaseSlide>
HeaderSlide.svelte
<script lang="ts">
import BaseSlide from './BaseSlide.svelte';
export let header: string;
</script>
<BaseSlide>
<h3 class="text-6xl">{header}</h3>
</BaseSlide>
BaseSlide.svelte
<section class="flex h-screen items-center justify-center bg-slate-300">
<slot />
</section>
What I want to do here is define all the slides in the slides array in +page.svelte with props depending on the component’s type in Typescript. I’ve done some research and found out that components can be typed with writable<typeof SvelteComponent> in this StackOverflow post but the only import that I can find for writable gave me an error when I typed it as export let slides: writable<typeof SvelteComponent>[]; I also tried export let slides: Writable<typeof SvelteComponent>[]; but it gives an error on the import statement. How can I fix this?
>Solution :
writable creates a store, which has nothing to do with components themselves.
The type of a writable store would be Writable<T>.
The type of a component should just be ComponentType, so the slides would be typed as:
export let slides: { component: ComponentType }[]