Skip to content

Commit d37d50a

Browse files
committed
Ensure const enum members with same value have same type identity
1 parent 70e2c43 commit d37d50a

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/compiler/checker.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3667,12 +3667,23 @@ namespace ts {
36673667
return links.declaredType;
36683668
}
36693669

3670+
function createEnumType(symbol: Symbol): Type {
3671+
const type = createType(TypeFlags.Enum);
3672+
type.symbol = symbol;
3673+
return type;
3674+
}
3675+
3676+
function getEnumMemberType(symbol: Symbol): Type {
3677+
const links = getSymbolLinks(getParentOfSymbol(symbol));
3678+
const map = links.enumMemberTypes || (links.enumMemberTypes = {});
3679+
const value = "" + getEnumMemberValue(<EnumMember>symbol.valueDeclaration);
3680+
return map[value] || (map[value] = createEnumType(symbol));
3681+
}
3682+
36703683
function getDeclaredTypeOfEnum(symbol: Symbol): Type {
36713684
const links = getSymbolLinks(symbol);
36723685
if (!links.declaredType) {
3673-
const type = createType(TypeFlags.Enum);
3674-
type.symbol = symbol;
3675-
links.declaredType = type;
3686+
links.declaredType = symbol.flags & SymbolFlags.EnumMember ? getEnumMemberType(symbol) : createEnumType(symbol);
36763687
}
36773688
return links.declaredType;
36783689
}

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,7 @@ namespace ts {
21362136
isDeclarationWithCollidingName?: boolean; // True if symbol is block scoped redeclaration
21372137
bindingElement?: BindingElement; // Binding element associated with property symbol
21382138
exportsSomeValue?: boolean; // True if module exports some value (not just types)
2139+
enumMemberTypes?: Map<Type>; // Enum member types indexed by enum value
21392140
}
21402141

21412142
/* @internal */

0 commit comments

Comments
 (0)