mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
Fixed an issue with property type display when contextual type is a union (#56318)
This commit is contained in:
parent
0ea57f6ca1
commit
02f9ddf55d
@ -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));
|
||||
}
|
||||
|
||||
17
tests/cases/fourslash/quickInfoFromContextualUnionType1.ts
Normal file
17
tests/cases/fourslash/quickInfoFromContextualUnionType1.ts
Normal 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");
|
||||
Loading…
x
Reference in New Issue
Block a user