@@ -3360,8 +3360,13 @@ namespace ts {
33603360 writeAnonymousType(<ObjectType>type, nextFlags);
33613361 }
33623362 else if (type.flags & TypeFlags.UniqueESSymbol) {
3363- writeKeyword(writer, SyntaxKind.UniqueKeyword);
3364- writeSpace(writer);
3363+ if (flags & TypeFormatFlags.AllowUniqueESSymbolType) {
3364+ writeKeyword(writer, SyntaxKind.UniqueKeyword);
3365+ writeSpace(writer);
3366+ }
3367+ else {
3368+ writer.reportInaccessibleUniqueSymbolError();
3369+ }
33653370 writeKeyword(writer, SyntaxKind.SymbolKeyword);
33663371 }
33673372 else if (type.flags & TypeFlags.StringOrNumberLiteral) {
@@ -8365,9 +8370,7 @@ namespace ts {
83658370 }
83668371
83678372 function getESSymbolLikeTypeForNode(node: Node) {
8368- if (isVariableDeclaration(node) ? isConst(node) && isIdentifier(node.name) && isVariableDeclarationInVariableStatement(node) :
8369- isPropertyDeclaration(node) ? hasReadonlyModifier(node) && hasStaticModifier(node) :
8370- isPropertySignature(node) && hasReadonlyModifier(node)) {
8373+ if (isValidESSymbolDeclaration(node)) {
83718374 const symbol = getSymbolOfNode(node);
83728375 const links = getSymbolLinks(symbol);
83738376 return links.type || (links.type = createUniqueESSymbolType(symbol));
@@ -17694,9 +17697,6 @@ namespace ts {
1769417697 // the native Promise<T> type later in this function.
1769517698 type = checkAwaitedType(type, /*errorNode*/ func, Diagnostics.The_return_type_of_an_async_function_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member);
1769617699 }
17697-
17698- // widen 'unique symbol' types when we infer the return type.
17699- type = getWidenedUniqueESSymbolType(type);
1770017700 }
1770117701 else {
1770217702 let types: Type[];
@@ -17728,37 +17728,49 @@ namespace ts {
1772817728 : voidType; // Normal function
1772917729 }
1773017730 }
17731+
1773117732 // Return a union of the return expression types.
1773217733 type = getUnionType(types, /*subtypeReduction*/ true);
17733-
17734- // widen 'unique symbol' types when we infer the return type.
17735- type = getWidenedUniqueESSymbolType(type);
17736-
17737- if (functionFlags & FunctionFlags.Generator) { // AsyncGenerator function or Generator function
17738- type = functionFlags & FunctionFlags.Async
17739- ? createAsyncIterableIteratorType(type) // AsyncGenerator function
17740- : createIterableIteratorType(type); // Generator function
17741- }
1774217734 }
1774317735
1774417736 if (!contextualSignature) {
1774517737 reportErrorsFromWidening(func, type);
1774617738 }
1774717739
17748- if (isUnitType(type) &&
17749- !(contextualSignature &&
17750- isLiteralContextualType(
17751- contextualSignature === getSignatureFromDeclaration(func) ? type : getReturnTypeOfSignature(contextualSignature)))) {
17752- type = getWidenedLiteralType(type);
17740+ if (isUnitType(type)) {
17741+ let contextualType = !contextualSignature ? undefined :
17742+ contextualSignature === getSignatureFromDeclaration(func) ? type :
17743+ getReturnTypeOfSignature(contextualSignature);
17744+ if (contextualType) {
17745+ switch (functionFlags & FunctionFlags.AsyncGenerator) {
17746+ case FunctionFlags.AsyncGenerator:
17747+ contextualType = getIteratedTypeOfGenerator(contextualType, /*isAsyncGenerator*/ true);
17748+ break;
17749+ case FunctionFlags.Generator:
17750+ contextualType = getIteratedTypeOfGenerator(contextualType, /*isAsyncGenerator*/ false);
17751+ break;
17752+ case FunctionFlags.Async:
17753+ contextualType = getPromisedTypeOfPromise(contextualType);
17754+ break;
17755+ }
17756+ }
17757+ type = getWidenedLiteralLikeTypeForContextualType(type, contextualType);
1775317758 }
1775417759
1775517760 const widenedType = getWidenedType(type);
17756- // From within an async function you can return either a non-promise value or a promise. Any
17757- // Promise/A+ compatible implementation will always assimilate any foreign promise, so the
17758- // return type of the body is awaited type of the body, wrapped in a native Promise<T> type.
17759- return (functionFlags & FunctionFlags.AsyncGenerator) === FunctionFlags.Async
17760- ? createPromiseReturnType(func, widenedType) // Async function
17761- : widenedType; // Generator function, AsyncGenerator function, or normal function
17761+ switch (functionFlags & FunctionFlags.AsyncGenerator) {
17762+ case FunctionFlags.AsyncGenerator:
17763+ return createAsyncIterableIteratorType(widenedType);
17764+ case FunctionFlags.Generator:
17765+ return createIterableIteratorType(widenedType);
17766+ case FunctionFlags.Async:
17767+ // From within an async function you can return either a non-promise value or a promise. Any
17768+ // Promise/A+ compatible implementation will always assimilate any foreign promise, so the
17769+ // return type of the body is awaited type of the body, wrapped in a native Promise<T> type.
17770+ return createPromiseType(widenedType);
17771+ default:
17772+ return widenedType;
17773+ }
1776217774 }
1776317775
1776417776 function checkAndAggregateYieldOperandTypes(func: FunctionLikeDeclaration, checkMode: CheckMode): Type[] {
@@ -24627,9 +24639,14 @@ namespace ts {
2462724639 let type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature))
2462824640 ? getWidenedLiteralType(getTypeOfSymbol(symbol))
2462924641 : unknownType;
24642+ if (type.flags & TypeFlags.UniqueESSymbol &&
24643+ type.symbol === symbol) {
24644+ flags |= TypeFormatFlags.AllowUniqueESSymbolType;
24645+ }
2463024646 if (flags & TypeFormatFlags.AddUndefined) {
2463124647 type = getNullableType(type, TypeFlags.Undefined);
2463224648 }
24649+
2463324650 getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags);
2463424651 }
2463524652
0 commit comments