Skip to content

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

Closed

Description

@ExE-Boss

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:

Activity

  1. RyanCavanaugh commented on Mar 16, 2020

    @RyanCavanaugh
    Member

    Ron Buckton (@rbuckton) Wesley Wigham (@weswigham) what do we think should happen here?

  2. weswigham commented on Mar 16, 2020

    @weswigham
    Member

    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.

  3. ExE-Boss commented on Mar 16, 2020

    @ExE-Boss
    ContributorAuthor

    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 {};
  4. weswigham commented on Mar 16, 2020

    @weswigham
    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.

  5. typescript-bot commented on Mar 12, 2021

    @typescript-bot
    Contributor

    👋 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 (@ExE-Boss)

    ⚠️ Assertions:

    • (property) customSymbol: symbol

    Historical Information

    Issue body code block by ExE Boss (@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fix AvailableA PR has been opened for this issueHas ReproThis issue has compiler-backed repros: https://aka.ms/ts-reprosNeeds InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions