@@ -577,7 +577,7 @@ function Foo() {
577
577
578
578
If you are sure that ` divRef.current ` will never be null, it is also possible to use the non-null assertion operator ` ! ` :
579
579
580
- ```
580
+ ``` tsx
581
581
const divRef = useRef <HTMLDivElement >(null ! );
582
582
// Later... No need to check if it is null
583
583
doSomethingWith (divRef .current );
@@ -1163,12 +1163,8 @@ Relevant for components that accept other React components as props.
1163
1163
1164
1164
``` tsx
1165
1165
export declare interface AppProps {
1166
- children1: JSX .Element ; // bad, doesnt account for arrays
1167
- children2: JSX .Element | JSX .Element []; // meh, doesn't accept strings
1168
- children3: React .ReactChildren ; // despite the name, not at all an appropriate type; it is a utility
1169
- children4: React .ReactChild []; // better, accepts array children
1170
- children: React .ReactNode ; // best, accepts everything (see edge case below)
1171
- functionChildren: (name : string ) => React .ReactNode ; // recommended function as a child render prop type
1166
+ children? : React .ReactNode ; // best, accepts everything React can render
1167
+ childrenElement: JSX .Element ; // A single React element
1172
1168
style? : React .CSSProperties ; // to pass through style props
1173
1169
onChange? : React .FormEventHandler <HTMLInputElement >; // form events! the generic parameter is the type of event.target
1174
1170
// more info: https://react-typescript-cheatsheet.netlify.app/docs/advanced/patterns_by_usecase/#wrappingmirroring
@@ -1184,7 +1180,7 @@ Before the [React 18 type updates](https://github.com/DefinitelyTyped/Definitely
1184
1180
1185
1181
``` tsx
1186
1182
type Props = {
1187
- children: React .ReactNode ;
1183
+ children? : React .ReactNode ;
1188
1184
};
1189
1185
1190
1186
function Comp({ children }: Props ) {
@@ -1726,7 +1722,10 @@ interface ProviderStore {
1726
1722
1727
1723
const Context = React .createContext ({} as ProviderStore ); // type assertion on empty object
1728
1724
1729
- class Provider extends React .Component <{}, ProviderState > {
1725
+ class Provider extends React .Component <
1726
+ { children? : React .ReactNode },
1727
+ ProviderState
1728
+ > {
1730
1729
public readonly state = {
1731
1730
themeColor: " red" ,
1732
1731
};
@@ -1776,7 +1775,7 @@ class CssThemeProvider extends React.PureComponent<Props> {
1776
1775
` forwardRef ` :
1777
1776
1778
1777
``` tsx
1779
- type Props = { children: React .ReactNode ; type: " submit" | " button" };
1778
+ type Props = { children? : React .ReactNode ; type: " submit" | " button" };
1780
1779
export type Ref = HTMLButtonElement ;
1781
1780
export const FancyButton = React .forwardRef <Ref , Props >((props , ref ) => (
1782
1781
<button ref = { ref } className = " MyClassName" type = { props .type } >
@@ -1791,7 +1790,7 @@ export const FancyButton = React.forwardRef<Ref, Props>((props, ref) => (
1791
1790
This was done [ on purpose] ( https://github.com/DefinitelyTyped/DefinitelyTyped/pull/43265/ ) . You can make it immutable if you have to - assign ` React.Ref ` if you want to ensure nobody reassigns it:
1792
1791
1793
1792
``` tsx
1794
- type Props = { children: React .ReactNode ; type: " submit" | " button" };
1793
+ type Props = { children? : React .ReactNode ; type: " submit" | " button" };
1795
1794
export type Ref = HTMLButtonElement ;
1796
1795
export const FancyButton = React .forwardRef (
1797
1796
(
@@ -1891,7 +1890,7 @@ Using `ReactDOM.createPortal`:
1891
1890
const modalRoot = document .getElementById (" modal-root" ) as HTMLElement ;
1892
1891
// assuming in your html file has a div with id 'modal-root';
1893
1892
1894
- export class Modal extends React .Component {
1893
+ export class Modal extends React .Component <{ children ? : React . ReactNode }> {
1895
1894
el: HTMLElement = document .createElement (" div" );
1896
1895
1897
1896
componentDidMount() {
@@ -1921,7 +1920,7 @@ import { createPortal } from "react-dom";
1921
1920
1922
1921
const modalRoot = document .querySelector (" #modal-root" ) as HTMLElement ;
1923
1922
1924
- const Modal: React .FC <{}> = ({ children }) => {
1923
+ const Modal: React .FC <{ children ? : React . ReactNode }> = ({ children }) => {
1925
1924
const el = useRef (document .createElement (" div" ));
1926
1925
1927
1926
useEffect (() => {
@@ -2008,7 +2007,7 @@ If you don't want to add a new npm package for this, you can also write your own
2008
2007
import React , { Component , ErrorInfo , ReactNode } from " react" ;
2009
2008
2010
2009
interface Props {
2011
- children: ReactNode;
2010
+ children? : ReactNode;
2012
2011
}
2013
2012
2014
2013
interface State {
0 commit comments