Skip to content

Commit

Permalink
Deduplicated code from someDirectSubtypeContainsPropName and merged i…
Browse files Browse the repository at this point in the history
…t with getPropertyOfType
  • Loading branch information
marekdedic committed Oct 22, 2021
1 parent 2015750 commit 08dcd94
Showing 1 changed file with 7 additions and 28 deletions.
35 changes: 7 additions & 28 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12118,8 +12118,11 @@ namespace ts {
return property;
}

function getPropertyOfUnionOrIntersectionType(type: UnionOrIntersectionType, name: __String, skipObjectFunctionPropertyAugment?: boolean): Symbol | undefined {
function getPropertyOfUnionOrIntersectionType(type: UnionOrIntersectionType, name: __String, skipObjectFunctionPropertyAugment?: boolean, includePartialProperties?: boolean): Symbol | undefined {
const property = getUnionOrIntersectionProperty(type, name, skipObjectFunctionPropertyAugment);
if (includePartialProperties) {
return property;
}
// We need to filter out partial properties in union types
return property && !(getCheckFlags(property) & CheckFlags.ReadPartial) ? property : undefined;
}
Expand Down Expand Up @@ -12197,7 +12200,7 @@ namespace ts {
* @param type a type to look up property from
* @param name a name of property to look up in a given type
*/
function getPropertyOfType(type: Type, name: __String, skipObjectFunctionPropertyAugment?: boolean): Symbol | undefined {
function getPropertyOfType(type: Type, name: __String, skipObjectFunctionPropertyAugment?: boolean, includePartialProperties?: boolean): Symbol | undefined {
type = getReducedApparentType(type);
if (type.flags & TypeFlags.Object) {
const resolved = resolveStructuredTypeMembers(type as ObjectType);
Expand All @@ -12219,7 +12222,7 @@ namespace ts {
return getPropertyOfObjectType(globalObjectType, name);
}
if (type.flags & TypeFlags.UnionOrIntersection) {
return getPropertyOfUnionOrIntersectionType(type as UnionOrIntersectionType, name, skipObjectFunctionPropertyAugment);
return getPropertyOfUnionOrIntersectionType(type as UnionOrIntersectionType, name, skipObjectFunctionPropertyAugment, includePartialProperties);
}
return undefined;
}
Expand Down Expand Up @@ -24160,32 +24163,8 @@ namespace ts {
}
return type;

// This function is almost like function `getPropertyOfType`, except when type.flags contains `UnionOrIntersection`
// it would return the property rather than undefiend even when property is partial.
function someDirectSubtypeContainsPropName(type: Type, name: __String): Symbol | undefined {
type = getReducedApparentType(type);
if (type.flags & TypeFlags.Object) {
const resolved = resolveStructuredTypeMembers(type as ObjectType);
const symbol = resolved.members.get(name);
if (symbol && symbolIsValue(symbol)) {
return symbol;
}
const functionType = resolved === anyFunctionType ? globalFunctionType :
resolved.callSignatures.length ? globalCallableFunctionType :
resolved.constructSignatures.length ? globalNewableFunctionType :
undefined;
if (functionType) {
const symbol = getPropertyOfObjectType(functionType, name);
if (symbol) {
return symbol;
}
}
return getPropertyOfObjectType(globalObjectType, name);
}
if (type.flags & TypeFlags.UnionOrIntersection) {
return getUnionOrIntersectionProperty(type as UnionOrIntersectionType, name);
}
return undefined;
return getPropertyOfType(type, name, false, true);
}
}

Expand Down

0 comments on commit 08dcd94

Please sign in to comment.