0

I have a react functional component where I store a state value to define if the Web Share API is available in the users browser. I do that by initializing the state with:

const [canShare, setCanShare] = useState<Boolean>(navigator.share ? true : false)

This works as expected, however I'm getting the following typescript error:

This condition will always return true since this function is always defined. Did you mean to call it instead?

What I don't fully understand is why the typescript error thinks that function is always defined? In browsers that don't support the Web Share API (i.e. Chrome on MacOS) that function is undefined.

I know I can safely ignore this warning, but I'm curious if there is a way to not receive this error?

The only work around I came up with was to call navigator.share() in a try-catch block and set the bool from that, but that seems really excessive.

0 Answers0