diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c864a8aa76a..eac5d563357 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3599,6 +3599,10 @@ module ts { return type; } + function getUnionTypeOfSubtypeConstituents(source: UnionType, target: Type): Type { + return getUnionType(filter(source.types, t => isTypeSubtypeOf(t, target))); + } + function getReducedTypeOfUnionType(type: UnionType): Type { // If union type was created without subtype reduction, perform the deferred reduction now if (!type.reducedType) { @@ -5354,26 +5358,26 @@ module ts { if (prototypeProperty) { let targetType = getTypeOfSymbol(prototypeProperty); if (targetType !== anyType) { - // Narrow to target type if it is a subtype of current type + // Narrow to the target type if it's a subtype of the current type if (isTypeSubtypeOf(targetType, type)) { return targetType; } - // If current type is a union type, remove all constituents that aren't subtypes of target type + // If the current type is a union type, remove all constituents that aren't subtypes of the target. if (type.flags & TypeFlags.Union) { - return getUnionType(filter((type).types, t => isTypeSubtypeOf(t, targetType))); + return getUnionTypeOfSubtypeConstituents(type, targetType); } } } // Target type is type of constructor signiture - let constructSignitures: Signature[]; + let constructSignatures: Signature[]; if (rightType.flags & TypeFlags.Interface) { - constructSignitures = (rightType).declaredConstructSignatures; + constructSignatures = (rightType).declaredConstructSignatures; + } else if (rightType.flags & TypeFlags.Anonymous) { + constructSignatures = (rightType).constructSignatures; } - if (rightType.flags & TypeFlags.Anonymous) { - constructSignitures = (rightType).constructSignatures; - } - if (constructSignitures) { - let instanceType = getUnionType(map(constructSignitures, constructSignature => { + + if (constructSignatures) { + let instanceType = getUnionType(map(constructSignatures, constructSignature => { if (constructSignature.typeParameters && constructSignature.typeParameters.length !== 0) { constructSignature = instantiateSignature(constructSignature, createTypeMapper(constructSignature.typeParameters, map(constructSignature.typeParameters, _ => anyType)), true) } @@ -5381,7 +5385,7 @@ module ts { })); // Pickup type from union types if (type.flags & TypeFlags.Union) { - return getUnionType(filter((type).types, t => isTypeSubtypeOf(t, instanceType))); + return getUnionTypeOfSubtypeConstituents(type, instanceType); } return instanceType; }