@@ -3360,8 +3360,13 @@ namespace ts {
3360
3360
writeAnonymousType(<ObjectType>type, nextFlags);
3361
3361
}
3362
3362
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
+ }
3365
3370
writeKeyword(writer, SyntaxKind.SymbolKeyword);
3366
3371
}
3367
3372
else if (type.flags & TypeFlags.StringOrNumberLiteral) {
@@ -8365,9 +8370,7 @@ namespace ts {
8365
8370
}
8366
8371
8367
8372
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)) {
8371
8374
const symbol = getSymbolOfNode(node);
8372
8375
const links = getSymbolLinks(symbol);
8373
8376
return links.type || (links.type = createUniqueESSymbolType(symbol));
@@ -17694,9 +17697,6 @@ namespace ts {
17694
17697
// the native Promise<T> type later in this function.
17695
17698
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);
17696
17699
}
17697
-
17698
- // widen 'unique symbol' types when we infer the return type.
17699
- type = getWidenedUniqueESSymbolType(type);
17700
17700
}
17701
17701
else {
17702
17702
let types: Type[];
@@ -17728,37 +17728,49 @@ namespace ts {
17728
17728
: voidType; // Normal function
17729
17729
}
17730
17730
}
17731
+
17731
17732
// Return a union of the return expression types.
17732
17733
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
- }
17742
17734
}
17743
17735
17744
17736
if (!contextualSignature) {
17745
17737
reportErrorsFromWidening(func, type);
17746
17738
}
17747
17739
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);
17753
17758
}
17754
17759
17755
17760
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
+ }
17762
17774
}
17763
17775
17764
17776
function checkAndAggregateYieldOperandTypes(func: FunctionLikeDeclaration, checkMode: CheckMode): Type[] {
@@ -24627,9 +24639,14 @@ namespace ts {
24627
24639
let type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature))
24628
24640
? getWidenedLiteralType(getTypeOfSymbol(symbol))
24629
24641
: unknownType;
24642
+ if (type.flags & TypeFlags.UniqueESSymbol &&
24643
+ type.symbol === symbol) {
24644
+ flags |= TypeFormatFlags.AllowUniqueESSymbolType;
24645
+ }
24630
24646
if (flags & TypeFormatFlags.AddUndefined) {
24631
24647
type = getNullableType(type, TypeFlags.Undefined);
24632
24648
}
24649
+
24633
24650
getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags);
24634
24651
}
24635
24652
0 commit comments