TS

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ƒ์—์„œ svg ํŒŒ์ผ์„ ์ปดํฌ๋„ŒํŠธ๋กœ์„œ importํ•  ๋•Œ ๋ฌธ์ œ

choi95 2022. 4. 11. 21:13

๋ฌธ์ œ

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•œ ํ”„๋กœ์ ํŠธ ๋‚ด์—์„œ svg ํŒŒ์ผ์„ ์ปดํฌ๋„ŒํŠธ๋กœ์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

import { ReactComponent as ProfileIcon } from '/images/icon-profile.svg';

ํ•˜์ง€๋งŒ ํ•ด๋‹น ์ฝ”๋“œ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

"*.svg"' ๋ชจ๋“ˆ์— ๋‚ด๋ณด๋‚ธ ๋ฉค๋ฒ„ 'ReactComponent'์ด(๊ฐ€) ์—†์Šต๋‹ˆ๋‹ค.

 

๋ฌธ์ œ ํ•ด๊ฒฐ

์Šคํƒ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ์—์„œ ๋‚˜์™€ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ๋ฐœ๊ฒฌํ•˜์˜€๊ณ  ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.https://stackoverflow.com/questions/54121536/typescript-module-svg-has-no-exported-member-reactcomponent

 

TypeScript - Module '"*.svg"' has no exported member 'ReactComponent

I'm trying to import an .svg file as a React component with TypeScript. According to the React docs, this is done like so: import { ReactComponent as Icon } from './Icon.svg'; Following the

stackoverflow.com

custom.d.ts ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ฃผ๊ณ  ํ•ด๋‹น ํŒŒ์ผ ๋‚ด์—์„œ ๋ชจ๋“  svg ํŒŒ์ผ์— ๋Œ€ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์†์„ฑ์„ ์„ค์ •ํ•ด ์ฃผ์—ˆ๋‹ค.

declare module '*.svg' {
    import React = require('react');
    export const ReactComponent: React.FC<React.SVGProps<SVGSVGElement>>;
    const src: string;
    export default src;
  }

๋˜ํ•œ ts ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ts|tsx ํ™•์žฅ์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก tsconfig.json์— custom.d.ts๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ํ•ด์ฃผ์–ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. 

{
  "compilerOptions": {
  
  ( ... )
   
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "next.config.js",
    "custom.d.ts",
    "styles",
    "pages",
    "public"
  ],
  
 ( ... )
}