STUDY/typescript
[error]Typescript image import 안되는 현상
3급우사기
2024. 3. 10. 20:06
타입스크립트로 처음 프로젝트 생성 후 진행 중에 이런 에러가 떳다.
경로입력시에 이미지 파일 이름이 자동완성되지도 않고 아예 인식이 안되는것 처럼 보였다.
처음 겪는 에러라 좀 당황했다...

Cannot find module '../../assets/파일이름' or its corresponding type declarations.
'../../assets/파일이름'모듈 또는 해당 형식 선언을 찾을 수 없습니다
해결법
1. src폴더 안에 custom.d.ts를 만든다.
2. 아래 처럼 작성한다.
declare module "*.jpg";
declare module "*.png";
declare module "*.jpeg";
declare module "*.gif";
이러면 작성한 확장자에서 에러가 사라진다!
참고사이트
https://www.carlrippon.com/using-images-react-typescript-with-webpack5/
Using images in React and TypeScript with Webpack 5
How to configure Webpack 5 so that images can be imported in a React app with TypeScript.
www.carlrippon.com