pngwn / MDsveX

A markdown preprocessor for Svelte.
https://mdsvex.pngwn.io
MIT License
2.44k stars 102 forks source link

How to import module type for dynamic imports? #508

Open AutomateAaron opened 1 year ago

AutomateAaron commented 1 year ago

I'm trying to dynamically import markdown blogs using the following:

Object.entries(import.meta.glob('./*.md')).map(async ([path, resolver]) => {
    const blog = await resolver();
    const slug = path.split('/').pop()?.split('.')[0];

    return {
        slug: slug,
        metadata: blog.metadata,
        content: blog.default,
    };
})

I'd like to define the type for the blog variable, but I can't figure out where to get a type for it.

akwirick commented 1 year ago

You can add a generic to the import.meta.glob<> that will get you what you want I think. If you load eagerly, it will look something like:

import.meta.glob<{
        default: SvelteComponent
        metadata: Record<string, string>
    }>(''./*.md'', { eager: true })

Without eager loading I think it's more like:


import.meta.glob<{
        default: Promise<SvelteComponent>
        metadata: Promise<Record<string, string>>
    }>('./*.md')