简体   繁体   English

如何修复 eslint 错误,即在实际使用 TypeScript props 接口时未使用它?

[英]How can I fix the eslint error saying a TypeScript props interface is not used when it's actually used?

I have this component using AvatarProps as type for the props :我有这个组件使用 AvatarProps 作为 props 的类型:

在此处输入图像描述

Here's the interface declaration:这是接口声明:

export interface AvatarProps {
  userName: string;
  userLastName: string;
  userImg?: string;
  onPress?: Function;
  backgroundColorAvatar?: string;
  editMode?: boolean;
  onPressEdit?: Function;
  editButtonIcon?: string;
  backgroundColorEditButton?: string;
  textStyle?: TextStyle;
}

When running eslint, this error pops up :运行 eslint 时,会弹出这个错误:

5:15  error    'AvatarProps' is defined but never used  

eslint is triggered on commit with lefthook from a lefthook.yml file like so : eslint 在提交时触发 lefthook.yml 文件中的 lefthook,如下所示:

 pre-commit:
  parallel: true
  commands:
    lint:
      files: git diff --name-only @{push}
      glob: "*.{js,ts,jsx,tsx}"
      run: npx eslint {files}
    types:
      files: git diff --name-only @{push}
      glob: "*.{js,ts, jsx, tsx}"
      run: npx tsc --noEmit
commit-msg:
  parallel: true
  commands:
    commitlint:
      run: npx commitlint --edit

What can I do to get rid of the error in a clean way?我该怎么做才能以干净的方式摆脱错误?

Could you try to change your component to this:您可以尝试将您的组件更改为:

import React from 'react'
import type { AvatarProps, DimensionsType } from '../shared/types';

const Avatar: React.FC<AvatarProps> = ({
        userName,
        userLastName,
        userImg,
        onPress,
        backgroundColorAvatar,
        editMode,
        onPressEdit,
        editButtonIcon,
        backgroundColorEditButton,
        textStyle,
    }) => {
        return <>
           {/* Here your component implementation */}
        </>;
    };

that's a way to describe the types of a functional component at least is more clean and descriptive.这是一种描述功能组件类型的方法,至少更清晰和更具描述性。 Please notice React.FC<YourType> allows you to extend all the common props a functional component has, plus all YourType fields.请注意React.FC<YourType>允许您扩展功能组件具有的所有常见道具,以及所有YourType字段。 This allows you to render children's components like this:这允许您像这样渲染子组件:

import React from 'react';
import { Text } from 'react-native';

// Yous an example type
export interface AvatarProps {
    userName: string;
    userLastName: string;
}

/**
 * A children component that doesn't render nothing but you can implement
 * whatever you want here
 */
const MyChildrenComponent = () => {
    return <></>;
};

const Avatar: React.FC<AvatarProps> = ({ userName, userLastName, children }) => {
    return (
        <>
            {children}
            <Text>{userName + ' ' + userLastName}</Text>
            {/* Here the rest of your component implementation */}
        </>
    );
};

const SomeParentComponent = () => {
    return (
        <Avatar userName="Something" userLastName="">
            {/* Notice here I can insert my own component as a children of Avatar component */}
            <MyChildrenComponent />
        </Avatar>
    );
};

this approach has several cons (you can read more about it here ) because basically you are typing the function and not his arguments (props) and the children argument is always implied even if you use it or not.这种方法有几个缺点(您可以 在此处阅读有关它的更多信息),因为基本上您输入的是函数而不是他的参数(道具),并且即使您使用或不使用它,也总是隐含children参数。

UPDATE: As mentioned in the comments React.FC isn't needed actually you can implement your component without using it like this:更新:正如评论中提到的那样,实际上不需要React.FC ,您可以在不使用它的情况下实现组件:

const Avatar: (props: AvatarProps) => JSX.Element = ({
    userName,
    userLastName,
    userImg,
    onPress,
    backgroundColorAvatar,
    editMode,
    onPressEdit,
    editButtonIcon,
    backgroundColorEditButton,
    textStyle,
}) => {
    return <>{/* Here your component implementation */}</>;
};

where JSX.Element is the return value of your component in this case (there are also different types you can use to type the return of a functional component, this is just an example)其中JSX.Element在这种情况下是您的组件的返回值(您还可以使用不同的类型来键入功能组件的返回值,这只是一个示例)

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

相关问题 当映射函数打字稿中未使用索引时,摆脱 eslint@typescript-eslint/no-unused-vars 错误 - Get rid of eslint@typescript-eslint/no-unused-vars error when index is not used in map function typescript 如何绕过 ESLint 调用 Typescript 中未定义的 Geolocation 接口? - How can I bypass ESLint calling the Geolocation interface undefined in Typescript? typescript-eslint 未使用 tsconfig - tsconfig not used by typescript-eslint 如何将其传递给 this 的属性中使用的接口? - How can I pass this to an interface used in a property of this? Vue中使用的接口的Eslint no-unused-vars错误显示 - Eslint no-unused-vars error shows for used interface in Vue Typescript 错误 - “类型已声明但从未使用” in.ts。 在界面中使用时 - Typescript error - “type is declared but never used” in .ts. When it was used in interface typescript-使用构造函数时作为接口的类 - typescript - class as interface when constructor is used 如何修复 TypeScript 接口错误? - How to fix error with TypeScript interface? Typescript ESLint 错误 - 使用 React 功能组件时如何输入子项? - Typescript ESLint Error - How can I type children when using react functional components? 对 React 中如何使用接口(TypeScript)感到困惑 - Confusion with how interface (TypeScript) is used in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM