mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Emit unqiue symbols with typeof if possible before issuing an error (#21403)
This commit is contained in:
@@ -2898,6 +2898,9 @@ namespace ts {
|
||||
}
|
||||
if (type.flags & TypeFlags.UniqueESSymbol) {
|
||||
if (!(context.flags & NodeBuilderFlags.AllowUniqueESSymbolType)) {
|
||||
if (isValueSymbolAccessible(type.symbol, context.enclosingDeclaration)) {
|
||||
return createTypeQueryNode(symbolToName(type.symbol, context, SymbolFlags.Value, /*expectsIdentifier*/ false));
|
||||
}
|
||||
if (context.tracker.reportInaccessibleUniqueSymbolError) {
|
||||
context.tracker.reportInaccessibleUniqueSymbolError();
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ class TypeWriterWalker {
|
||||
// Workaround to ensure we output 'C' instead of 'typeof C' for base class expressions
|
||||
// let type = this.checker.getTypeAtLocation(node);
|
||||
const type = node.parent && ts.isExpressionWithTypeArgumentsInClassExtendsClause(node.parent) && this.checker.getTypeAtLocation(node.parent) || this.checker.getTypeAtLocation(node);
|
||||
const typeString = type ? this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation) : "No type information available!";
|
||||
const typeString = type ? this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType) : "No type information available!";
|
||||
return {
|
||||
line: lineAndCharacter.line,
|
||||
syntaxKind: node.kind,
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
//// [indirectUniqueSymbolDeclarationEmit.ts]
|
||||
export const x = Symbol();
|
||||
export const y = Symbol();
|
||||
declare function rand(): boolean;
|
||||
export function f() {
|
||||
return rand() ? x : y;
|
||||
}
|
||||
|
||||
//// [indirectUniqueSymbolDeclarationEmit.js]
|
||||
"use strict";
|
||||
exports.__esModule = true;
|
||||
exports.x = Symbol();
|
||||
exports.y = Symbol();
|
||||
function f() {
|
||||
return rand() ? exports.x : exports.y;
|
||||
}
|
||||
exports.f = f;
|
||||
|
||||
|
||||
//// [indirectUniqueSymbolDeclarationEmit.d.ts]
|
||||
export declare const x: unique symbol;
|
||||
export declare const y: unique symbol;
|
||||
export declare function f(): typeof x | typeof y;
|
||||
@@ -0,0 +1,20 @@
|
||||
=== tests/cases/compiler/indirectUniqueSymbolDeclarationEmit.ts ===
|
||||
export const x = Symbol();
|
||||
>x : Symbol(x, Decl(indirectUniqueSymbolDeclarationEmit.ts, 0, 12))
|
||||
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
|
||||
|
||||
export const y = Symbol();
|
||||
>y : Symbol(y, Decl(indirectUniqueSymbolDeclarationEmit.ts, 1, 12))
|
||||
>Symbol : Symbol(Symbol, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --))
|
||||
|
||||
declare function rand(): boolean;
|
||||
>rand : Symbol(rand, Decl(indirectUniqueSymbolDeclarationEmit.ts, 1, 26))
|
||||
|
||||
export function f() {
|
||||
>f : Symbol(f, Decl(indirectUniqueSymbolDeclarationEmit.ts, 2, 33))
|
||||
|
||||
return rand() ? x : y;
|
||||
>rand : Symbol(rand, Decl(indirectUniqueSymbolDeclarationEmit.ts, 1, 26))
|
||||
>x : Symbol(x, Decl(indirectUniqueSymbolDeclarationEmit.ts, 0, 12))
|
||||
>y : Symbol(y, Decl(indirectUniqueSymbolDeclarationEmit.ts, 1, 12))
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
=== tests/cases/compiler/indirectUniqueSymbolDeclarationEmit.ts ===
|
||||
export const x = Symbol();
|
||||
>x : unique symbol
|
||||
>Symbol() : unique symbol
|
||||
>Symbol : SymbolConstructor
|
||||
|
||||
export const y = Symbol();
|
||||
>y : unique symbol
|
||||
>Symbol() : unique symbol
|
||||
>Symbol : SymbolConstructor
|
||||
|
||||
declare function rand(): boolean;
|
||||
>rand : () => boolean
|
||||
|
||||
export function f() {
|
||||
>f : () => unique symbol | unique symbol
|
||||
|
||||
return rand() ? x : y;
|
||||
>rand() ? x : y : unique symbol | unique symbol
|
||||
>rand() : boolean
|
||||
>rand : () => boolean
|
||||
>x : unique symbol
|
||||
>y : unique symbol
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// @lib: es6
|
||||
// @declaration: true
|
||||
export const x = Symbol();
|
||||
export const y = Symbol();
|
||||
declare function rand(): boolean;
|
||||
export function f() {
|
||||
return rand() ? x : y;
|
||||
}
|
||||
Reference in New Issue
Block a user