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

Object variable declaration conflicts on commonjs #35478

Open
falsandtru opened this issue Dec 3, 2019 · 7 comments
Open

Object variable declaration conflicts on commonjs #35478

falsandtru opened this issue Dec 3, 2019 · 7 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@falsandtru
Copy link
Contributor

TypeScript Version: 3.7.x-dev.20191203

Search Terms:

Code

export default {}
const Object = {};

Expected behavior:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
{
exports.default = {};
const Object = {};
}

no error

Actual behavior:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = {};
const Object = {};
VM69:2 Uncaught ReferenceError: Cannot access 'Object' before initialization
    at eval (eval at <anonymous> (main-3.js:1239), <anonymous>:2:1)
    at main-3.js:1239

Playground Link: http://www.typescriptlang.org/play/index.html?module=1#code/KYDwDg9gTgLgBAE2AMwIYFcA28DeBfAKAGMIA7AZ3gHkAjAK2CPgF458BuIA

Related Issues:

@weswigham
Copy link
Member

weswigham commented Dec 3, 2019

I'm pretty sure babel does the same thing. If you incompatibly mangle Object, then you incompatibly mangle Object.

@falsandtru
Copy link
Contributor Author

falsandtru commented Dec 4, 2019

Avoiding reference to global variables such as const { Object } = globalThis; is an effective performance tuning technique: https://falsandtru.github.io/benchmark/suites/10/

But the current behavior prevents this tuning.

@falsandtru
Copy link
Contributor Author

If you incompatibly mangle Object, then you incompatibly mangle Object.

As an aside, I'm not sure why you wrote the same thing twice. Do you need a coffee?

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Dec 10, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Dec 10, 2019
@RyanCavanaugh
Copy link
Member

At a minimum we should issue an error when we detect that the emit won't work

@falsandtru
Copy link
Contributor Author

However, developers would feel that error strange. And the error would be enabled only with the few module types (any other similar errors dependent on module types?). Basically other (mainly native) module types don't have this limitation. Therefore this is incompatibility of module types with a very common namespace. Compilers should hide such incompatibility as much as possible. I can find no reason to avoid an easy solution that just surrounding code by curly brackets.

@falsandtru
Copy link
Contributor Author

Node.js is a user of this technique too.

https://github.com/nodejs/node/search?l=JavaScript&q=primordials+Object

@falsandtru
Copy link
Contributor Author

Note that you must not make import { Object } from '...' an error because it won't be a runtime error.

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

No branches or pull requests

3 participants