Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type definition for vite-plugin-svgr?

I have typescript error while adding props to Svg as ReactComponent- type is not assignable to type 'IntrinsicAttributes'. Plugin: vite-plugin-svgr

vite.config.ts -

svgr({
      exportAsDefault: true,
      svgrOptions: {
        icon: true,
      },
    })

Added

"types" : "vite-plugin-svgr/client" 

to ts compiler and I import svg like

import Icon from "./path"

Any suggestions?

like image 829
iufb Avatar asked Nov 17 '25 23:11

iufb


2 Answers

For me i didn't create any custom file, you just need

  1. In tsconfig.json
"compilerOptions": 
{
    [...rest...]
    "types": ["vite-plugin-svgr/client"] <--- this line
},
  1. In vite.config.ts
import svgr from "vite-plugin-svgr";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), svgr()],
});
  1. In your tsx file in order to use it
import { ReactComponent as MySuperCustomIconComponent } from "../assets/logo.svg";

and simply use it

<MySuperCustomIconComponent/>
like image 136
Thanasis Kapelonis Avatar answered Nov 21 '25 08:11

Thanasis Kapelonis


There is a declaration helper that you just need to append to your vite-env.d.ts file. I had compatibility issues with other solutions.

// vite-env.d.ts
// ...
/// <reference types="vite-plugin-svgr/client" />
like image 39
Andreas Riedmüller Avatar answered Nov 21 '25 09:11

Andreas Riedmüller