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

Property does not exist on globalThis immediately after declaring it #39504

Open
weswigham opened this issue Jul 8, 2020 · 4 comments
Open
Labels
Suggestion An idea for TypeScript

Comments

@weswigham
Copy link
Member

Code

declare module "buf" {
    global {
        class Buffer {}
    }

    import Buf = globalThis.Buffer;
    export { Buf as Buffer };
}

Expected behavior:
No errors.

Actual behavior:
Property 'Buffer' does not exist on type 'typeof globalThis'.

Playground Link

@ExE-Boss
Copy link
Contributor

ExE-Boss commented Jul 8, 2020

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.

This is also why microsoft/TypeScript-DOM-lib-generator#858 doesn’t quite work the way I intended.

@weswigham
Copy link
Member Author

😢 Hmm, in this case we actually want a varlike global class, though, hrm

@ExE-Boss
Copy link
Contributor

ExE-Boss commented Jul 8, 2020

How about supporting var class <name> in ambient contexts?

declare module "buf" {
	global {
		var class Buffer {}
	}

	import Buf = globalThis.Buffer;
	export { Buf as Buffer };
}

@trusktr
Copy link
Contributor

trusktr commented Apr 11, 2023

To explain this differently for other people, this behavior is because it works like the following in actual browser script tags (assuming that's what the TypeScript global declarations are emulating):

<script>
    const foo = 123;
    var bar = 456;
    let baz = 789;
</script>

<script type="module">
    console.log(foo); // logs "123"
    console.log(globalThis.foo); // logs "undefined"
    console.log(bar); // logs "456"
    console.log(globalThis.bar); // logs "456" (not undefined!)
    console.log(baz); // logs "789"
    console.log(globalThis.baz); // logs "undefined"
</script>

Only the var variables define a property on the global object.

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

No branches or pull requests

3 participants