在 create-react-app 中找不到 svg 模块打字稿错误

Rob*_*ohr 7 svg typescript create-react-app react-typescript

在 create-react-app 中,我将正在运行的 jsx 文件转换为 tsx 文件,并添加以下行:

import { ReactComponent as Logo } from "./logo.svg";
Run Code Online (Sandbox Code Playgroud)

抛出打字稿错误:找不到模块“./logo.svg”或其相应的类型声明.ts(2307)

在 tsconfig.json 我有:

{
  "include": ["src", "./src/**/*.ts", "./src/**/*.svg"]
}
Run Code Online (Sandbox Code Playgroud)

看起来 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;
}
Run Code Online (Sandbox Code Playgroud)

所以我不确定问题是什么。

SVG 的开头如下:

<svg xmlns="http://www.w3.org/2000/svg" height="100" width="100"><path d="M38.0
Run Code Online (Sandbox Code Playgroud)

Eze*_*llo 9

我认为它需要对react-scripts库的显式引用。

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

/// <reference types="react-scripts" />
Run Code Online (Sandbox Code Playgroud)

该文件通常由 Create React App 创建。