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

window.PointerEvent is undefined #18756

Closed
appsforartists opened this issue Sep 25, 2017 · 11 comments
Closed

window.PointerEvent is undefined #18756

appsforartists opened this issue Sep 25, 2017 · 11 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@appsforartists
Copy link

TypeScript Version: 2.4.1

Code

if (window.PointerEvent) {

Expected behavior:
This should work. It is a valid feature detection test.

Actual behavior:

error TS2339: Property 'PointerEvent' does not exist on type 'Window'.

Notes
It appears that PointerEvent is defined on dom.generated.d.ts with declare var PointerEvent. Should Window be augmented with that type, or is there a more idiomatic way to check for the presence of a global definition in TypeScript?

@appsforartists
Copy link
Author

typeof PointerEvent appears to be a working test. Still, testing if window has a property is a common way to do feature detection. TypeScript should be more forgiving here.

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Sep 25, 2017
@mhegazy mhegazy added this to the Community milestone Sep 26, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Sep 26, 2017

Global declarations are not all mirrored on Window. we recommend you add it in your project, e.g.:

interface Window {
    PointerEvent : typeof PointerEvent ;
}

@mhegazy mhegazy removed this from the Community milestone Sep 26, 2017
@mhegazy mhegazy added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Help Wanted You can do this Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Sep 26, 2017
@appsforartists
Copy link
Author

@mhegazy I had tried that before opening this issue, and it didn't work for me either.

Still, checking for a property on window is a very common way to do feature detection. It should be handled correctly by TypeScript. For instance, Google, Microsoft, and the W3C all recommend if (window.PointerEvent):

@mhegazy
Copy link
Contributor

mhegazy commented Sep 26, 2017

looking at other issues seems we have been inconsistent here. some we did add and some we did not.. here are some:
#8866
#14402
#9325
#3753
#11305

it is possible we keep adding one off, or we need to have a more general solution. #14052 seems like a prerequisite to correctly model global polluter in JS

@aluanhaddad
Copy link
Contributor

@appsforartists

I had tried that before opening this issue, and it didn't work for me either.

That absolutely should work. I've done it numerous times and it has always worked. are you sure the file containing the declaration is part of your compilation context?

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Sep 26, 2017

It's not working there because you're trying to augment Window in a module (something with imports/exports). Try adding it to a global-types.d.ts

(p.s. yes it's subtle, I'm sorry about that 😕)

@appsforartists
Copy link
Author

I think I'm just going to use typeof PointerEvent to avoid having to mess with globals.

If I was trying to use window.PointerEvent, would global-types.d.ts be a file I create locally, or one that's on the system somewhere?

This code is going into a library, so global side effects are probably not great, but neither are type errors.

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Sep 26, 2017

If you are writing a module then you need to use

declare global  {
  interface Window {
    PointerEvent: typeof PointerEvent;
  }
}

which augments the type of the global window.

If you are writing a module and you do not write the declare global wrapper then you are declaring a locally scoped Window type that shadows the global Window interface in the type space, rather than merging with it.

@appsforartists
Copy link
Author

Closing as a duplicate of #14052.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Sep 26, 2017

I'd recommend declaring it exactly as if the generator script emits it so that you don't get duplicate declaration errors down the line.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants