Skip to content
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

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

Merged
Merged
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: 5 additions & 2 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3273,12 +3273,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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main idea behind the fix is that we don't want to go into this branch because the type satisfies its contextual type in this test case. So there is one filtered type here but there are no discriminated property symbols. The first type in the union doesn't have an explicit prop for someProp and the second one is rejected by the isTypeInvalidDueToUnionDiscriminant check.

// 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");
Loading