简体   繁体   English

使用 ESlint 在 TypeScript 项目中使用 SVG 的正确方法

[英]Correct way of working with SVG in a TypeScript React project with ESlint

In my project I am using such technologies as TypeScript, React and ESlint.在我的项目中,我使用了 TypeScript、React 和 ESlint 等技术。
The problem is that eslint is always rising an error for svg files问题是 eslint 总是对 svg 文件报错
"Unsafe assignment of an any value.(eslint@typescript-eslint/no-unsafe-assignment)". “任何值的不安全分配。(eslint@typescript-eslint/no-unsafe-assignment)”。
在此处输入图像描述

In root of my src folder I have custom.d.ts with svg module declared:在我的 src 文件夹的根目录中,我声明了带有 svg 模块的 custom.d.ts:

// declare module "*.svg";

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

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

  const src: string;
  export default src;
}

tried both declarations commented one and the one taken from create-react-app.尝试了两种声明都评论了一个,一个来自 create-react-app。
The way I am using SVG's in the project:我在项目中使用 SVG 的方式:

import Logo from "images/logo.svg";

<Link to={PATH.ROOT} className={styles.logo}>
    <img src={Logo} alt="Logo" className={styles.icon} />
     <span className={styles.title}>Ascension Wiki</span>
</Link>

To get rid of the error I can use type casting like:为了摆脱错误,我可以使用类型转换,如:

<img src={Logo as string} alt="Logo" className={styles.icon} />

Or turn off the rule in ESlint, but I think there should be a better way of dealing with this issue.或者关闭 ESlint 中的规则,但我认为应该有更好的方法来处理这个问题。

using this declaration removes the eslint error for me:使用此声明为我消除了 eslint 错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用 typescript 反应 CRA 不能与 eslint 一起使用 - React CRA with typescript not working with eslint 使用ESlint整理React + Typescript项目 - Linting React+Typescript project with ESlint 创建 React 应用程序:ESLint 配置不适用于 Typescript - Create React App: ESLint config is not working with Typescript 在 React 项目中使用 Prettier 安装 ESlint 的正确方法 - Proper way of Installing ESlint with Prettier in a React Project 如何使用 ESLint 检查 React/Typescript 项目中的 prop 错误? - How do you check for prop errors in a React/Typescript project with ESLint? Typescript baseUrl 在 React Typescript 项目中不起作用 - Typescript baseUrl not working in React Typescript project 带有子文件夹的 typescript 项目中的 eslint 配置 - eslint configuration in a typescript project with subfolders 将 SVG 图像添加到 React 项目的简单方法 - Simple way to add SVG images into a React project 使用 React、React 测试库和 Cypress 为 Typescript 项目设置 ESLint - Setting up ESLint for a Typescript Project using React, React Testing Library, and Cypress React 类型文件在 React TypeScript (.tsx) 文件中不起作用 - 为 React 组件抛出 ESLint 错误 - React types files not working in React TypeScript (.tsx) file - Throwing ESLint errors for React Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM