Skip to content
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

Closed
jpcastberg opened this issue Aug 12, 2023 · 5 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@jpcastberg
Copy link

jpcastberg commented Aug 12, 2023

πŸ”Ž Search Terms

TS2339, Property does not exist on type 'never'

πŸ•— Version & Regression Information

  • This changed between versions 4.84 and 4.95

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.9.5#code/JYMwBAFARAzgFgQwE4FMpmAOzJhA3YAcwQBcB7JASjAG8AoMRsAembHmRXbIFsUS4WQnQC+YFABsYXekxz4ipCgDoAxhOAAHAEZlkAE1FA

πŸ’» Code

if ("share" in navigator) {
    // share something
} else {
    navigator.clipboard // TS throws "error TS2339: Property 'clipboard' does not exist on type 'never'"
}

πŸ™ 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

@fatcerberus
Copy link

fatcerberus commented Aug 12, 2023

Duplicate of #51059. This is by design, for better or worse.
Workaround: #51059 (comment)

@jpcastberg
Copy link
Author

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?

@MartinJohns
Copy link
Contributor

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.

@jpcastberg
Copy link
Author

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.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 14, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants