TypeScript 7031 on a destructured children prop in React
Binding element 'children' implicitly has an 'any' type.ts(7031)Short answer
With noImplicitAny on, TypeScript refuses to guess a type it cannot infer. A component written as ({ children }) has no type for its props object, so every destructured name becomes an implicit any. Give the props an interface that declares children as React.ReactNode, and keep strict on.
Fix it fast
1Find out which cause you have
Check whether strict or noImplicitAny is on. If it is, the error is working as designed and the fix is a type on the props, not a config change.
npx tsc --showConfig | grep -E 'strict|noImplicitAny'2Match what you see, then jump to its fix
- 1
The line is function Layout({ children }) { ... } with nothing after the closing braceThe props object has no type annotationFix - 2
The component is built inside a helper, higher-order function or callback, for example withAuth(({ children }) => ...)A wrapper function gives the component no contextual typeFix
- 1
What does the error look like?
Binding element 'children' implicitly has an 'any' type.ts(7031)
Cause 1 of 2
The props object has no type annotation
1Check
TypeScript falls back to any when it cannot infer a type and there is no annotation. With noImplicitAny enabled, it reports an error at that point.
grep -rn '({ *children' src/2Fix
Declare the props and type children as React.ReactNode, which accepts anything React can render.
interface LayoutProps {
title: string;
children: React.ReactNode;
}
function Layout({ title, children }: LayoutProps) {
return (
<section>
<h2>{title}</h2>
{children}
</section>
);
}3Verify
Run the type check. Error 7031 is gone and a wrong prop type is now reported.
npx tsc --noEmitConfirmed by react.dev
Cause 2 of 2
A wrapper function gives the component no contextual type
1Check
In that spot TypeScript has nothing to infer from, so the parameter is untyped. The same rule applies: no inferred type and no annotation means implicit any.
grep -rn 'withAuth\|=> *{$' src/ | head2Fix
Annotate the parameter at the place it is written, with the same props type.
type Props = { children: React.ReactNode };
const Guarded = ({ children }: Props) => <main>{children}</main>;3Verify
Run the type check again.
npx tsc --noEmitConfirmed by typescriptlang.org
How do I stop it coming back?
- Keep strict on in tsconfig.json and type props at the component, not later.
- Reuse one shared props type for layout components that only take children.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
ESLint reports no-unused-vars for React components and imports that are used
no-unused-vars reported for a component that is rendered as a JSX tag
ESLint's core no-unused-vars does not see variables used only as JSX tags, so a component imported and rendered as <Hello /> looks unused.
3 causes, each with a fixWeb apps
TypeScript rejects MUI Data Grid columns that set a type
A TypeScript error inside a column's renderCell callback
A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.
2 causes, each with a fixWeb apps
The renderInput prop stopped working on the MUI X DatePicker
The custom TextField (variant, size, helperText) is ignored
renderInput was removed from the MUI X date pickers in version 6.
3 causes, each with a fix