Skip to content

Commit a5c9f40

Browse files
author
Andy
authored
In services, show the aliasSymbol for a type even if it's not accessible in the current scope (#17810)
1 parent 8f02979 commit a5c9f40

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,11 @@ namespace ts {
21532153
return false;
21542154
}
21552155

2156+
function isTypeSymbolAccessible(typeSymbol: Symbol, enclosingDeclaration: Node): boolean {
2157+
const access = isSymbolAccessible(typeSymbol, enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false);
2158+
return access.accessibility === SymbolAccessibility.Accessible;
2159+
}
2160+
21562161
/**
21572162
* Check if the given symbol in given enclosing declaration is accessible and mark all associated alias to be visible if requested
21582163
*
@@ -2483,8 +2488,7 @@ namespace ts {
24832488
// Ignore constraint/default when creating a usage (as opposed to declaration) of a type parameter.
24842489
return createTypeReferenceNode(name, /*typeArguments*/ undefined);
24852490
}
2486-
if (!inTypeAlias && type.aliasSymbol &&
2487-
isSymbolAccessible(type.aliasSymbol, context.enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === SymbolAccessibility.Accessible) {
2491+
if (!inTypeAlias && type.aliasSymbol && isTypeSymbolAccessible(type.aliasSymbol, context.enclosingDeclaration)) {
24882492
const name = symbolToTypeReferenceName(type.aliasSymbol);
24892493
const typeArgumentNodes = mapToTypeNodes(type.aliasTypeArguments, context);
24902494
return createTypeReferenceNode(name, typeArgumentNodes);
@@ -3251,7 +3255,7 @@ namespace ts {
32513255
buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Type, SymbolFormatFlags.None, nextFlags);
32523256
}
32533257
else if (!(flags & TypeFormatFlags.InTypeAlias) && type.aliasSymbol &&
3254-
isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === SymbolAccessibility.Accessible) {
3258+
((flags & TypeFormatFlags.UseAliasDefinedOutsideCurrentScope) || isTypeSymbolAccessible(type.aliasSymbol, enclosingDeclaration))) {
32553259
const typeArguments = type.aliasTypeArguments;
32563260
writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, length(typeArguments), nextFlags);
32573261
}

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,6 +2713,8 @@ namespace ts {
27132713
AddUndefined = 1 << 13, // Add undefined to types of initialized, non-optional parameters
27142714
WriteClassExpressionAsTypeLiteral = 1 << 14, // Write a type literal instead of (Anonymous class)
27152715
InArrayType = 1 << 15, // Writing an array element type
2716+
UseAliasDefinedOutsideCurrentScope = 1 << 16, // For a `type T = ... ` defined in a different file, write `T` instead of its value,
2717+
// even though `T` can't be accessed in the current scope.
27162718
}
27172719

27182720
export const enum SymbolFormatFlags {

src/services/utilities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,7 @@ namespace ts {
12611261
}
12621262

12631263
export function signatureToDisplayParts(typechecker: TypeChecker, signature: Signature, enclosingDeclaration?: Node, flags?: TypeFormatFlags): SymbolDisplayPart[] {
1264+
flags |= TypeFormatFlags.UseAliasDefinedOutsideCurrentScope;
12641265
return mapToDisplayParts(writer => {
12651266
typechecker.getSymbolDisplayBuilder().buildSignatureDisplay(signature, writer, enclosingDeclaration, flags);
12661267
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////export type X = { x: number };
5+
////export function f(x: X): void {}
6+
7+
// @Filename: /b.ts
8+
////import { f } from "./a";
9+
/////**/f({ x: 1 });
10+
11+
verify.quickInfoAt("", "(alias) f(x: X): void\nimport f");

0 commit comments

Comments
 (0)