Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'FunctionComponent<SVGAttributes<SVGElement>>' is not assignable to type 'string'.ts

I am getting below error when I converted my js based react app into typescript.

enter image description here

Type 'FunctionComponent<SVGAttributes<SVGElement>>' is not assignable to type 'string'.ts(2322)
index.d.ts(2188, 9): The expected type comes from property 'src' which is declared here on type 'DetailedHTMLProps<ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement>'

Below is my App.tsx file

import React from 'react';
import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.tsx</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

Here the src attribute of image tag shows this compile error

No idea how to resolve this

My react version is 9.1.2

I tried creating a react app with template specified as typescript, the same code works fine and I copied that tsx file contents into my react app, it shows the same error.

npx create-react-app myapp --template typescript

The same content on that app works without any problem. I coped the tsconfig.json as well into my main project. That doesn't helped in anyway.

like image 823
Sandeep Thomas Avatar asked Apr 26 '26 03:04

Sandeep Thomas


1 Answers

Change your content from

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

to

declare module "*.svg" {
    const content: any;
    export default content;
}
like image 71
Renat Avatar answered Apr 27 '26 20:04

Renat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!