· 5 min read · updated

Typed props and links in Svelte 5 components

How I typed my hero component's props, made links that can't point to a missing page, and handled empty lists, with a few TypeScript tricks along the way.

Machine with shape holes

The top of my portfolio's home page is a "hero": my name, a tagline, a photo and some links. Building it was a good excuse to get Svelte 5 props and TypeScript working together properly.

The page holds the data, the component holds the layout

The hero doesn't know anything about me. It takes everything as props:

svelte
<script lang="ts">
	import type { Picture } from '@sveltejs/enhanced-img';
	import type { Link } from '#lib/links.ts';

	interface Props {
		name: string;
		links: Link[];
		tagline?: string;
		photo?: Picture;
	}

	let { name, links, tagline, photo }: Props = $props();
</script>

The home page passes the content in:

svelte
<Hero name="Alfonso Baeza" tagline="Web developer." {photo} {links} />

Required or optional?

My first instinct was to give every prop a default, to be safe. But think about what a default does for name:

ts
let { name = '' }: Props = $props(); // ❌

If I forget to pass it, the page renders an empty <h1>. It looks fine. Screen readers, though, announce a heading with no text, and nothing warns me. Making name required turns that silent bug into a type error.

So I use one rule:

  • Required when the component makes no sense without it: name, links.
  • Optional when it still works without it: the hero renders fine with no tagline or no photo.

The hero has two kinds of links: pages on my site, and external profiles. One type covers both:

ts
import type { RouteId } from '$app/types';

export type Link = { label: string; route: RouteId } | { label: string; url: `https://${string}` };
  • RouteId is generated by SvelteKit from my actual routes. A typo like '/porjects' is a type error.
  • `https://${string}` is a template literal type. 'http://…' or a bare 'github.com' don't compile.
  • A link has route or url, never both.

In the template, the in operator tells TypeScript which kind of link it has:

svelte
<a href={'route' in link ? resolve(link.route) : link.url}>{link.label}</a>

Inside the first branch, TypeScript knows link.route exists. This is called narrowing.

Later, adding a route with a parameter (/devlog/[slug]) broke this. resolve('/devlog/[slug]') needs a slug too, so resolve() no longer accepts "any RouteId" on its own. These links never point to a route with a parameter, so the type says so now:

ts
export type RouteWithoutParams = Exclude<RouteId, `${string}[${string}`>;

export type Link =
	{ label: string; route: RouteWithoutParams } | { label: string; url: `https://${string}` };

Exclude removes every route id with a [ in it. It's the same template literal trick as the https:// one.

satisfies: check the type, keep the details

My GitHub and LinkedIn links appear in the hero and in the footer, so they live in one list:

ts
export const profiles = [
	{ label: 'GitHub', url: 'https://github.com/abafdez' },
	{ label: 'LinkedIn', url: 'https://www.linkedin.com/in/alfonso-baeza-fdez/' }
] as const satisfies readonly Link[];

If I'd written const profiles: Link[] = …, TypeScript would forget that these are all URLs. The footer would need an if ('url' in profile) check for every entry. satisfies checks that each entry is a valid Link without widening the type, so the footer can use profile.url directly.

Sharing a type from a component

The teaser cards for "latest posts" have their own props. To let pages type their lists, the card exports its props type from a <script module> block, which runs once per file instead of once per card:

svelte
<script module lang="ts">
	import type { ResolvedPathname } from '$app/types';

	export interface Teaser {
		title: string;
		description?: string;
		href?: ResolvedPathname;
	}
</script>

The card takes a link that's already resolved. The page that renders it knows the exact route, so it calls resolve('/devlog/[slug]', { slug }), and that call checks the route and its params. Inside the card, the route could be any of them, and TypeScript couldn't check the params.

ts
import TeaserCard, { type Teaser } from '#lib/components/TeaserCard.svelte';
const posts: Teaser[] = [];

Empty lists get a fallback, not fake data

My blog doesn't exist yet. My first version showed three fake "Coming soon" cards. The better version uses the real (empty) list, with Svelte's {:else}:

svelte
{#each posts as post (post.title)}
	<TeaserCard {...post} />
{:else}
	<p>No posts yet. They'll show up here once the blog is live.</p>
{/each}

When the blog arrives, I fill the array and nothing else changes.

One gotcha: file extensions in aliases

My project uses Node's imports field in package.json for the #lib alias. It maps to exact file paths, so TypeScript files need their extension:

ts
import type { Link } from '#lib/links.ts'; // ✅
import type { Link } from '#lib/links'; // ❌ Cannot find module