score:111

Accepted answer

edit 2022: with react 18, fc no longer provides children, so you have to type it yourself, and you can drop fc:

import react, { reactnode } from "react";

interface props {
    children?: reactnode
    // any props that come into the component
}

const button1 = ({ children, ...props }: props) => (
    <button {...props}>{children}</button>
);

yes you are missing a type for props as whole, which means typescript sees it as any and your ts rules dont allow it.

you have to type your props as:

import react, { fc } from "react";

interface props {
    // any props that come into the component
}

const button1: fc<props> = ({ children, ...props }) => (
    <button {...props}>{children}</button>
);

score:1

this error can be fixed by explicitly defining type for the variable (children in this case) and not leaving it to be implicitly inferred

error can be stopped altogether with typescript --noimplicitany compiler option

score:6

you can use types as well

type buttonprops = {
    children: reactnode;
    
}

const button = ({ children }: buttonprops) => (
    <button>{children}</button>
);

score:6

i find it best to have the component props interface extend from react.htmlattributes because it gives you the standard html attributes without any extra configuration:

interface button1props extends react.htmlattributes<element> {
  // add any custom props, but don't have to specify `children`
}

const button1 = ({ children, ...props }: button1props) => (
    <button {...props}>{children}</button>
)

if you want to enforce children to be provided, you can make it required by redefining it in the props interface:

interface button1props extends react.htmlattributes<element> {
  children: react.reactnode
  // add any custom props, but don't have to specify `children`
}

const button1 = ({ children, ...props }: button1props) => (
    <button {...props}>{children}</button>
)

score:12

as another approach, you can use the built-in generic type "react.propswithchildren" for children in props, taking those props accordingly. a very short code would look like this:

import react from "react";
import button from "./styles";

type mycomponentprops = react.propswithchildren<{}>;

export default function mycomponent({ children, ...other}: mycomponentprops) {
  return <button {...other}>{children}</button>;
}

score:22

this was a major issue for me and i wasted a lot of time figuring out the correct solution. right now you have an error with the children prop but in the future, you might have this error for a lot of functions where you are destructuring the params. so i would suggest, follow this github issue.

const yourfunc = ({destructuredprops}: {destructuredprops: type}) => {}

score:24

you can also add the predefined type to your functional components like this:

const button1: react.fc<{}>  = ({ children }) => (
    <button>{children}</button>
);

by this way you don't have to repeat yourself to define children props.

the fuller version could be like this:

interface props {
// any other props that come into the component, you don't have to explicitly define children.
}

const button: react.fc<props> = ({ children, ...props }) => {
  return (
      <button {...props}>{children}</button>
  );
};

please note it works for react 16.8


Related Query

More Query from same tag