#Checking the props type of the children in React JSX

23 messages · Page 1 of 1 (latest)

remote vector
#
tropic glacierBOT
#
Basix#2873

Preview:```ts
import React from "react"

interface Props {
children?:
| React.ReactElement<{requiredProp: number}>
| React.ReactElement<{requiredProp: number}>[]
requiredProp?: number
}

function C(props: Props) {
return <>{props.children}</>
}

function D(
...```

candid kraken
#

@remote vector ?: makes a property optional

#

which is why it's not an error

remote vector
#

removing it has no effect

tropic glacierBOT
#
Basix#2873

Preview:```ts
import React from "react"

interface Props {
children?:
| React.ReactElement<
{requiredProp: number},
typeof C
>
| React.ReactElement<
{requiredProp: number},
typeof C
>[]
requiredProp: number
}

function C(props: Props) {
return <>{props.children}</>
...```

remote vector
#

it feels like <D /> inside C's children should throw error but it doesnt

candid kraken
# tropic glacier

it's not an error because you've added the requiredProp to the component...

#

oh children

remote vector
#

yeah that part

candid kraken
strange kite
#

@remote vector You really can't check the types of children in JSX, they aren't really modeled in the type-system.

candid kraken
#

:(

remote vector
#

😦

strange kite
#

When you do <D /> the type is JSX.Element, when you do <D someProp={"foo"} /> the type is still JSX.Element.

remote vector
#

Doesn't JSX.Element's type include the props?

strange kite
#

No, JSX.Element isn't a generic type.

#

It's essentially an alias of React.ReactElement which is generic, and can track props, but JSX.Element itself does not, and that's what you get from JSX syntax.

remote vector
#

That's very unfortunate, thanks for the answer

#

Is there any issue tracking this problem?

strange kite
#

Would be very surprised if there isn't, but I don't know it off-hand.