Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Import of images using import.meta.glob() as described in the Documentation is causing error: ts(2322) #5695

Closed
scharf-dev opened this issue Dec 8, 2023 · 4 comments · Fixed by #5898
Labels
help wanted Issues looking for someone to run with them! improve documentation Enhance existing documentation (e.g. add an example, improve description)

Comments

@scharf-dev
Copy link

In a project i started on v3, i used dynamic image importing as described in the Astro Documentation and it worked as expected.

This is the code:

---
import { Image } from "astro:assets";
const images =  import.meta.glob('../assets/images/slider/*.{jpeg,jpg,png,gif}');
---

<div id="slider" class="full-width img-wrapper">
  {Object.keys(images).map((path, index) => {
    return <Image src={images[path]()} alt="test"/>
  })}
</div>

Since i upgraded the project to Astro 4.0, i get the following error on the src property:

type 'Promise' is not assignable to type 'string | ImageMetadata | Promise<{ default: ImageMetadata; }>'.
Type 'Promise' is not assignable to type 'Promise<{ default: ImageMetadata; }>'.ts(2322)
types.d.ts(136, 5): The expected type comes from property 'src' which is declared here on type 'IntrinsicAttributes & Props'
(property) src: Promise

Despite the error, the images still show up on the dev server. However, astro build fails because of this.

@scharf-dev scharf-dev changed the title Dynamic Import of images using import.meta.glob() as descriped in the Documentation is causing error: ts(2322) Dynamic Import of images using import.meta.glob() as described in the Documentation is causing error: ts(2322) Dec 8, 2023
@lilnasy
Copy link
Contributor

lilnasy commented Dec 13, 2023

Since Vite 5, import.meta.glob became stricter, any became unknown. You can tell it the expected shape of the module like this:

import type { ImageMetadata } from "astro"
import { Image, ImageMetadata } from "astro:assets";
const images =  import.meta.glob<{ default: ImageMetadata }>('../assets/images/slider/*.{jpeg,jpg,png,gif}');

@lilnasy lilnasy added improve documentation Enhance existing documentation (e.g. add an example, improve description) help wanted Issues looking for someone to run with them! labels Dec 13, 2023
@VoxelMC
Copy link
Contributor

VoxelMC commented Dec 19, 2023

Since Vite 5, import.meta.glob became stricter, any became unknown. You can tell it the expected shape of the module like this:

import type { ImageMetadata } from "astro"
import { Image, ImageMetadata } from "astro:assets";
const images =  import.meta.glob<{ default: ImageMetadata }>('../assets/images/slider/*.{jpeg,jpg,png,gif}');

Shall I draft up a PR to reflect this suggestion?
I will test it later to make sure. After slight correction, is this correct?

import type { ImageMetadata } from "astro";
import { Image } from "astro:assets";
const images = import.meta.glob<{ default: ImageMetadata }>('../assets/images/slider/*.{jpeg,jpg,png,gif}');

@sarah11918
Copy link
Member

@VoxelMC Thanks for tackling this! A PR would be great, then you can tag @Princesseuh to confirm!

@Princesseuh
Copy link
Member

Funnily enough it's not that it became stricter, it's that the import was bugged in Vite 4, so it resolved to any

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issues looking for someone to run with them! improve documentation Enhance existing documentation (e.g. add an example, improve description)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants