Skip to content

Commit 0ab5c4a

Browse files
committed
Fix optional property quick info showing redundant '| undefined'
1 parent 65cb4bd commit 0ab5c4a

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/services/symbolDisplay.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ function getSymbolDisplayPartsDocumentationAndSymbolKindWorker(
322322
let signature: Signature | undefined;
323323
type ??= isThisExpression ? typeChecker.getTypeAtLocation(location) : typeChecker.getTypeOfSymbolAtLocation(symbol, location);
324324

325+
// For optional symbols, use the non-optional type to avoid showing both '?' and '| undefined'
326+
if (symbol.flags & SymbolFlags.Optional && type) {
327+
type = typeChecker.getNonOptionalType(type);
328+
}
329+
325330
if (location.parent && location.parent.kind === SyntaxKind.PropertyAccessExpression) {
326331
const right = (location.parent as PropertyAccessExpression).name;
327332
// Either the location is on the right of a property access, or on the left and the right is missing

tests/cases/fourslash/quickInfoInOptionalChain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
verify.quickInfoAt("1", "(property) A.arr: string[]");
2424
verify.quickInfoAt("2", "(property) Foo.bar: {\n baz: string;\n}");
2525
verify.quickInfoAt("3", "(property) baz: string | undefined");
26-
verify.quickInfoAt("4", "(property) Foo2.bar?: {\n baz: {\n qwe: string;\n };\n} | undefined");
26+
verify.quickInfoAt("4", "(property) Foo2.bar?: {\n baz: {\n qwe: string;\n };\n}");
2727
verify.quickInfoAt("5", "(property) baz: {\n qwe: string;\n}");
2828
verify.quickInfoAt("6", "(property) qwe: string | undefined");
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
//// interface Options {
4+
//// width?: number;
5+
//// height?: number;
6+
//// color?: ColorOptions;
7+
//// border?: BorderOptions;
8+
//// }
9+
////
10+
//// interface ColorOptions {
11+
//// primary: string;
12+
//// secondary: string;
13+
//// }
14+
////
15+
//// interface BorderOptions {
16+
//// style: string;
17+
//// width: number;
18+
//// }
19+
////
20+
//// function processOptions(options: Options) {
21+
//// return options.wi/*1*/dth + options.he/*2*/ight + options.co/*3*/lor + options.bo/*4*/rder;
22+
//// }
23+
24+
// Test that optional properties show consistently with '?' and not '| undefined'
25+
verify.quickInfoAt("1", "(property) Options.width?: number");
26+
verify.quickInfoAt("2", "(property) Options.height?: number");
27+
verify.quickInfoAt("3", "(property) Options.color?: ColorOptions");
28+
verify.quickInfoAt("4", "(property) Options.border?: BorderOptions");

0 commit comments

Comments
 (0)