From b61a6fdb057ea33d6987ba3ae980c7b8faa55db3 Mon Sep 17 00:00:00 2001 From: Lachlan Collins <1667261+lachlancollins@users.noreply.github.com> Date: Mon, 27 Nov 2023 04:13:55 +0900 Subject: [PATCH] fix: Update type for getPages function (#491) * Fix getPages type * Even more precise type --------- Co-authored-by: MacFJA --- src/lib/Mdsvx.d.ts | 7 +++++++ src/routes/events/+page.ts | 6 +++++- src/routes/pageList.ts | 8 +++++--- src/routes/recipes/+layout.ts | 6 +++++- 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 src/lib/Mdsvx.d.ts diff --git a/src/lib/Mdsvx.d.ts b/src/lib/Mdsvx.d.ts new file mode 100644 index 0000000..5a7b623 --- /dev/null +++ b/src/lib/Mdsvx.d.ts @@ -0,0 +1,7 @@ +export type RecipeMetadata = { + title: string; + layout: string; + date: string; + children?: Array; +}; +export type EventMetadata = { title: string; layout: string; date: string }; diff --git a/src/routes/events/+page.ts b/src/routes/events/+page.ts index e9126e5..b014e4e 100644 --- a/src/routes/events/+page.ts +++ b/src/routes/events/+page.ts @@ -1,8 +1,12 @@ import { error } from '@sveltejs/kit'; import { getPages } from '../pageList'; +import type { SvxMetadata } from '../pageList'; +import type { EventMetadata } from '$lib/Mdsvx'; export async function load() { - const events = await getPages(import.meta.glob('./**/*.svx')); + const events = await getPages( + import.meta.glob>('./**/*.svx') + ); if (events) { events.sort((a, b) => Date.parse(b.date) - Date.parse(a.date)); diff --git a/src/routes/pageList.ts b/src/routes/pageList.ts index f832a59..6b38e89 100644 --- a/src/routes/pageList.ts +++ b/src/routes/pageList.ts @@ -1,6 +1,8 @@ -export async function getPages( - metaGlob: Array -): Promise> { +export type SvxMetadata = { metadata: T }; + +export async function getPages( + metaGlob: Record Promise>> // Should be `ReturnType` but the ast function overload is pick, which is the wrong one +): Promise> { const pages = await Promise.all( Object.entries(metaGlob).map(async ([fullPath, page]) => { const { metadata } = await page(); diff --git a/src/routes/recipes/+layout.ts b/src/routes/recipes/+layout.ts index 495d14d..cf293e9 100644 --- a/src/routes/recipes/+layout.ts +++ b/src/routes/recipes/+layout.ts @@ -1,9 +1,13 @@ import { error } from '@sveltejs/kit'; import '$styles/highlight.css'; import { getPages } from '../pageList'; +import type { SvxMetadata } from '../pageList'; +import type { RecipeMetadata } from '$lib/Mdsvx'; export async function load() { - const pages = (await getPages(import.meta.glob('./**/*.svx'))).map((element) => ({ + const pages = ( + await getPages(import.meta.glob>('./**/*.svx')) + ).map((element) => ({ ...element, path: '/recipes' + element.path.substring(1) }));