Compare enums semi-structurally.

1. Unqualified names must match.
2. Target contains members with same names as all source members.
This commit is contained in:
Nathan Shively-Sanders 2015-12-10 11:03:45 -08:00
parent ecfeb21965
commit 6cd5a4dac9

View file

@ -5040,6 +5040,11 @@ namespace ts {
if (source === undefinedType) return Ternary.True;
if (source === nullType && target !== undefinedType) return Ternary.True;
if (source.flags & TypeFlags.Enum && target === numberType) return Ternary.True;
if (source.flags & TypeFlags.Enum && target.flags & TypeFlags.Enum) {
if (result = enumRelatedTo(source, target)) {
return result;
}
}
if (source.flags & TypeFlags.StringLiteral && target === stringType) return Ternary.True;
if (relation === assignableRelation) {
if (isTypeAny(source)) return Ternary.True;
@ -5750,6 +5755,25 @@ namespace ts {
}
return Ternary.False;
}
function enumRelatedTo(source: Type, target: Type) {
if (source.symbol.name !== target.symbol.name) {
return Ternary.False;
}
const sourceDecl = <EnumDeclaration>getMergedSymbol(source.symbol).valueDeclaration;
const targetDecl = <EnumDeclaration>getMergedSymbol(target.symbol).valueDeclaration;
const targetMembers = arrayToMap(targetDecl.members, member => getTextOfPropertyName(<PropertyName>member.name));
for (const member of sourceDecl.members) {
const name = getTextOfPropertyName(<PropertyName>member.name);
if (!targetMembers[name]) {
reportError(Diagnostics.Property_0_is_missing_in_type_1,
name,
typeToString(target, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType));
return Ternary.False;
}
}
return Ternary.True;
}
}
// Return true if the given type is part of a deeply nested chain of generic instantiations. We consider this to be the case