Skip to content

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

@ExE-Boss

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:

Metadata

Metadata

Assignees

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