Skip to content

Limit when we allow nested unique symbols to be serialized #40886

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5812,7 +5812,7 @@ namespace ts {
}
const oldFlags = context.flags;
if (type.flags & TypeFlags.UniqueESSymbol &&
type.symbol === symbol) {
type.symbol === symbol && (!context.enclosingDeclaration || some(symbol.declarations, d => getSourceFileOfNode(d) === getSourceFileOfNode(context.enclosingDeclaration!)))) {
context.flags |= NodeBuilderFlags.AllowUniqueESSymbolType;
}
const result = typeToTypeNodeHelper(type, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
tests/cases/compiler/b.ts(2,14): error TS2527: The inferred type of 'A1' references an inaccessible 'unique symbol' type. A type annotation is necessary.


==== tests/cases/compiler/a.ts (0 errors) ====
type AX = { readonly A: unique symbol };
export const A: AX = 0 as any;
==== tests/cases/compiler/b.ts (1 errors) ====
import { A } from './a';
export const A1 = A;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, why couldn’t it just serialize as typeof A (or typeof import('./a').A if the import gets erased) here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I said as why in the issue, but I'll restate it here: because that could only work for specific patterns of assignments, and while we may carve out syntactic patterns for checking JS, we do not for TS. So because it doesn't generalize beyond var x = entity.name assignments to all assignments, it doesn't make sense to do, really.

~~
!!! error TS2527: The inferred type of 'A1' references an inaccessible 'unique symbol' type. A type annotation is necessary.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/compiler/declarationEmitExpressionWithNonlocalPrivateUniqueSymbol.ts] ////

//// [a.ts]
type AX = { readonly A: unique symbol };
export const A: AX = 0 as any;
//// [b.ts]
import { A } from './a';
export const A1 = A;

//// [a.js]
"use strict";
exports.__esModule = true;
exports.A = void 0;
exports.A = 0;
//// [b.js]
"use strict";
exports.__esModule = true;
exports.A1 = void 0;
var a_1 = require("./a");
exports.A1 = a_1.A;


//// [a.d.ts]
declare type AX = {
readonly A: unique symbol;
};
export declare const A: AX;
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/a.ts ===
type AX = { readonly A: unique symbol };
>AX : Symbol(AX, Decl(a.ts, 0, 0))
>A : Symbol(A, Decl(a.ts, 0, 11))

export const A: AX = 0 as any;
>A : Symbol(A, Decl(a.ts, 1, 12))
>AX : Symbol(AX, Decl(a.ts, 0, 0))

=== tests/cases/compiler/b.ts ===
import { A } from './a';
>A : Symbol(A, Decl(b.ts, 0, 8))

export const A1 = A;
>A1 : Symbol(A1, Decl(b.ts, 1, 12))
>A : Symbol(A, Decl(b.ts, 0, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/a.ts ===
type AX = { readonly A: unique symbol };
>AX : AX
>A : unique symbol

export const A: AX = 0 as any;
>A : AX
>0 as any : any
>0 : 0

=== tests/cases/compiler/b.ts ===
import { A } from './a';
>A : { readonly A: unique symbol; }

export const A1 = A;
>A1 : { readonly A: unique symbol; }
>A : { readonly A: unique symbol; }

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @declaration: true
// @filename: a.ts
type AX = { readonly A: unique symbol };
export const A: AX = 0 as any;
// @filename: b.ts
import { A } from './a';
export const A1 = A;