Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript and React - children type?

I have a very simple functional component as follows:

import * as React from 'react';  export interface AuxProps  {      children: React.ReactNode  }   const aux = (props: AuxProps) => props.children;  export default aux; 

And another component:

import * as React from "react";  export interface LayoutProps  {     children: React.ReactNode }  const layout = (props: LayoutProps) => (     <Aux>         <div>Toolbar, SideDrawer, Backdrop</div>         <main>             {props.children}         </main>     <Aux/> );  export default layout; 

I keep on getting the following error:

[ts] JSX element type 'ReactNode' is not a constructor function for JSX elements. Type 'undefined' is not assignable to type 'ElementClass'. [2605]

How do I type this correctly?

like image 751
Asool Avatar asked Dec 09 '18 02:12

Asool


People also ask

What is ReactNode TypeScript?

The ReactFragment , which is included in the ReactNode type, includes an empty interface. Due to the way that TypeScript handles excess property checks, this means that the ReactNode type will accept any object except an object literal. For almost all intents and purposes, it is functionally equivalent to an any type.

Can TypeScript be used with React?

Using TypeScript with Create React AppCreate React App supports TypeScript out of the box. You can also add it to an existing Create React App project, as documented here.

Is TypeScript and React the same?

TypeScript is a language which extends JavaScript by adding type definitions, much like Flow. While React Native is built in Flow, it supports both TypeScript and Flow by default.


1 Answers

Just children: React.ReactNode.

like image 140
Ridd Avatar answered Oct 03 '22 23:10

Ridd