Skip to content

Missing window properties #44647

Closed
Closed

Description

Bug Report

🔎 Search Terms

missing window constructor properties

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about missing types

⏯ Playground Link

The playground does not exhibit this issue - it occurs in my local project only.

I have tried using the same version 4.3.2 as the playground.

💻 Code

// We can quickly address your report if:
//  - The code sample is short. Nearly all TypeScript bugs can be demonstrated in 20-30 lines of code!
//  - It doesn't use external libraries. These are often issues with the type definitions rather than TypeScript bugs.
//  - The incorrectness of the behavior is readily apparent from reading the sample.
// Reports are slower to investigate if:
//  - We have to pare too much extraneous code.
//  - We have to clone a large repo and validate that the problem isn't elsewhere.
//  - The sample is confusing or doesn't clearly demonstrate what's wrong.

I use functions with dependency injection for window globals - parameters such as MutationObserver: typeof MutationObserver do not work, so I use e.g. MutationObserver: typeof window.MutationObserver instead, since qualifying the reference with window avoids the ambiguity with the MutationObserver parameter.

function bootstrap(
  MutationObserver: typeof window.MutationObserver,
  CustomEvent: typeof window.CustomEvent
) {
  // ...
}

This is fine in the playground - but in VS Code, I get an error:

Property 'CustomEvent' does not exist on type 'Window'. ts(2339)

This could be configuration related, but I haven't been able to find a configuration on the playground to recreate the issue.

My tsconfig.json is extremely simple:

{
  "compilerOptions": {
    "strict": true,
    "rootDir": "src",
    "target": "ES2018",
    "moduleResolution": "node"
  }
}

I've tried changing target and moduleResolution to different values, but still get the error.

It's not that the types aren't there - I can type-hint against MutationObserver type itself - for example, MutationObserver: MutationObserver works fine (but is not what I want; dependency is on the constructor) while MutationObserver: typeof MutationObserver gives a different error:

'MutationObserver' is referenced directly or indirectly in its own type annotation. ts(2502)

I could solve this by renaming MutationObserver, of course - but it is expected to be precisely the MutationObserver constructor, it's only there for the sake of unit-testing, so I don't want to make up a different name, nor do I want to risk somebody accessing the global MutationObserver from within the body of the function in question. Hiding the real MutationObserver within that scope is the intended behavior.

When I look at the declaration sources, I noticed the global constructors are declared using declare var rather than actual properties on the Window type - I don't know if that makes a difference?

I'm working around the problem now by adding a globals.d.ts to the root of my project and manually declaring the constructors:

interface Window {
  CustomEvent: typeof CustomEvent;
  MutationObserver: typeof MutationObserver;
}

This works - again, meaning the types are there, the window properties just aren't.

And I see the declare var declarations in the loaded declaration sources, but they seem to have no effect.

🙁 Actual behavior

The window.MutationObserver constructor property does not seem to exist - even though the MutationObserver type is present, and the declarations appear in the declaration source-code.

🙂 Expected behavior

window.MutationObserver should resolve, same as it does on the playground.

Activity

  1. MartinJohns commented on Jun 18, 2021

    @MartinJohns
    Contributor

    My tsconfig.json is extremely simple:

    You're missing the dom library.

    https://www.typescriptlang.org/tsconfig#lib

  2. mindplay-dk commented on Jun 18, 2021

    @mindplay-dk
    Author

    What? How? All the DOM types are there. 🤷‍♂️

  3. mindplay-dk commented on Jun 18, 2021

    @mindplay-dk
    Author

    Martin Johns (@MartinJohns) I tried adding "lib": ["ES2018", "DOM", "DOM.Iterable"] and it still can't see the constructors.

  4. RyanCavanaugh commented on Jun 18, 2021

    @RyanCavanaugh
    Member

    This is working fine for me
    image

    We'd need some way to repro to investigate further

  5. added
    Needs More Info
    on Jun 18, 2021
  6. mindplay-dk commented on Jun 21, 2021

    @mindplay-dk
    Author

    Well, this was tough to isolate, but I got it: playground

    Try commenting-out the first parameter window: Window.

    So I think I see the problem now: if you have a parameter named window anywhere in your parameter list, then the typeof window expression, in any other parameter-type, will resolve to the window parameter rather than the window variable.

    This has actually bitten me a number of times before in different ways - it's quite confusing, and I never realized until just now what's going on. As explained, I use dependency injection for all the environment globals in this project, so I can mock and unit-test everything.

    I could disambiguate by renaming all parameters to $window etc. - the problem what that, is it leaves the window global accessible from the scope of those functions, which it really shouldn't be. I'm hiding it deliberately. Because, if it's left accessible, someone will accidentally use it - and what's worse, it'll actually work if they run it in the browser.

    What I'm wondering actually, is why am I able to reference other parameters in a type-expression in the first place? Is this by design maybe just the way it works out?

    I mean, it's possible you could do something useful with the type of another parameter - but shouldn't you be using generics for that? Is there anything you could do with typeof for a parameter, in the declaration of another parameter, that you can't do (in a clearer, more conventional way) with generics?

    I understand the need to be able to reference a parameter's value in the declaration of another parameter as, say, a default value. But does this have to affect typing? Should it? 🤔

  7. noneta commented on Jun 21, 2021

    @noneta
  8. noneta commented on Jun 21, 2021

    @noneta
  9. mindplay-dk commented on Jun 21, 2021

    @mindplay-dk
    Author

    Actually, why would my work-around even work?

    I'm working around the problem now by adding a globals.d.ts to the root of my project and manually declaring the constructors

    Maybe the problem isn't what I described at all. 🤨

  10. MartinJohns commented on Jun 21, 2021

    @MartinJohns
    Contributor

    You can do this:

    function bootstrap(
      window: Window,
      MutationObserver: typeof globalThis.window.MutationObserver,
      CustomEvent: typeof globalThis.window.CustomEvent
    ) {
      // ...
    }
    
  11. fatcerberus commented on Jun 21, 2021

    @fatcerberus

    What is with these weird spambots lately?

  12. mindplay-dk commented on Jun 27, 2021

    @mindplay-dk
    Author

    Martin Johns (@MartinJohns) sure, but typeof globalThis.window.MutationObserver is noisy requires a lot of explaining compared to MutationObserver, the obvious choice. (Even typeof window.MutationObserver is inelegant - where that's possible.)

    The parser ought to be expecting a type where a type-hint should be - why is it trying to resolve to a variable in the first place?

    In the case of ambiguity: if a type-hint can be resolved to a type, shouldn't that be the parser's first choice?

  13. mindplay-dk commented on Jul 1, 2021

    @mindplay-dk
    Author

    I think there are two important questions to be answered here:

    1. Why does the parser attempt to resolve value symbols where a type symbol is expected?

    2. If there is some reason for (1) then why does my work-around with redeclaring the window properties even work?

    It doesn't seem like these two behaviors can both be correct? 🤔

  14. added
    Working as Intended
    and removed
    Needs More Info
    on Jul 7, 2021
  15. RyanCavanaugh commented on Jul 13, 2021

    @RyanCavanaugh
    Member

    I don't see any defect being demonstrated here. typeof follows the same resolution logic as a value would, because that's by far the most straightforward behavior.

  16. RyanCavanaugh commented on Jul 13, 2021

    @RyanCavanaugh
    Member

    In the case of ambiguity: if a type-hint can be resolved to a type, shouldn't that be the parser's first choice?

    No, because then introducing a new declaration with that name at an intermediate lexical scope could unexpectedly break something.

  17. typescript-bot commented on Jul 15, 2021

    @typescript-bot
    Contributor

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

  18. mindplay-dk commented on Jul 16, 2021

    @mindplay-dk
    Author

    I don't see any defect being demonstrated here. typeof follows the same resolution logic as a value would, because that's by far the most straightforward behavior.

    I'm not talking about typeof specifically, I'm talking about type-hints in general.

    The typeof operator applies to values and extracts types from them, so of course the typeof operator must expect a value symbol as operand.

    But the type-hint itself, when resolved, why would it consider value symbols in the first place?

    I mean, sure, there's classes, but those are sort of two declarations in one. If you have class A, then there are two symbols, right? The value symbol A, which is a constructor - and the type symbol A, which is a constructor type. We have import type and typeof which allows us to extract types from class declarations.

    The point is, while this is valid...

    class Foo {}
    
    function bar(foo: Foo) {}

    ...the following would never be valid for any reason, would it?

    const Baz = 123;
    
    function bar(foo: Baz) {}

    The const declaration declares a value symbol only - while the class declaration also declares a type symbol.

    What we're interested in when resolving a type-hint must be type symbols, right?

    There's no use-case for value symbols where a type symbol is expected, is there?

    So why would a value be able to hide a type?

  19. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as Intended

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions