Skip to content

Fix optional property quick info showing redundant '| undefined' #62084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/services/symbolDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
isNameOfFunctionDeclaration,
isNewExpressionTarget,
isObjectBindingPattern,
isOptionalChain,
isTaggedTemplateExpression,
isThisInTypeQuery,
isTransientSymbol,
Expand Down Expand Up @@ -322,6 +323,12 @@ function getSymbolDisplayPartsDocumentationAndSymbolKindWorker(
let signature: Signature | undefined;
type ??= isThisExpression ? typeChecker.getTypeAtLocation(location) : typeChecker.getTypeOfSymbolAtLocation(symbol, location);

// For optional symbols, use the non-optional type to avoid showing both '?' and '| undefined'
// but only when not in optional chaining contexts where '| undefined' is semantically meaningful
if (symbol.flags & SymbolFlags.Optional && type && !isOptionalChain(location)) {
type = typeChecker.getNonOptionalType(type);
}

if (location.parent && location.parent.kind === SyntaxKind.PropertyAccessExpression) {
const right = (location.parent as PropertyAccessExpression).name;
// Either the location is on the right of a property access, or on the left and the right is missing
Expand Down
28 changes: 28 additions & 0 deletions tests/cases/fourslash/quickInfoOptionalPropertyConsistency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// <reference path='fourslash.ts'/>

//// interface Options {
//// width?: number;
//// height?: number;
//// color?: ColorOptions;
//// border?: BorderOptions;
//// }
////
//// interface ColorOptions {
//// primary: string;
//// secondary: string;
//// }
////
//// interface BorderOptions {
//// style: string;
//// width: number;
//// }
////
//// function processOptions(options: Options) {
//// return options.wi/*1*/dth + options.he/*2*/ight + options.co/*3*/lor + options.bo/*4*/rder;
//// }

// Test that optional properties show consistently with '?' and not '| undefined'
verify.quickInfoAt("1", "(property) Options.width?: number");
verify.quickInfoAt("2", "(property) Options.height?: number");
verify.quickInfoAt("3", "(property) Options.color?: ColorOptions");
verify.quickInfoAt("4", "(property) Options.border?: BorderOptions");
Loading