Skip to content

Should preserve unique symbols when re‑assigning module.exports #37403

Closed

Description

@ExE-Boss

TypeScript Version:

3.9.0-dev.20200315

Search Terms:

  • object property preserve unique symbol
  • module.exports preserve unique symbol

Code

// @target: ES2015
// @module: CommonJS
// @allowJs
// @checkJs
// @noEmit
// @filename: index.js
const customSymbol = Symbol("custom");
//              ^?

// This is a common pattern in Node’s built-in modules:
module.exports = {
	customSymbol,
	//        ^?
};

Workbench Repro

Also, the following requires a // @ts‑ignore comment, but otherwise works:

// some-module.js
const customSymbol = Symbol("custom");

// This is a common pattern in Node’s built-in modules:
module.exports = {
	/** @type {typeof customSymbol} */
	// @ts-ignore
	customSymbol,
// 	~~~~~~~~~~~~ TS2322: Type 'symbol' is not assignable to type 'unique symbol'.
};

Expected behavior:

// some-module.d.ts
declare const customSymbol: unique symbol;
declare const _default: {
	customSymbol: typeof customSymbol;
};
export = _default;

Actual behavior:

// some-module.d.ts
declare const _default: {
	customSymbol: symbol;
};
export = _default;

Playground Link:

https://www.typescriptlang.org/play?module=1#code/MYewdgzgLgBMCu0QFsDKBPZAjEAbGAvDBtngBQBECSyFAlANwCwAUAKYAeADiAE6xEA3qxii4iKChI5cAGlYBfZiyA

Related Issues:

Activity

  1. ExE-Boss commented on May 3, 2020

    @ExE-Boss
    ContributorAuthor

    Also, the following requires a // @ts‑ignore comment, but otherwise works:

    // some-module.js
    const customSymbol = Symbol("custom");
    
    // This is a common pattern in Node’s built-in modules:
    module.exports = {
    	/** @type {typeof customSymbol} */
    	// @ts-ignore
    	customSymbol,
    // 	~~~~~~~~~~~~ TS2322: Type 'symbol' is not assignable to type 'unique symbol'.
    };
  2. 1 remaining item

  3. typescript-bot commented on Mar 10, 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 2 repros in this issue running against the nightly TypeScript.


    Issue body code block by ExE Boss (@ExE-Boss)

    ⚠️ Assertions:

    • const customSymbol: typeof customSymbol
    • (property) customSymbol: symbol

    Comment by ExE Boss (@ExE-Boss)

    ⚠️ Assertions:

    • (property) customSymbol: symbol

    Historical Information

    Issue body code block by ExE Boss (@ExE-Boss)

    Version Reproduction Outputs
    4.1.2, 4.2.2

    ⚠️ Assertions:

    • const customSymbol: typeof customSymbol
    • (alias) const customSymbol: typeof customSymbol import customSymbol

    3.8.2, 3.9.2, 4.0.2, Nightly

    ⚠️ Assertions:

    • const customSymbol: typeof customSymbol
    • (property) customSymbol: symbol

    Comment by ExE Boss (@ExE-Boss)

    Version Reproduction Outputs
    Nightly

    ⚠️ Assertions:

    • (property) customSymbol: symbol

  4. ExE-Boss commented on Mar 10, 2021

    @ExE-Boss
    ContributorAuthor

    Looks like this got fixed at some point in TypeScript 4.1 with the changes to module.exports handling.

  5. weswigham commented on Mar 10, 2021

    @weswigham
    Member

    Yeah, #42655 probably got this as a drive by?

  6. ExE-Boss commented on Mar 11, 2021

    @ExE-Boss
    ContributorAuthor

    Note that this breaks if you do:

    // @target: ES2015
    // @module: CommonJS
    // @allowJs
    // @checkJs
    // @noEmit
    // @filename: index.js
    
    // This is also a common pattern in Node’s built-in modules:
    module.exports = {
    	customSymbol: Symbol("custom"),
    	//        ^?
    };

    Workbench Repro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

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.RescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions