【问题标题】:Cannot find module 'file-name.png' or its corresponding type declarations - typescript react找不到模块'file-name.png'或其相应的类型声明 - 打字稿反应
【发布时间】:2022-02-13 19:01:05
【问题描述】:

我正在尝试在我的 typescript react 项目中导入 png,如下所示:
import logo from 'assets/Logo.svg';

我收到了这个 TS 错误:
Cannot find module 'assets/Logo.svg' or its corresponding type declarations.ts(2307)

知道这是为什么吗?我在下面附上了我的tsconfig.json 文件,以防我配置错误:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "baseUrl": "src"
  },
  "include": ["src"]
} 

【问题讨论】:

标签: reactjs typescript


【解决方案1】:

有一个react-app-env.d.ts 项目中的文件 只需在该文件中添加以下代码即可。

declare module "*.png";
declare module "*.svg";
declare module "*.jpeg";
declare module "*.jpg";

【讨论】:

  • 我在我的项目中没有看到这样的文件?我在我的 src 文件夹中添加了一个具有该名称和内容的文件,但错误仍然存​​在
  • @Tamjid int 应该在您的 src 文件中。 /// 声明模块 ".png";声明模块“.svg”;声明模块“.jpeg”;声明模块“.jpg”;
  • 谢谢你,我把它放在我项目的根文件夹中,把文件放在我的src文件夹中解决了这个问题。
猜你喜欢
  • 2021-05-03
  • 1970-01-01
  • 2022-12-16
  • 2020-10-19
  • 2021-09-21
  • 2020-12-23
  • 2020-08-16
  • 2021-02-20
  • 1970-01-01
相关资源
最近更新 更多