skip to Main Content

I’m working on a React Native project with TypeScript, and I’m trying to import an image like this:

Cannot find module '@/assets/image.png' or its corresponding type declarations.ts

I’ve already checked that the image exists at the specified path. How do I resolve this error and properly handle static assets like images in a TypeScript-based project?
Any help would be appreciated!

2

Answers


  1. Chosen as BEST ANSWER
    
    Step 1: Create a declarations.d.ts file
    In the root of your project (or in a src/types folder if you have one), create a file called `declarations.d.ts`.
    
    Inside this file, add the following code to declare the module types for image assets like PNG, JPEG, etc.```
    
    `declare module '*.png' {
      const value: any;
      export default value;
    }`
    
    `declare module '*.jpg' {
      const value: any;
      export default value;
    }`
    
    `declare module '*.jpeg' {
      const value: any;
      export default value;
    }`
    
    `declare module '*.svg' {
      const value: any;
      export default value;
    }
    `
    

  2. Create a index.d.ts file in the src folder and insert this declaration.

    declare module '*.png';
    

    Restart the TS Compiler if issue persists.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search