Skip to content
Advertisement

TypeScript : What is the right way to define types

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>
  );

Advertisement

Answer

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) => (
  //...
);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement