@@ -7924,14 +7924,7 @@ namespace ts {
79247924 links.resolvedType = getIndexType(getTypeFromTypeNode(node.type));
79257925 break;
79267926 case SyntaxKind.UniqueKeyword:
7927- if (node.type.kind === SyntaxKind.SymbolKeyword) {
7928- const parent = skipParentheses(node).parent;
7929- const symbol = getSymbolOfNode(parent);
7930- links.resolvedType = symbol ? getUniqueESSymbolTypeForSymbol(symbol) : esSymbolType;
7931- }
7932- else {
7933- links.resolvedType = unknownType;
7934- }
7927+ links.resolvedType = getUniqueType(<UniqueTypeOperatorNode>node);
79357928 break;
79367929 }
79377930 }
@@ -8299,37 +8292,35 @@ namespace ts {
82998292 return links.resolvedType;
83008293 }
83018294
8295+ function nodeCanHaveUniqueESSymbolType(node: Node) {
8296+ return isVariableDeclaration(node) ? isConst(node) && isIdentifier(node.name) && isVariableDeclarationInVariableStatement(node) :
8297+ isPropertyDeclaration(node) ? hasReadonlyModifier(node) && hasStaticModifier(node) :
8298+ isPropertySignature(node) ? hasReadonlyModifier(node) :
8299+ false;
8300+ }
8301+
83028302 function createUniqueESSymbolType(symbol: Symbol) {
83038303 const type = <UniqueESSymbolType>createType(TypeFlags.UniqueESSymbol);
83048304 type.symbol = symbol;
83058305 return type;
83068306 }
83078307
8308- function isReferenceToValidDeclarationForUniqueESSymbol(symbol: Symbol) {
8309- if (symbol.valueDeclaration) {
8310- switch (symbol.valueDeclaration.kind) {
8311- case SyntaxKind.VariableDeclaration:
8312- return getNameOfDeclaration(symbol.valueDeclaration).kind === SyntaxKind.Identifier
8313- && isVariableDeclarationInVariableStatement(<VariableDeclaration>symbol.valueDeclaration)
8314- && !!(symbol.valueDeclaration.parent.flags & NodeFlags.Const);
8315- case SyntaxKind.PropertySignature:
8316- return hasModifier(symbol.valueDeclaration, ModifierFlags.Readonly);
8317- case SyntaxKind.PropertyDeclaration:
8318- return hasModifier(symbol.valueDeclaration, ModifierFlags.Readonly)
8319- && hasModifier(symbol.valueDeclaration, ModifierFlags.Static);
8320- }
8321- }
8322- return false;
8323- }
8324-
8325- function getUniqueESSymbolTypeForSymbol(symbol: Symbol) {
8326- if (isReferenceToValidDeclarationForUniqueESSymbol(symbol)) {
8308+ function getUniqueESSymbolTypeForNode(node: Node) {
8309+ const parent = walkUpParentheses(node);
8310+ if (nodeCanHaveUniqueESSymbolType(parent)) {
8311+ const symbol = getSymbolOfNode(parent);
83278312 const links = getSymbolLinks(symbol);
83288313 return links.type || (links.type = createUniqueESSymbolType(symbol));
83298314 }
83308315 return esSymbolType;
83318316 }
83328317
8318+ function getUniqueType(node: UniqueTypeOperatorNode) {
8319+ return node.type.kind === SyntaxKind.SymbolKeyword
8320+ ? getUniqueESSymbolTypeForNode(node.parent)
8321+ : unknownType;
8322+ }
8323+
83338324 function getTypeFromJSDocVariadicType(node: JSDocVariadicType): Type {
83348325 const links = getNodeLinks(node);
83358326 if (!links.resolvedType) {
@@ -17180,9 +17171,7 @@ namespace ts {
1718017171 // Treat any call to the global 'Symbol' function that is part of a const variable or readonly property
1718117172 // as a fresh unique symbol literal type.
1718217173 if (returnType.flags & TypeFlags.ESSymbolLike && isSymbolOrSymbolForCall(node)) {
17183- const parent = skipParentheses(node).parent;
17184- const symbol = getSymbolOfNode(parent);
17185- if (symbol) return getUniqueESSymbolTypeForSymbol(symbol);
17174+ return getUniqueESSymbolTypeForNode(node.parent);
1718617175 }
1718717176 return returnType;
1718817177 }
@@ -25542,19 +25531,6 @@ namespace ts {
2554225531 }
2554325532 break;
2554425533
25545- // report specific errors for cases where a `unique symbol` type is disallowed even when it is in a `const` or `readonly` declaration.
25546- case SyntaxKind.UnionType:
25547- return grammarErrorOnNode(node, Diagnostics.unique_symbol_types_are_not_allowed_in_a_union_type);
25548- case SyntaxKind.IntersectionType:
25549- return grammarErrorOnNode(node, Diagnostics.unique_symbol_types_are_not_allowed_in_an_intersection_type);
25550- case SyntaxKind.ArrayType:
25551- return grammarErrorOnNode(node, Diagnostics.unique_symbol_types_are_not_allowed_in_an_array_type);
25552- case SyntaxKind.TupleType:
25553- return grammarErrorOnNode(node, Diagnostics.unique_symbol_types_are_not_allowed_in_a_tuple_type);
25554- case SyntaxKind.MappedType:
25555- return grammarErrorOnNode(node, Diagnostics.unique_symbol_types_are_not_allowed_in_a_mapped_type);
25556-
25557- // report a general error for any other invalid use site.
2555825534 default:
2555925535 return grammarErrorOnNode(node, Diagnostics.unique_symbol_types_are_not_allowed_here);
2556025536 }
0 commit comments