skip to Main Content

I have a ts(2307) error when I import some jpg files from my src folder and vsc warns that ‘Cannot find module or its corresponding type declarations.’

Though, my code compiled and run well, but I don’t want to see the warning messages.
According to some references on the Google, it seems related to tsconfig.json file, but I’m very new to this typescript area … so..

Here is my code where the error comes out

import splashImg from '../assets/images/splashImg.jpg';
import naverLogin from '../assets/images/naverLogin.jpg';
import appleLogin from '../assets/images/appleLogin.jpg';

and Here is my tsconfig.json file

{
  "compilerOption" : {
    "target" : "esnext",
    "module" : "commonjs",
    "lib" : ["es2017"],
    "allowJs" : true,
    "jsx" : "react-native",
    "noEmit" : true,
    "strict" : true,
    "moduleResolution" : "node",
    "allowSyntheticDefaultImports" : true,
    "esModuleInterop" : true,
    "skipLibCheck" : false,
    "resolveJsonModule" : true
    },
    "exclude" : ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"]
    
 }

Thank you for your time, and thank you in advance.

Please Help 🙁

2

Answers


  1. import splashImg from '../assets/images/splashImg.jpg';
    

    change it to

    const splashImg = require('../assets/images/splashImg.jpg');
    

    change path according to your directory structure. if not work please share your project repo hierarchy

    Login or Signup to reply.
  2. in most baseline projects like nextjs or create-react-app you can simply add global.t.ds file in which you’ll be able to declare (and tell) typescript compiler "Hey! this type of file are perfectly fine modules to be consumed, no need to complain, Thanks!"

    // global.d.ts
    declare module "*.png";
    declare module "*.svg";
    declare module "*.jpeg";
    declare module "*.jpg";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search