在create-react-app中找不到svg模块的typescript错误。

6
在create-react-app中,我将一个工作的jsx文件转换为tsx文件,以下是需要转换的代码行:
import { ReactComponent as Logo } from "./logo.svg";

出现了typescript错误:无法找到模块'./logo.svg'或其对应的类型声明.ts(2307)

在tsconfig.json中我添加了如下内容:

{
  "include": ["src", "./src/**/*.ts", "./src/**/*.svg"]
}

看起来 node_modules/react-scripts/lib/react-app.d.ts 已经有了:


declare module '*.svg' {
  import * as React from 'react';

  export const ReactComponent: React.FunctionComponent<React.SVGProps<
    SVGSVGElement
  > & { title?: string }>;

  const src: string;
  export default src;
}

所以我不确定问题出在哪里。

这个SVG的开头是以下内容:

<svg xmlns="http://www.w3.org/2000/svg" height="100" width="100"><path d="M38.0
1个回答

8

我认为它需要明确引用 react-scripts 库。

我有这个文件 src/react-app-env.d.ts,其中包含这一行:

/// <reference types="react-scripts" />

这个文件通常是由 Create React App 创建的。


这个问题是由于手动迁移到TS,而不是使用--template typescript的create-react-app引起的吗? - Ryuujo

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