Skip to content

Commit 77df9fa

Browse files
authored
Merge pull request #40886 from weswigham/error-on-anonymous-type-with-nonlocal-unique-symbol
Limit when we allow nested unique symbols to be serialized
2 parents 6ee4a6b + b86dc34 commit 77df9fa

6 files changed

+82
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5820,7 +5820,7 @@ namespace ts {
58205820
}
58215821
const oldFlags = context.flags;
58225822
if (type.flags & TypeFlags.UniqueESSymbol &&
5823-
type.symbol === symbol) {
5823+
type.symbol === symbol && (!context.enclosingDeclaration || some(symbol.declarations, d => getSourceFileOfNode(d) === getSourceFileOfNode(context.enclosingDeclaration!)))) {
58245824
context.flags |= NodeBuilderFlags.AllowUniqueESSymbolType;
58255825
}
58265826
const result = typeToTypeNodeHelper(type, context);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
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.
2+
3+
4+
==== tests/cases/compiler/a.ts (0 errors) ====
5+
type AX = { readonly A: unique symbol };
6+
export const A: AX = 0 as any;
7+
==== tests/cases/compiler/b.ts (1 errors) ====
8+
import { A } from './a';
9+
export const A1 = A;
10+
~~
11+
!!! error TS2527: The inferred type of 'A1' references an inaccessible 'unique symbol' type. A type annotation is necessary.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [tests/cases/compiler/declarationEmitExpressionWithNonlocalPrivateUniqueSymbol.ts] ////
2+
3+
//// [a.ts]
4+
type AX = { readonly A: unique symbol };
5+
export const A: AX = 0 as any;
6+
//// [b.ts]
7+
import { A } from './a';
8+
export const A1 = A;
9+
10+
//// [a.js]
11+
"use strict";
12+
exports.__esModule = true;
13+
exports.A = void 0;
14+
exports.A = 0;
15+
//// [b.js]
16+
"use strict";
17+
exports.__esModule = true;
18+
exports.A1 = void 0;
19+
var a_1 = require("./a");
20+
exports.A1 = a_1.A;
21+
22+
23+
//// [a.d.ts]
24+
declare type AX = {
25+
readonly A: unique symbol;
26+
};
27+
export declare const A: AX;
28+
export {};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/a.ts ===
2+
type AX = { readonly A: unique symbol };
3+
>AX : Symbol(AX, Decl(a.ts, 0, 0))
4+
>A : Symbol(A, Decl(a.ts, 0, 11))
5+
6+
export const A: AX = 0 as any;
7+
>A : Symbol(A, Decl(a.ts, 1, 12))
8+
>AX : Symbol(AX, Decl(a.ts, 0, 0))
9+
10+
=== tests/cases/compiler/b.ts ===
11+
import { A } from './a';
12+
>A : Symbol(A, Decl(b.ts, 0, 8))
13+
14+
export const A1 = A;
15+
>A1 : Symbol(A1, Decl(b.ts, 1, 12))
16+
>A : Symbol(A, Decl(b.ts, 0, 8))
17+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/a.ts ===
2+
type AX = { readonly A: unique symbol };
3+
>AX : AX
4+
>A : unique symbol
5+
6+
export const A: AX = 0 as any;
7+
>A : AX
8+
>0 as any : any
9+
>0 : 0
10+
11+
=== tests/cases/compiler/b.ts ===
12+
import { A } from './a';
13+
>A : { readonly A: unique symbol; }
14+
15+
export const A1 = A;
16+
>A1 : { readonly A: unique symbol; }
17+
>A : { readonly A: unique symbol; }
18+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @declaration: true
2+
// @filename: a.ts
3+
type AX = { readonly A: unique symbol };
4+
export const A: AX = 0 as any;
5+
// @filename: b.ts
6+
import { A } from './a';
7+
export const A1 = A;

0 commit comments

Comments
 (0)