3.4 globalThis support makes incorrect assumptions about let/const bindings · Issue #30477 · microsoft/TypeScript (original) (raw)

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:

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