[英]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.