在TypeScript React中导入图片 - "找不到模块"

218

我正在尝试在使用TypeScript的React组件中导入图像。我使用的打包工具是Parcel(而不是Webpack)。

我在项目中创建了一个带有图像文件扩展名的 .d.ts 文件,并将其包含在 tsconfig.json 中。但是,当我尝试导入图像时,TS会抱怨找不到模块。

我的项目结构:

+ src
  + assets
    - image.jpg
  + components
    - Box.tsx
    - App.tsx
  - index.d.ts
  - index.html
  - index.tsx
- tsconfig.json
- tslint.json

我尝试像这样在 App.tsx 中导入图像。VS Code下划线标记了 '../assets/image.jpg',并显示了 无法找到模块'../assets/image.jpg'

import * as React from 'react';
import * as img from '../assets/image.jpg';

const Box = props => {
  // do things...
}

export default Box;

我在网上找到的讨论指出需要自己定义一个.d.ts文件,因此我创建了这个带有以下代码行的index.d.ts文件。

declare module '*.jpg';

然后在 "compilerOptions" : {...} 后面的 tsconfig.json 中添加 "include": ["./src/index.d.ts"]

我错过了什么?我该如何解决 TS 抛出的错误?


请查看 https://github.com/Microsoft/TypeScript/issues/15146。 - diegosasw
你可以像这样做 https://dev59.com/wFcP5IYBdhLWcg3w6-EH#66251201 - Carlos
有没有任何@types包可以直接提供这些声明,这样我就不必自己明确地提供它们了? - Paul Razvan Berg
有没有任何@types包可以直接提供这些声明,这样我就不必自己明确地提供它们了? - undefined
20个回答

3
在您的 src 目录或任何 rootDir 中创建一个 global.d.ts 文件。
declare module "*.png" {
 export default "" as string
}

这将利用一项资产,本例中为png图像,并清除vscode错误。

3

我测试了所有的回复,并找到了答案。

首先,在您的路由文件夹中创建一个名为.d.ts的文件。

然后将这些行复制到该文件中。


declare module "*.png" {
  export default "" as string;
}
declare module "*.svg" {
  export default "" as string;
}
declare module "*.jpeg" {
  export default "" as string;
}
declare module "*.jpg" {
  export default "" as string;
}


最后,前往你的路由文件夹中的tsconfig.json文件,并定义新的子元素。

{
   "include": ["src", ".d.ts"]
}

你已经准备好了。


1

这对我起了作用

declare module "*.png" {
const src: string
export default src
}

1

Example false positive

上图说明了当您错误引用了路径时,此错误会被误报的情况。请注意,我不小心使用了双引号 "" 开始并以单引号 " 结尾。很容易修复。

1

我在根目录中添加了文件index.d.ts

将以下代码粘贴到其中:/// <reference types="react-scripts" />

tsconfig.json中添加:"include": ["src", "index.d.ts"]


0

当你的src文件夹中没有包含react-app-env.d.ts文件时,就会出现这个错误。因为你需要两行代码来解决这个错误 ==> <reference types="react-scripts" />


0
如果你尝试在src文件夹中创建index.d.ts文件,并在其中添加declare module '*.jpg';,但它没有起作用,只需将index.d.ts重命名为其他名称,比如export.d.ts。

0
为了解决这个问题,请在 src/Types 目录下创建一个名为 index.d.ts 的文件。在该文件中添加以下声明:
declare module '*.jpg';

这个声明允许TypeScript识别和导入JPG图像文件,而不会出现任何类型错误。


0

针对在单体库中遇到此问题的人

首先让我们澄清一件事,我假设您正在使用create react app或vite,并且仍然遇到此问题。尽管这确实不应该有影响。

我是如何陷入这个困境的?

我只是有一个单体库,我正在从根tsconfig.json扩展,这是在整个单体库中保留每个共享配置的地方。因此,在我的单体库中的每个子存储库中都会从它进行扩展。好的,这是我的目录树的样子

|-apps
|---backend
|-----src
|-----tsconfig.json
|---frontend
|-----src
|-----tsconfig.json
|-tsconfig.json

e.x. 应用程序/前端/tsconfig.json

{
    "extends": "../../tsconfig.json",
    "compilerOptions": {
        /* ... */
    }
}

解决方案 #1

因此,TS 无法找到图像文件的定义。我所做的就是在我的 monorepo 根目录中创建一个名为 declarations.d.ts 的新文件,并将以下代码粘贴到其中:

// images
declare module '*.png' {
    const src: string;
    export default src;
}
declare module '*.jpg' {
    const src: string;
    export default src;
}
declare module '*.jpeg' {
    const src: string;
    export default src;
}
declare module '*.jfif' {
    const src: string;
    export default src;
}
declare module '*.pjpeg' {
    const src: string;
    export default src;
}
declare module '*.pjp' {
    const src: string;
    export default src;
}
declare module '*.gif' {
    const src: string;
    export default src;
}
declare module '*.svg' {
    const src: string;
    export default src;
}
declare module '*.ico' {
    const src: string;
    export default src;
}
declare module '*.webp' {
    const src: string;
    export default src;
}
declare module '*.avif' {
    const src: string;
    export default src;
}

解决方案 #2

虽然您可以通过在./apps/frontend/tsconfig.json中删除那个扩展来解决此问题。但我选择删除扩展,因为依赖于Vite比依赖于自己的代码更容易。此外,这样我们就少了一些需要担心的代码。


0
在 Blitz.js(基于 Next.js)中,这对我来说是有效的:
npx tsc-files --pretty --noEmit next-env.d.ts filenames

在 package.json 文件中:
"lint-staged": {
  "*.{ts,tsx}": "tsc-files --pretty --noEmit next-env.d.ts"
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接