Skip to content

Cannot access the globalThis namespace #42395

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

Closed
juanrgm opened this issue Jan 18, 2021 · 2 comments
Closed

Cannot access the globalThis namespace #42395

juanrgm opened this issue Jan 18, 2021 · 2 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@juanrgm
Copy link

juanrgm commented Jan 18, 2021

Bug Report

πŸ”Ž Search Terms

root namespace, namespace collision

πŸ•— Version & Regression Information

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

⏯ Playground Link

https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEAXAngBwQIXgXngRgFgAoUSWBMgZ0vgGF4BvAX2NOjngDsoBbES1FDAIAgo2LxJ8EAA9UAexiIkaTDngBzCPIBGUCABUAFgEtKAOiwB6K-BPKA7ooDWlCVNkKl8KjXqzEEE5gGi1dfWMzc3pmeBt4AAUYeXQlZHgAcloM+GB5fi55ZVkzZXlOFXRMlHR5ADNNbT1DU0oM82ImIA

πŸ’» Code

declare type B = 1
declare class C {}
declare namespace A {
    export type B = globalThis.B // It works
    export class C extends globalThis.C {} // Property 'C' does not exist on type 'typeof globalThis'.
}

πŸ™ Actual behavior

Property 'C' does not exist on type 'typeof globalThis'.

It is not possible access to the root namespace.

πŸ™‚ Expected behavior

Be able to access to the root namespace.

@whzx5byb
Copy link

related: #39504

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jan 19, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.3.0 milestone Jan 19, 2021
@sandersn sandersn changed the title Can not access to the root namespace Cannot access the globalThis namespace Apr 5, 2021
@sandersn
Copy link
Member

sandersn commented Apr 6, 2021

Thanks for the link @whzx5byb! There, @ExE-Boss points out that this is how JS works -- classes aren't accessible from globalThis.

You can work around for normal values by using a class expression:

var C = class { }

For ambient declarations, you have to create an old-style interface/value merger:

interface CStatic {
    new(): C
}
interface C {
    m1(): number
    a1: number
}
declare var C: CStatic

@sandersn sandersn closed this as completed Apr 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

5 participants