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

3.4 globalThis support makes incorrect assumptions about let/const bindings #30477

Closed
shicks opened this issue Mar 18, 2019 · 2 comments · Fixed by #30510
Closed

3.4 globalThis support makes incorrect assumptions about let/const bindings #30477

shicks opened this issue Mar 18, 2019 · 2 comments · Fixed by #30510
Assignees
Labels
Bug A bug in TypeScript High Priority

Comments

@shicks
Copy link
Contributor

shicks commented Mar 18, 2019

TypeScript Version: 3.4.0-dev.20190316

Search Terms:
3.4 globalThis let const property

Code

// Compile with `tsc -t es2015`
const foo: number = 42;
const bar: null = globalThis.foo; // Type 'number' is not assignable to type 'null';

For reference, the following demonstrates how these bindings behave in browser and node, respectively:

<script>
const foo = 42;
alert(window.foo); // --> "undefined"
</script>
const foo = 42;
console.log(global.foo); // --> "undefined"

Expected behavior:
TypeScript models types consistently with what's actually happening in VMs. Specifically, variables bound by let or const are not translated to properties on globalThis.

Actual behavior:
TypeScript converts let/const bindings onto properties of globalThis, but these properties do not exist in ES2015+ output (when let and const are retained in the output).

This is a problem because it enables coding patterns that depend on transpilation to ES5 or lower to work correctly. When the output level is switched to ES2015, these will break at runtime with no compile-time warning.

Playground Link:
N/A: playground does not support 3.4 yet

Related Issues:
None found

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Mar 20, 2019

Verified, and checked with MDN. Not sure how that is going to translate to our var-declared classes in our .d.ts files.

@sandersn
Copy link
Member

The lib.d.ts file that changes the most is es5.d.ts, with Function, String, et al declare const instead of declare var. Bigint from esnext also changes, and that's it.

After some discussion, I vote for shipping as-is and then adding the restriction for 3.5, along with changes to d.ts.

I don't think being incorrectly permissive is going to cause problems given how permissive global used to be, and the risk of changing Function et al from const to var seems scary to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript High Priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants