Repository navigation
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
MartinJohns commentedon Jun 18, 2021
You're missing the
domlibrary.https://www.typescriptlang.org/tsconfig#lib
mindplay-dk commentedon Jun 18, 2021
What? How? All the DOM types are there. 🤷♂️
mindplay-dk commentedon Jun 18, 2021
Martin Johns (@MartinJohns) I tried adding
"lib": ["ES2018", "DOM", "DOM.Iterable"]and it still can't see the constructors.RyanCavanaugh commentedon Jun 18, 2021
This is working fine for me

We'd need some way to repro to investigate further
mindplay-dk commentedon Jun 21, 2021
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
windowanywhere in your parameter list, then thetypeof windowexpression, in any other parameter-type, will resolve to thewindowparameter rather than thewindowvariable.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
$windowetc. - the problem what that, is it leaves thewindowglobal 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
typeoffor 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? 🤔
noneta commentedon Jun 21, 2021
noneta commentedon Jun 21, 2021
mindplay-dk commentedon Jun 21, 2021
Actually, why would my work-around even work?
Maybe the problem isn't what I described at all. 🤨
MartinJohns commentedon Jun 21, 2021
You can do this:
fatcerberus commentedon Jun 21, 2021
What is with these weird spambots lately?
mindplay-dk commentedon Jun 27, 2021
Martin Johns (@MartinJohns) sure, but
typeof globalThis.window.MutationObserveris noisy requires a lot of explaining compared toMutationObserver, the obvious choice. (Eventypeof window.MutationObserveris 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?
mindplay-dk commentedon Jul 1, 2021
I think there are two important questions to be answered here:
Why does the parser attempt to resolve value symbols where a type symbol is expected?
If there is some reason for (1) then why does my work-around with redeclaring the
windowproperties even work?It doesn't seem like these two behaviors can both be correct? 🤔
RyanCavanaugh commentedon Jul 13, 2021
I don't see any defect being demonstrated here.
typeoffollows the same resolution logic as a value would, because that's by far the most straightforward behavior.RyanCavanaugh commentedon Jul 13, 2021
No, because then introducing a new declaration with that name at an intermediate lexical scope could unexpectedly break something.
typescript-bot commentedon Jul 15, 2021
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
mindplay-dk commentedon Jul 16, 2021
I'm not talking about
typeofspecifically, I'm talking about type-hints in general.The
typeofoperator applies to values and extracts types from them, so of course thetypeofoperator 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 symbolA, which is a constructor - and the type symbolA, which is a constructor type. We haveimport typeandtypeofwhich allows us to extract types from class declarations.The point is, while this is valid...
...the following would never be valid for any reason, would it?
The
constdeclaration declares a value symbol only - while theclassdeclaration 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?