Skip to content

Commit d4b6731

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

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/services/symbolDisplay.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
isNameOfFunctionDeclaration,
5858
isNewExpressionTarget,
5959
isObjectBindingPattern,
60+
isOptionalChain,
6061
isTaggedTemplateExpression,
6162
isThisInTypeQuery,
6263
isTransientSymbol,
@@ -322,6 +323,12 @@ function getSymbolDisplayPartsDocumentationAndSymbolKindWorker(
322323
let signature: Signature | undefined;
323324
type ??= isThisExpression ? typeChecker.getTypeAtLocation(location) : typeChecker.getTypeOfSymbolAtLocation(symbol, location);
324325

326+
// For optional symbols, use the non-optional type to avoid showing both '?' and '| undefined'
327+
// but only when not in optional chaining contexts where '| undefined' is semantically meaningful
328+
if (symbol.flags & SymbolFlags.Optional && type && !isOptionalChain(location)) {
329+
type = typeChecker.getNonOptionalType(type);
330+
}
331+
325332
if (location.parent && location.parent.kind === SyntaxKind.PropertyAccessExpression) {
326333
const right = (location.parent as PropertyAccessExpression).name;
327334
// Either the location is on the right of a property access, or on the left and the right is missing
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)