Skip to content

Commit 02f9ddf

Browse files
authoredJan 4, 2024
Fixed an issue with property type display when contextual type is a union (#56318)
1 parent 0ea57f6 commit 02f9ddf

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed
 

‎src/services/services.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -3282,12 +3282,15 @@ export function getPropertySymbolsFromContextualType(node: ObjectLiteralElementW
32823282
return symbol ? [symbol] : emptyArray;
32833283
}
32843284

3285-
const discriminatedPropertySymbols = mapDefined(contextualType.types, t => (isObjectLiteralExpression(node.parent) || isJsxAttributes(node.parent)) && checker.isTypeInvalidDueToUnionDiscriminant(t, node.parent) ? undefined : t.getProperty(name));
3285+
const filteredTypes = isObjectLiteralExpression(node.parent) || isJsxAttributes(node.parent)
3286+
? filter(contextualType.types, t => !checker.isTypeInvalidDueToUnionDiscriminant(t, node.parent))
3287+
: contextualType.types;
3288+
const discriminatedPropertySymbols = mapDefined(filteredTypes, t => t.getProperty(name));
32863289
if (unionSymbolOk && (discriminatedPropertySymbols.length === 0 || discriminatedPropertySymbols.length === contextualType.types.length)) {
32873290
const symbol = contextualType.getProperty(name);
32883291
if (symbol) return [symbol];
32893292
}
3290-
if (discriminatedPropertySymbols.length === 0) {
3293+
if (!filteredTypes.length && !discriminatedPropertySymbols.length) {
32913294
// Bad discriminant -- do again without discriminating
32923295
return mapDefined(contextualType.types, t => t.getProperty(name));
32933296
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @strict: true
4+
//// // based on https://github.com/microsoft/TypeScript/issues/55495
5+
//// type X =
6+
//// | {
7+
//// name: string;
8+
//// [key: string]: any;
9+
//// }
10+
//// | {
11+
//// name: "john";
12+
//// someProp: boolean;
13+
//// };
14+
////
15+
//// const obj = { name: "john", /*1*/someProp: "foo" } satisfies X;
16+
17+
verify.quickInfoAt("1", "(property) someProp: string");

0 commit comments

Comments
 (0)
Please sign in to comment.