I have a component called Visible with two props: condition and children the flow is if the condition is true the component will return the children(s) and if not it will return null (just like condition rendering), but VScode doesn't recognized that what the condition is doing and throw an error for children (the variable may be null)
is there a way to define a type for this component return that VScode recognizes the condition checks the variables?
Visible.ts:
interface IVisible {
condition: any
children: React.ReactNode
}
function Visible(props: IVisible) : React.ReactNode {
const {condition, children} = props
if( !(!!condition) ) return null;
return <Fragment>{children}</Fragment>
}
usage ex:
const [banana, setBanana] = useState<undefined|string[]>(["foo","bar"])
return (
<Visible condition={banana}>
<ul>
{banana.map(e => <li key={e}>{e}</li>) }
</ul>
</Visible>
)
conditionprop isboolean, as it should be. Then you can just think about how to convert that array to a boolean before passing it as a prop.