Skip to content

When there is only one member in an enumDeclaration, const enum E {A = 1}, the declared type of the enumDeclaration is incorrect #61871

Open
@liyanch

Description

@liyanch

🔎 Search Terms

We use the version of TypeScript is v4.9.5.

🕗 Version & Regression Information

  • This changed between versions v4.9.5 and v5.8.x.
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because the case of const enum EnumD1 { A1 = 1 }

⏯ Playground Link

No response

💻 Code

// Your code here
const enum EnumD1 {
  A1 = 1
}

const enum EnumD2 {
  A1 = 1, B1 = 2
}
export {EnumD1}
export {EnumD2}

🙁 Actual behavior

// the source code about TypeScript
function getDeclaredTypeOfEnum(symbol: Symbol): Type {
  const links = getSymbolLinks(symbol);
  if (links.declaredType) {
      return links.declaredType;
  }
  if (getEnumKind(symbol) === EnumKind.Literal) {
      enumCount++;
      const memberTypeList: Type[] = [];
      if (symbol.declarations) {
          for (const declaration of symbol.declarations) {
              if (declaration.kind === SyntaxKind.EnumDeclaration) {
                  for (const member of (declaration as EnumDeclaration).members) {
                      const value = getEnumMemberValue(member);
                      const memberType = getFreshTypeOfLiteralType(getEnumLiteralType(value !== undefined ? value : 0, enumCount, getSymbolOfNode(member)));
                      getSymbolLinks(getSymbolOfNode(member)).declaredType = memberType;
                      memberTypeList.push(getRegularTypeOfLiteralType(memberType));
                  }
              }
          }
      }
      if (memberTypeList.length) {
          const enumType = getUnionType(memberTypeList, UnionReduction.Literal, symbol, /*aliasTypeArguments*/ undefined);
          if (**enumType.flags & TypeFlags.Union**) {
              enumType.flags |= TypeFlags.EnumLiteral;
              **enumType.symbol = symbol;**
          }
          **return links.declaredType = enumType;**
      }
  }
  const enumType = createType(TypeFlags.Enum);
  enumType.symbol = symbol;
  return links.declaredType = enumType;
}

When const enum EnumD1 { A1 = 1 } is used, EnumD1 is not a Union type. Therefore, the links.declaredType obtained is A1 instead of EnumD1, which is not expected.
When const enum EnumD2 { A1 = 1, B1 = 2 } is used, EnumD1 is a Union type. Therefore, the links.declaredType obtained is EnumD2, which is
expected.

🙂 Expected behavior

When const enum EnumD1 { A1 = 1 } is used, the links.declaredType obtained is EnumD1.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions