Skip to content

window.parent should have the same global variables as window #41684

@lukejagodzinski

Description

@lukejagodzinski

TypeScript Version: 4.1.2

Search Terms: window.parent, window.Element, globalThis

Code

window.HTMLDivElement;
window.parent.HTMLDivElement; // Error here

Expected behavior:

The window.parent object should have the same base global variables as the window object. If not in the context of iframe, then window === window.parent so all global variables should be the same in such a case. When being in the iframe context window !== window.parent however it should still have all the base global variables, so that we can do for example element instanceof window.parent.Element. You could think, why don't just write element instanceof Element? The answer is that it would work in almost all browsers beside Chrome. And Chrome is actually the one implementing it correctly. Both global window and iframe window have their own global context so window.parent.Element !== window.Element. In TS all the global variables are defined in the globalThis so the type for the window.parent should be declare var parent: Window & typeof globalThis;

Actual behavior:

The window.parent.HTMLDivElement; does not exist on type Window.

Playground Link: https://www.typescriptlang.org/play?#code/O4SwdgJg9sB0ASAVAsgGQCIgG4FEA2ApgLYFgAuA3AFCiQywAOAhgE6lkIobb7HsVA

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions