0

I have this component using AvatarProps as type for the props :

enter image description here

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 :

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

eslint is triggered on commit with lefthook from a lefthook.yml file like so :

 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?

JSharles
  • 565
  • 1
  • 5
  • 16
  • Any previous research ? – l -_- l Jul 07 '22 at 13:33
  • Tried yarn lint --fix as recommended but it does not fix it. Found a post advising to create an .eslintrc file. There is this post https://stackoverflow.com/questions/57802057/eslint-configuring-no-unused-vars-for-typescript but I think it's not applicable in my context since I use npx from lefthook. – JSharles Jul 07 '22 at 13:42
  • Can you show how did you declared the type? – Konrad Jul 07 '22 at 15:31
  • @KonradLinkowski I just edited the original post. – JSharles Jul 07 '22 at 15:35
  • You are missing some plugins in eslint config. Can you show it? – Konrad Jul 07 '22 at 15:38
  • I'm actually using this library https://github.com/callstack/react-native-builder-bob/blob/main/README.md in which ESLint is pre-configured. Not sure where to configure eslint. – JSharles Jul 07 '22 at 16:20
  • The project has a devDependencies though : "eslint-config-prettier": "^8.5.0", – JSharles Jul 07 '22 at 16:21

1 Answers1

0

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. 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.

UPDATE: As mentioned in the comments React.FC isn't needed actually you can implement your component without using it like this:

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)

  • I have never seen syntax like this. I'm pretty sure `React.Fc` is deprecated at this point. – Konrad Jul 07 '22 at 15:30
  • 1
    it's not deprecated as you. can see here https://github.com/DefinitelyTyped/DefinitelyTyped/blob/570f4d59b5414ffa4ca99ad67f9bee19d4cd3d89/types/react/index.d.ts#L517. This is only not needed. if you don't want to render a `children` in your component but I will update my answer to clrarify this – Marlon Alejandro Espinosa Cast Jul 07 '22 at 16:08