Skip to content

"declare class" in a global is not visible in external files #58056

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
jayshah123 opened this issue Apr 3, 2024 · 7 comments
Closed

"declare class" in a global is not visible in external files #58056

jayshah123 opened this issue Apr 3, 2024 · 7 comments

Comments

@jayshah123
Copy link

🔎 Search Terms

"declare class" in a global is not visible in external files,
extending typescript global object with "declare class",
declared class in global not found,
typescript globals.d.ts declared global class not found

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about globals and declaration files here

⏯ Playground Link

https://github.com/jayshah123/typescript-declare-global-class-issue

💻 Code

// global.d.ts
export { };

declare global {
    var myGlobalVariable: string;
    class WorldClass {
        constructor();
        important: number;
    }
}
// index.ts
const w = new global.WorldClass(); // Property 'WorldClass' does not exist on type 'typeof globalThis'.ts(2339)
\
const h = global.myGlobalVariable;

console.log(h);

🙁 Actual behavior

Class declared in global not allowed
Screenshot 2024-04-03 at 6 47 07 PM

🙂 Expected behavior

Class declared in global should be allowed.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Your "Playground" link does not work and leads to a 404.

@jayshah123
Copy link
Author

jayshah123 commented Apr 3, 2024

Your "Playground" link does not work and leads to a 404.

It's a github link as I need more than one file: https://github.com/jayshah123/typescript-declare-global-class-issue

@MartinJohns
Copy link
Contributor

And I'm telling you that your link doesn't work, it leads to a 404 page. Perhaps you have set it to private.

@whzx5byb
Copy link

whzx5byb commented Apr 3, 2024

See #39504 (comment)

That’s because class, let and const don’t create own properties on the global object, instead they create variable bindings on a Declarative Environment Record, so this is the expected behaviour.

If you still want to do this, try this workaround.

declare global {
    namespace globalThis {
        class WorldClass {
            constructor();
            important: number;
        }
        export { WorldClass }
    }
}

const w = new global.WorldClass(); // OK

@jayshah123
Copy link
Author

And I'm telling you that your link doesn't work, it leads to a 404 page. Perhaps you have set it to private.

My bad, made it public

@jayshah123
Copy link
Author

jayshah123 commented Apr 3, 2024

Is this a common pattern for globals class declaration - declare interface + declare var,

/**
 * The URL interface represents an object providing static methods used for creating object URLs.
 *
 * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL)
 */
interface URL {
    hash: string;
    host: string;
    hostname: string;
    href: string;
    toString(): string;
    readonly origin: string;
    password: string;
    pathname: string;
    port: string;
    protocol: string;
    search: string;
    readonly searchParams: URLSearchParams;
    username: string;
    toJSON(): string;
}

declare var URL: {
    prototype: URL;
    new(url: string | URL, base?: string | URL): URL;
    canParse(url: string | URL, base?: string): boolean;
    createObjectURL(obj: Blob | MediaSource): string;
    revokeObjectURL(url: string): void;
};

if the behavior mentioned in the issue description is an expcted one,
should we add this to FAQ?

@fatcerberus
Copy link

The behavior mentioned matches JS runtime behavior:

<script>
class Foo {}
console.log(Foo);  // [class Foo]
console.log(globalThis.Foo);  // undefined
</script>

I don't think it needs to be documented separately if the types match the runtime reality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants