diff --git a/src/services/services.ts b/src/services/services.ts index 812eafd2c94..f08e2ab8858 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -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)); } diff --git a/tests/cases/fourslash/quickInfoFromContextualUnionType1.ts b/tests/cases/fourslash/quickInfoFromContextualUnionType1.ts new file mode 100644 index 00000000000..32031032d2a --- /dev/null +++ b/tests/cases/fourslash/quickInfoFromContextualUnionType1.ts @@ -0,0 +1,17 @@ +/// + +// @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");