Home>

I write react in typescript
Parameter 'props' implicitly has an 'any' type. TS7006
Will result in an error.

Title.tsx

import React from 'react';
const Title = (props) =>{
  return (
    
         {props.title} 
    
  );
}
export default Title;

Tell me if you know someone

  • Answer # 1

    Since

    Props type is not specified,props.titlevalue may not exist, resulting in an error.
    I think that it is necessary to specify the type of Props as follows.

    import React from 'react';
    type Props = {
      title: stirng
    }
    const Title = (props: Props) =>{
      return (
        
             {props.title} 
        
      );
    }
    export default Title;

    I'm sorry, I have been chosen as the best answer, but I'll add the following.
    I think that the error can be solved by the above, but if you are using TypeScript, you can write more safely.
    I haven't written aTitletype now, but this is a function component, so it has the typeReact.FC.
    Since this type can specify the type of props by generics,

    const Title: React.FC= (props) =>{


    I think that it is more beautiful to write