First, I don't think Gatsby is involved in problem as runnning tsc -p .
will detect the same compilation errors, before gatby's yarn start
is ran.
I have a folder with some react tsx files I want to compile
src/
@types/
index.d.ts
components/
bottom/bottom.tsx
layout.tsx
images/
email.png
costs.png
gatsby-config.js
gatsby-node.js
tsconfig.json
package.json
I want bottom.tsx
to load email.png, but I have this error
As written in this post, I declare all pictures as modules in src/@types/index.d.ts
. I have created a interface in this file for test urpose, and the file is correctly read by the compiler.
declare module '*.jpg';
declare module '*.png';
export interface Thing {
name: string
}
Using import as or adding content to module declaration won't change anything. However the code is running fine if I ignore typescript compilator :
//@ts-ignore
import email from '../../images/email.png'
//@ts-ignore
import logo from '../../images/logo-bw.png'
It works, so the structure of the code is ok with Gatsby, but obviously I lose a lot of benefits of using typescript as images is a big part of a website... Plus, there is no IDE autocompletion to help image import.
This Gatsby starter is made to be open source, so you can check the configuration at this branch : https://github.com/robusta-code/gatsby-the-robust/tree/0.0.1
Note that loading css or sass modules would be ok : import '../styles/home.scss'
To fix the "Cannot find module" error with importing images in TypeScript React, we should declare the . jpg module in a TypeScript type definition file. declare module '*.
To allow image import with TypeScript, we can use declare to declare the type for image files. to declare the type for modules with the . png extension.
Since Gatsby natively supports JavaScript and TypeScript, you can change files from . js / . jsx to .
To fix the "Cannot find module" error with importing images in TypeScript React, we should declare the .jpg module in a TypeScript type definition file. ← How to check against custom type with ‘typeof’ in TypeScript? → How to use Node.js require inside a TypeScript file?
TypeScript integration is supported through automatically including gatsby-plugin-typescript. Visit that link to see configuration options and limitations of this setup. If you are new to TypeScript, check out these other resources to learn more:
In the last post, we added CSS to our Webpack configuration for our React and TypeScript app. In this post, we will extend this Webpack configuration so that images can be used in the app. If we run the app in dev mode ( npm start ), we see that the image isn’t found:
To fix the "Cannot find module" error with importing images in TypeScript React, we should declare the .jpg module in a TypeScript type definition file. in index.d.ts in the TypeScript project folder to let the TypeScript compiler know that .jpg files can imported into our project directly.
Wow... The problem is that my index.d.ts
was exporting an interface !
In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well)
Removing export interface Thing{}
was enough.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With