Fixed an issue with property type display when contextual type is a union (#56318)

This commit is contained in:
Mateusz Burzyński 2024-01-04 18:29:15 +01:00 committed by GitHub
parent 0ea57f6ca1
commit 02f9ddf55d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View File

@ -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));
}

View File

@ -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");