This is a small example
function Loading({
data,
children,
}: PropsWithChildren<{ data: number | undefined }>) {
if (!data) return <></>;
return children;
}
function My({ data }: { data: number }) {
return <div>{data}</div>;
}
function Home() {
const data: number | undefined = Math.random() > 0.5 ? 1 : undefined;
return (
<Loading data={data}>
<My data={data}></My>
</Loading>
);
}
In this Loading component is taking data and based on it's value, it's rendering the children
Logically, I know that data will never be undefined inside Loading Node, but typescript still considers it as undefined for My Component.
How can I assert that data is not undefined without doing data! or data && <My data={data}/>
can it be done without passig data as well?