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

Question: Accessing modules living in different window object? #1865

Closed
reezom opened this issue Jan 30, 2015 · 3 comments
Closed

Question: Accessing modules living in different window object? #1865

reezom opened this issue Jan 30, 2015 · 3 comments

Comments

@reezom
Copy link

reezom commented Jan 30, 2015

What is a good/proper/recommended way to access modules in a different window object?
For example, take the iframe scenario (assuming same domain) below:

module Container {
    export function magic() { /*...*/ };    
}
module Frame {
    function doContainerMagic() {
        window.parent.Container.magic(); // COMPILER ERROR
    }
}

"Casting to a module" seems not possible. Would #420 (modules implementing interface) provide a possible solution?

@reezom reezom changed the title Question: Accessing modules living in different document object? Question: Accessing modules living in different window object? Jan 30, 2015
@NoelAbrahams
Copy link

@reezom, if your code actually works then you probably only need a type assertion:

module Container {
    export function magic() { /*...*/ };    
}

interface iFrame extends Window {
    Container: typeof Container;
}

module Frame {
    function doContainerMagic() {

        (<iFrame>window.parent).Container.magic();
    }
}

@reezom
Copy link
Author

reezom commented Jan 30, 2015

@NoelAbrahams Thank you, that solves it.
Having learned that modules have a type, I ended up with:

module Container {
    export function magic() { /*...*/ };    
}

module Frame {
    function doContainerMagic() {
        (<typeof Container>window.parent["Container"]).magic();
    }
}

@reezom reezom closed this as completed Jan 30, 2015
@NoelAbrahams
Copy link

Yes, that'll work, but may create problems one day when --noimplicitany is turned on. In fact there's no need for the type assertion, because accessing Container via the bracket notation effectively renders the type to any:

    function doContainerMagic() {
        window.parent["Container"].magic22();
    }

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants