· 2 min read
Hero component: typed props, links and fallbacks
Built Hero.svelte (name, tagline, photo, links) and TeaserCard.svelte, and composed them in the home page.
What we did
- Built
Hero.svelte(name, tagline, photo, links) andTeaserCard.svelte, and composed them in the home page. - Added
src/lib/links.tswith aLinktype and a sharedprofileslist (GitHub, LinkedIn) used by the hero and the footer. - Replaced fake "Coming soon" cards with typed, empty lists and
{#each}…{:else}fallbacks.
What I learned
- The page holds the data and the component holds the layout. Hero knows nothing about my content.
Linkis a union of two shapes: aroute(aRouteId) or aurl(anhttps://string).'route' in linknarrows it, so TypeScript knows whether to callresolve()or use the URL.- A template literal type (
`https://${string}`) rejectshttp://and bare strings at compile time. - Required props (
name,links) turn a missing value into a type error. A default like''would render an empty<h1>: it looks fine, but screen readers get a heading with no text, and nothing warns me. - Optional props (
tagline?,photo?) are for things the component still makes sense without. <script module>runs once per file, not once per instance, so it's where a sharedTeasertype goes. Pages import it withimport TeaserCard, { type Teaser } from '…'.{#each list as item}…{:else}…{/each}renders a fallback for an empty list. When real data arrives, nothing else changes.as const satisfies readonly Link[]checks each entry is a validLinkbut keeps the exact type, so the footer knows every profile has aurl. With: Link[]it would need anif.- The
#lib/*alias inpackage.jsonmaps to exact paths, so TS files need the extension:#lib/links.ts.
Problems & fixes
Cannot find module '#lib/links': added the.tsextension.svelte-checkreported "<script>was left open" because a comment in my script contained the literal text<style>. It finds blocks by their tags in the raw text, so I reworded the comment.
Links
Next
- Write the real tagline (TODO in
src/routes/+page.svelte).