-
Notifications
You must be signed in to change notification settings - Fork 12.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TS assumes navigator does not exist if it doesn't have a "share" property #55341
Comments
Duplicate of #51059. This is by design, for better or worse. |
Thanks for the fast reply. I don't fully comprehend the reasoning behind this behavior, but can appreciate that accounting for these kind of dynamic browser-dependent interfaces might be out of scope for the project. Perhaps the error messaging could be updated to acknowledge this case and describe the workaround? |
There are no individual error messages for specific types, and changing the error message in general for all cases is just unnecessarily muddying the waters for 99.69 % of the cases. |
Agreed - even though it makes me sad. I discovered another workaround - saving the check to a variable and using that makes the error go away: const isNavigatorShareAvailable = "share" in navigator;
if (isNavigatorShareAvailable) {
// share something
} else {
navigator.clipboard // no error, yay
} A bit surprising that the compiler doesn't mark this case as an error as well though. |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
TS2339, Property does not exist on type 'never'
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=4.9.5#code/JYMwBAFARAzgFgQwE4FMpmAOzJhA3YAcwQBcB7JASjAG8AoMRsAembHmRXbIFsUS4WQnQC+YFABsYXekxz4ipCgDoAxhOAAHAEZlkAE1FA
π» Code
π Actual behavior
When checking for device support of the "navigator.share" api, typescript assumes that if the "share" property does not exist on navigator, then navigator must be of type "never".
π Expected behavior
Not all browsers support the navigator.share api, so the share property not being present on navigator, but other properties of navigator still being present is a valid case. See browser support for navigator.share on MDN: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share#browser_compatibility
The text was updated successfully, but these errors were encountered: