전체 방문자
오늘
어제
  • 전체 글
    • HTML
    • CSS
    • Javascript
    • React
    • Typescript
    • Next.js
    • Webpack
    • Vue.js
    • Git & GitHub
    • Error
    • Study
    • 개발 일지✨

블로그 메뉴

  • 💡
  • ⚙️
hELLO · Designed By 정상우.
하루

Home

[TS] TypeScript가 SVG 파일을 import 하지 못해서 에러가 발생할 때
Error

[TS] TypeScript가 SVG 파일을 import 하지 못해서 에러가 발생할 때

2022. 11. 20. 01:07

TypeScript에서 svg 파일을 import 해오고자 할 때 svg에 대한 타입을 찾을 수 없으면 위와 같은 에러가 발생한다.

별도의 d.ts 파일을 만들어서 svg 파일에 대한 타입을 지정해주면 에러가 사라진다.

 

해결방안

// custom.d.ts

declare module '*.svg' {
  const content: React.FunctionComponent<React.SVGAttributes<SVGElement>>;
  export default content;
}

 

이후 tsconfig.json 파일에서 아래와 같이 webpack 설정을 추가해준다. (어떤 파일들에 어떤 로더를 적용할지)

"include": ["src/components", "src/custom.d.ts"]

 

 

출처/참고

  • https://tesseractjh.tistory.com/227
  • https://stackoverflow.com/questions/44717164/unable-to-import-svg-files-in-typescript
    'Error' 카테고리의 다른 글
    • [React] 이미지 위치에 따른 오류 해결 You can either move it inside src/, or add a symlink to it from project's node_moules/.
    • Delete `␍` prettier/prettier 해결 방법
    • Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named i..
    • windows 10 에서 docker 설치 후 open //./pipe/docker_engine: The system cannot find the file specified

    티스토리툴바