How to fix Binding element 'children' implicitly has an 'any' type.ts(7031)?
ReactjsTypescriptReactjs Problem Overview
I'm missing something here with the validation how to add types validation? Having error "element 'children' implicitly has an 'any' type".
import * as React from 'react';
import Button from './Styles';
const Button1 = ({ children, ...props }) => (
<Button {...props}>{children}</Button>
);
Button1.propTypes = {};
export default Button1;
Reactjs Solutions
Solution 1 - Reactjs
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>
);
Solution 2 - Reactjs
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
Solution 3 - Reactjs
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}) => {}
Solution 4 - Reactjs
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>;
}
Solution 5 - Reactjs
You can use types as well
type ButtonProps = {
children: ReactNode;
}
const Button = ({ children }: ButtonProps) => (
<button>{children}</button>
);
Solution 6 - Reactjs
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>
)
Solution 7 - Reactjs
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