Skip to content

Should create unique symbols when creating properties on module.exports #37404

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
ExE-Boss opened this issue Mar 15, 2020 · 5 comments · Fixed by #39816
Closed

Should create unique symbols when creating properties on module.exports #37404

ExE-Boss opened this issue Mar 15, 2020 · 5 comments · Fixed by #39816
Labels
Fix Available A PR has been opened for this issue Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Needs Investigation This issue needs a team member to investigate its status.
Milestone

Comments

@ExE-Boss
Copy link
Contributor

ExE-Boss commented Mar 15, 2020

TypeScript Version:

3.9.0-dev.20200315

Search Terms:

  • module.exports unique symbol property
  • module.exports unique symbol
  • exports unique symbol property
  • exports unique symbol

Code

// @target: ES2015
// @module: CommonJS
// @allowJs
// @checkJs
// @noEmit
// @filename: index.js

exports.customSymbol = Symbol("custom");
//                ^?

Workbench Repro

Expected behavior:

// some-module.d.ts
export const customSymbol: unique symbol;

Actual behavior:

// some-module.d.ts
export var customSymbol: symbol;

Playground Link:

https://www.typescriptlang.org/play?module=1&useJavaScript=true#code/KYDwDg9gTgLgzgOgMYFc4wgWwMoE9MBGEANgAQC8pehJAFAESrpb0CUA3AFBA

Related Issues:

@RyanCavanaugh
Copy link
Member

@rbuckton @weswigham what do we think should happen here?

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Mar 16, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 16, 2020
@weswigham
Copy link
Member

weswigham commented Mar 16, 2020

We don't make unique symbols because we see the position as mutable, ie,

exports.customSymbol = Symbol("custom1")
// later
exports.customSymbol = Symbol("custom2")

is perfectly acceptable.

In theory, we could recognize

Object.defineProperty(module.exports, "customSymbol", {
  writable: false,
  value: Symbol("custom")
});

as making the customSymbol property immutable and thus the symbol appropriately unique, but we currently do not.

@ExE-Boss
Copy link
Contributor Author

ExE-Boss commented Mar 16, 2020

But TypeScript complains when you re‑assign a property on module.exports with:

TS2323: Cannot redeclare exported variable 'customSymbol'.

Also, that code looks like a bug to me, so I don’t think it should be the default.


Also, if you do: (🔗)

const customSymbol = Symbol("custom");
exports.customSymbol = customSymbol;

Then you get:

export var customSymbol: typeof customSymbol;
declare const customSymbol: unique symbol;
export {};

@weswigham
Copy link
Member

Hm. I didn't realize we issued the Cannot redeclare exported variable 'customSymbol'. error - I'm surprised we do, actually, considering there's no such limitation for cjs modules. Afaik we shouldn't be.

@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Jul 29, 2020
@typescript-bot typescript-bot added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Mar 11, 2021
@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 12, 2021

👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in this issue running against the nightly TypeScript.


Issue body code block by @ExE-Boss

⚠️ Assertions:

  • (property) customSymbol: symbol

Historical Information

Issue body code block by @ExE-Boss

Version Reproduction Outputs
3.8.2, 3.9.2, 4.0.2, 4.1.2, 4.2.2, Nightly

⚠️ Assertions:

  • (property) customSymbol: symbol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix Available A PR has been opened for this issue Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants