Merge pull request #40886 from weswigham/error-on-anonymous-type-with-nonlocal-unique-symbol

Limit when we allow nested unique symbols to be serialized
This commit is contained in:
Wesley Wigham
2020-10-05 11:59:45 -07:00
committed by GitHub
6 changed files with 82 additions and 1 deletions

View File

@@ -5820,7 +5820,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);

View File

@@ -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;
~~
!!! error TS2527: The inferred type of 'A1' references an inaccessible 'unique symbol' type. A type annotation is necessary.

View File

@@ -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 {};

View File

@@ -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))

View File

@@ -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; }

View File

@@ -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;