microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.44k stars 12.42k forks source link

typing for navigator.clipboard #59057

Closed Manish3177 closed 3 months ago

Manish3177 commented 3 months ago

⚙ Compilation target

ES5

⚙ Library

None

Missing / Incorrect Definition

lib.dom.ts defines interface Navigator with:

`/**

This allows writing code like navigator.clipboard.readText() which crashes in unsecure environments (over HTTP).

Fix suggestion: Change the return type in typing to Clipboard | null to force a null check in consuming code. This is kind of a breaking change but that's the point.

Sample Code

navigator.clipboard.readText() ;will crash over HTTP

Documentation Link

No response

fatcerberus commented 3 months ago

This is intentional. There are a lot of calls like this that only work in a secure context. If you know your code is always going to run over HTTPS, it would be incredibly annoying and counterproductive if the compiler forced you do either do a null check or add a non-null assertion to every such call.

fatcerberus commented 3 months ago

See also https://github.com/microsoft/TypeScript/issues/52044 which was marked WAI.

Manish3177 commented 3 months ago

Thanks for those links @fatcerberus but in my humble opinion, the "it works in secure context" argument is not much better than a "but it works on my machine" argument. Those that don't care about running in contexts where the property can be null are free to use optional chaining or non-null assertion (if they want to assert they really don't care). A single character isn't counter-productive or something that causes clutter. A false assurance on the other hand is bad for people who care.

fatcerberus commented 3 months ago

“but it works on my machine”

This is a bad analogy because HTTP/HTTPS is a server-side concern and not something that would be affected by end-user configuration.

Anyway I’m just telling you the position of the TS maintainers on this - it’s not considered a bug for the reasons stated before and that’s not likely to change.

typescript-bot commented 3 months ago

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