I have a simple React component like this:
interface ComponentProps {
onClick?: () => void;
}
const Component = (props: ComponentProps) => {
if (!props.onClick) {
return <></>;
}
return (
// Typescript complains: Cannot invoke an object which is possibly 'undefined'.ts(2722)
<button onClick={() => props.onClick()}>
test
</button>
);
};
Typescript complains props.onClick could be undefined, even if after the check.
Could anyone help me to understand why props.onClick can still possibly be undefined?