fix: Update type for getPages function (#491)

* Fix getPages type

* Even more precise type

---------

Co-authored-by: MacFJA <macfja@gmail.com>
This commit is contained in:
Lachlan Collins
2023-11-27 04:13:55 +09:00
committed by GitHub
parent 0518b5ed22
commit b61a6fdb05
4 changed files with 22 additions and 5 deletions

7
src/lib/Mdsvx.d.ts vendored Normal file
View File

@@ -0,0 +1,7 @@
export type RecipeMetadata = {
title: string;
layout: string;
date: string;
children?: Array<unknown>;
};
export type EventMetadata = { title: string; layout: string; date: string };

View File

@@ -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<EventMetadata>(
import.meta.glob<SvxMetadata<EventMetadata>>('./**/*.svx')
);
if (events) {
events.sort((a, b) => Date.parse(b.date) - Date.parse(a.date));

View File

@@ -1,6 +1,8 @@
export async function getPages(
metaGlob: Array<string>
): Promise<Array<{ [key: string]: string; filename: string; path: string; title?: string }>> {
export type SvxMetadata<T extends object> = { metadata: T };
export async function getPages<T extends object>(
metaGlob: Record<string, () => Promise<SvxMetadata<T>>> // Should be `ReturnType<vite.ImportGlobFunction>` but the ast function overload is pick, which is the wrong one
): Promise<Array<T & { filename: string; path: string }>> {
const pages = await Promise.all(
Object.entries(metaGlob).map(async ([fullPath, page]) => {
const { metadata } = await page();

View File

@@ -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<RecipeMetadata>(import.meta.glob<SvxMetadata<RecipeMetadata>>('./**/*.svx'))
).map((element) => ({
...element,
path: '/recipes' + element.path.substring(1)
}));