File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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" ) ;
You can’t perform that action at this time.
0 commit comments