Overview
When using TypeScript on React, an error occur when trying to import an image.
So, fix the error that occurs when importing images in React.
What should I do ?
Create a type definition for the image to be used
#1 Install React with TypeScript
npx create-react-app my-app --template typescript
#2 Directory structure
#3 Place the image to be used
#4 Create a type definition
declare module "*.jpg";
#5 import type definition file
import "./App.css";
import "./types/image.d.ts";
import sample_image from "./assets/sample.jpg";
function App() {
return <img src={sample_image} alt="sample"/>;
}
export default App;
done.
Top comments (0)