I think I'm missing something basic on how you can check for a truthy value with && in Typescript.
Why can props.function still be undefined in test1 and has to be checked like I did in test2.
import React from "react";
interface PropsModel {
label?: string
function?: Function,
}
function test1(props: PropsModel) {
return (
<div>
{props.label && <p>{props.label}</p>} // props.label is checked for undefined
{props.function && <button onClick={() => props.function()}>Dummy button</button>} // props.function is not checked for undefined
</div>
)
}
function test2(props: PropsModel) {
return (
<div>
{props.label && <p>{props.label}</p>} // props.label is checked for undefined
{props.function && <button onClick={() => {if (props.function) props.function()}}>Dummy button</button>} // props.function is checked for undefined
</div>
)
}
EDIT 1: Added Link to playground
Link to Typescript playground