Skip to content

Commit

Permalink
Fixed an issue with property type display when contextual type is a u…
Browse files Browse the repository at this point in the history
…nion (#56318)
  • Loading branch information
Andarist authored Jan 4, 2024
1 parent 0ea57f6 commit 02f9ddf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3282,12 +3282,15 @@ export function getPropertySymbolsFromContextualType(node: ObjectLiteralElementW
return symbol ? [symbol] : emptyArray;
}

const discriminatedPropertySymbols = mapDefined(contextualType.types, t => (isObjectLiteralExpression(node.parent) || isJsxAttributes(node.parent)) && checker.isTypeInvalidDueToUnionDiscriminant(t, node.parent) ? undefined : t.getProperty(name));
const filteredTypes = isObjectLiteralExpression(node.parent) || isJsxAttributes(node.parent)
? filter(contextualType.types, t => !checker.isTypeInvalidDueToUnionDiscriminant(t, node.parent))
: contextualType.types;
const discriminatedPropertySymbols = mapDefined(filteredTypes, t => t.getProperty(name));
if (unionSymbolOk && (discriminatedPropertySymbols.length === 0 || discriminatedPropertySymbols.length === contextualType.types.length)) {
const symbol = contextualType.getProperty(name);
if (symbol) return [symbol];
}
if (discriminatedPropertySymbols.length === 0) {
if (!filteredTypes.length && !discriminatedPropertySymbols.length) {
// Bad discriminant -- do again without discriminating
return mapDefined(contextualType.types, t => t.getProperty(name));
}
Expand Down
17 changes: 17 additions & 0 deletions tests/cases/fourslash/quickInfoFromContextualUnionType1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />

// @strict: true
//// // based on https://github.com/microsoft/TypeScript/issues/55495
//// type X =
//// | {
//// name: string;
//// [key: string]: any;
//// }
//// | {
//// name: "john";
//// someProp: boolean;
//// };
////
//// const obj = { name: "john", /*1*/someProp: "foo" } satisfies X;

verify.quickInfoAt("1", "(property) someProp: string");

0 comments on commit 02f9ddf

Please sign in to comment.