Home > Software engineering >  TypeScript : What is the right way to define types
TypeScript : What is the right way to define types

Time:06-21

I'm a little confused with the assignment of typescript types. In my example I want to know how I define the type of props that will not be "any type" and also how I define the type of the title, handleOnPress, svg parameters of the SideMenuItem function

const SideMenu = (props: any) => {
  const SideMenuItem = ({ title, handleOnPress, svg }) => (
    <TouchableOpacity style={styles.sideMenuItem} onPress={handleOnPress}>
      <View style={styles.syncView}>
        {svg}
        <Text style={styles.sideMenuItemText}>{title}</Text>
      </View>
    </TouchableOpacity>
  );

CodePudding user response:

Define the prop types for your component in an interface

interface SideMenuItemProps {
  title: string;
  handleOnPress: () => void;
  svg: ReactNode;
}

Then define the prop type in the component definition

const SideMenuItem = ({ title, handleOnPress, svg }: SideMenuItemProps) => (
  //...
);
  • Related