Skip to content

Commit 6218567

Browse files
authored
Emit unqiue symbols with typeof if possible before issuing an error (microsoft#21403)
1 parent 7a31192 commit 6218567

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,6 +2898,9 @@ namespace ts {
28982898
}
28992899
if (type.flags & TypeFlags.UniqueESSymbol) {
29002900
if (!(context.flags & NodeBuilderFlags.AllowUniqueESSymbolType)) {
2901+
if (isValueSymbolAccessible(type.symbol, context.enclosingDeclaration)) {
2902+
return createTypeQueryNode(symbolToName(type.symbol, context, SymbolFlags.Value, /*expectsIdentifier*/ false));
2903+
}
29012904
if (context.tracker.reportInaccessibleUniqueSymbolError) {
29022905
context.tracker.reportInaccessibleUniqueSymbolError();
29032906
}

src/harness/typeWriter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class TypeWriterWalker {
7979
// Workaround to ensure we output 'C' instead of 'typeof C' for base class expressions
8080
// let type = this.checker.getTypeAtLocation(node);
8181
const type = node.parent && ts.isExpressionWithTypeArgumentsInClassExtendsClause(node.parent) && this.checker.getTypeAtLocation(node.parent) || this.checker.getTypeAtLocation(node);
82-
const typeString = type ? this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation) : "No type information available!";
82+
const typeString = type ? this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType) : "No type information available!";
8383
return {
8484
line: lineAndCharacter.line,
8585
syntaxKind: node.kind,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [indirectUniqueSymbolDeclarationEmit.ts]
2+
export const x = Symbol();
3+
export const y = Symbol();
4+
declare function rand(): boolean;
5+
export function f() {
6+
return rand() ? x : y;
7+
}
8+
9+
//// [indirectUniqueSymbolDeclarationEmit.js]
10+
"use strict";
11+
exports.__esModule = true;
12+
exports.x = Symbol();
13+
exports.y = Symbol();
14+
function f() {
15+
return rand() ? exports.x : exports.y;
16+
}
17+
exports.f = f;
18+
19+
20+
//// [indirectUniqueSymbolDeclarationEmit.d.ts]
21+
export declare const x: unique symbol;
22+
export declare const y: unique symbol;
23+
export declare function f(): typeof x | typeof y;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/indirectUniqueSymbolDeclarationEmit.ts ===
2+
export const x = Symbol();
3+
>x : Symbol(x, Decl(indirectUniqueSymbolDeclarationEmit.ts, 0, 12))
4+
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
5+
6+
export const y = Symbol();
7+
>y : Symbol(y, Decl(indirectUniqueSymbolDeclarationEmit.ts, 1, 12))
8+
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
9+
10+
declare function rand(): boolean;
11+
>rand : Symbol(rand, Decl(indirectUniqueSymbolDeclarationEmit.ts, 1, 26))
12+
13+
export function f() {
14+
>f : Symbol(f, Decl(indirectUniqueSymbolDeclarationEmit.ts, 2, 33))
15+
16+
return rand() ? x : y;
17+
>rand : Symbol(rand, Decl(indirectUniqueSymbolDeclarationEmit.ts, 1, 26))
18+
>x : Symbol(x, Decl(indirectUniqueSymbolDeclarationEmit.ts, 0, 12))
19+
>y : Symbol(y, Decl(indirectUniqueSymbolDeclarationEmit.ts, 1, 12))
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/indirectUniqueSymbolDeclarationEmit.ts ===
2+
export const x = Symbol();
3+
>x : unique symbol
4+
>Symbol() : unique symbol
5+
>Symbol : SymbolConstructor
6+
7+
export const y = Symbol();
8+
>y : unique symbol
9+
>Symbol() : unique symbol
10+
>Symbol : SymbolConstructor
11+
12+
declare function rand(): boolean;
13+
>rand : () => boolean
14+
15+
export function f() {
16+
>f : () => unique symbol | unique symbol
17+
18+
return rand() ? x : y;
19+
>rand() ? x : y : unique symbol | unique symbol
20+
>rand() : boolean
21+
>rand : () => boolean
22+
>x : unique symbol
23+
>y : unique symbol
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @lib: es6
2+
// @declaration: true
3+
export const x = Symbol();
4+
export const y = Symbol();
5+
declare function rand(): boolean;
6+
export function f() {
7+
return rand() ? x : y;
8+
}

0 commit comments

Comments
 (0)