【发布时间】:2021-07-03 22:27:04
【问题描述】:
我正在尝试使用 import
添加图片import image from '../assets/images/picture.jpg
我在 gatsby 中使用 Typescript ... 奇怪的是 它在普通 javascript 中工作正常(.js ~ .jsx 文件)但是当我使用时(.ts ~ .tsx 文件)它抛出这个错误
找不到模块“../assets/images/picture.jpg”或其对应的类型声明。ts(2307)
奇怪的是它与普通组件一样工作正常
import Header from '../components/Header'
这是我的 tsconfig.json 文件
{
"compilerOptions": {
"module": "commonjs",
"target": "esnext",
"jsx": "preserve",
"lib": [
"dom",
"es2015",
"es2017"
],
"strict": true,
"noEmit": true,
"isolatedModules": true,
"esModuleInterop": true,
"skipLibCheck": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": false,
"preserveConstEnums": true
},
"include": [
"./src/**/*"
]
}
这里是 gatsby-config.json 文件
module.exports = {
/* Your site config here */
plugins: [
{
resolve: `gatsby-plugin-typescript`,
options: {
isTSX: true, // defaults to false
jsxPragma: `jsx`, // defaults to "React"
allExtensions: true, // defaults to false
},
},
],
}
文件夹结构
- 源
- 组件
- MainComponent.tsx
- Header.tsx
- 资产
- 图片
- 图片.jpg
- 图片
- 组件
【问题讨论】:
-
你能分享一下你的项目结构吗?
-
@FerranBuireu 我添加了它
-
Typescript 不处理图像导入,webpack 可以。也许只是将 allExtensions 改回 false。
-
实际上,它确实...我在使用 typescript 的反应中使用了图像导入
标签: typescript visual-studio-code import gatsby tsconfig