Is import.meta.glob supposed to be able to infer correct types? #14869
Unanswered
einarpersson
asked this question in
Q&A
Replies: 2 comments
-
Hey @einarpersson I don't know if you found the answer to this since November, but if you didn't, here is my take on it:
Here is how I did it for global registration of some components: interface ModuleImportInterface {
default: Object;
}
//Inspired by https://zerotomastery.io/blog/how-to-auto-register-components-for-vue-with-vite/
const componentFiles = import.meta.glob('@/components/App*.vue', {
eager: true,
});
const componentFilesEntries = Object.entries(componentFiles);
for (const [componentPath, moduleImport] of componentFilesEntries) {
const componentName: string | undefined = componentPath
.split('/')
.pop()
?.replace('.vue', '');
if (!componentName) {
console.warn(
`The componentName couldn't be extracted from path > ${componentPath} `
);
continue;
}
app.component(
componentName!,
(moduleImport as ModuleImportInterface).default
);
console.info(`Registered component <${componentName!}> globally.`);
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
I find this way better. Hope this helps. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi!
Using
const modules = import.meta.glob('./*.svelte', { eager: true} )
in aindex.ts
file with siblings/Foo.svelte
and/Bar.svelte
Is
modules
supposed to be typed correctly? I getRecord<string, unknown>
.Is it today simply not possible to say "import all files in this folder and give me the results, which will be an array with types AS IF I had imported them one-by-one and put them in an array"?
(I see there seems to be a way to use generics to add types, but then it becomes a kind of chicken-and-egg problem. I have to first import all types to build some kind of tagged union and then use that type)
Related:
https://futurestud.io/tutorials/vite-resolve-import-meta-glob-in-typescript
https://vitejs.dev/guide/features.html#glob-import
#9599
Using
"vite": "4.4.11"
in the context of"@sveltejs/kit": "1.25.1"
and"svelte": "4.2.1"
Beta Was this translation helpful? Give feedback.
All reactions