Skip to content

window.ontouchstart should be optional #33176

@Joncom

Description

@Joncom

TypeScript Version: Version 3.5.1

Code

touchDevice = (('ontouchstart' in window) || (window.navigator.msMaxTouchPoints > 0));

Expected behavior:

No error

Actual behavior:

capture

Produces the error: Property 'navigator' does not exist on type 'never'.

Playground Link: http://www.typescriptlang.org/play/#code/G4QwTgBALg9grgYwBYBECmwCWC0QLwQAUhA5DAHayJIDOU4UJEm5EA7iwCYxsCUEAHwFEO5bmwB05EFgDmIWGAkBbGgFkQADwAq8ZAAUYLKDQgA+CAAZevANxA

More

Apparently this issue happens due to something called "type narrowing", the idea being that TypeScript is warning us that the code after the || is unreachable. But that's simply not true. Modern browsers without touch support may have an undefined window.ontouchstart. So it appears that TypeScript is treating ontouchstart as a required property, when in fact it should be optional.

Screen Shot 2019-08-31 at 9 33 02 PM

Screen Shot 2019-08-31 at 9 33 22 PM

Screen Shot 2019-08-31 at 9 33 31 PM

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptExperience EnhancementNoncontroversial enhancements

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions