Skip to content

Commit

Permalink
fix: updated type safety in component
Browse files Browse the repository at this point in the history
  • Loading branch information
bbtgnn committed Mar 1, 2024
1 parent 1d335c3 commit f0d4e59
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
28 changes: 25 additions & 3 deletions src/_modules/components/Relation.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import Document from './Document.svelte';
import type { Document } from '$modules/types';
import DocumentComponent from './Document.svelte';
import type { RelationField } from '$modules/fields';
import type { CollectionName } from '$modules/database';
Expand All @@ -9,10 +10,31 @@
type C = $$Generic<CollectionName>;
export let to: RelationField<C> | undefined;
//
/**
* THIS IS A HACK
*
* Aim: Enforce type-safety when using Relation component
* Why it's needed: Forwarding the "doc" slot prop of <DocumentComponent> breaks the type
*
* Important: All of the parts below are required
* 1. let doc variable
* 2. function castType
* 3. {_const doc} declaration
*
* The name used in 1. and 3. must match
*/
let doc: Document<C>;
function castType(data: any): Document<C> {
return data;
}
</script>

{#if to}
<Document collection={to.collection} name={to.document} let:doc>
<DocumentComponent collection={to.collection} name={to.document} let:doc={relation}>
{@const doc = castType(relation)}
<slot {doc} />
</Document>
</DocumentComponent>
{/if}
11 changes: 4 additions & 7 deletions src/routes/cv/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,10 @@
{#if document.props?.date_end}
<div class="rounded-xl border border-stone-300 p-4">
<p>{document.props.roles.join(', ')}</p>
<Document
collection={document.props.employer.collection}
name={document.props.employer.document}
let:doc
>
<p>{doc.props?.name}</p>
</Document>
<Relation to={document.props.employer} let:doc>
<p>{doc.props?.location}</p>
</Relation>

<p>
<span>
{formatDate(document.props.date_start, 'MM / yyyy')}
Expand Down

0 comments on commit f0d4e59

Please sign in to comment.