Skip to content

TypeScript const enum from proto prevents frontend import usage #1506

@SidStraw

Description

@SidStraw

Issue Description

Regression introduced after version 2.0

When compiling proto files to TypeScript, enums are generated as const enum types. This leads to issues in frontend projects that attempt to directly import and use these enums. For example, the following usage will result in a TypeScript error:

Cannot access ambient const enums when 'verbatimModuleSyntax' is enabled.ts(2748)

Sample code:

const checkStatus = (status: SwitchStatus.Value): boolean => {
  switch (status) {
    case SwitchStatus.Value.CLOSE:
      return false
    case SwitchStatus.Value.OPEN:
      return true
  }
}

Previously, frontend code depended on importing these enums for type-safe logic. After recent changes (see PR #1463: fix: format of typescript definition), this is no longer possible due to the const enum generation.

Expected Behavior

  • Frontend should be able to import generated proto enums and use them directly in TypeScript code without errors, regardless of TypeScript config such as verbatimModuleSyntax.

Actual Behavior

  • Importing generated enums causes build errors, breaking existing frontend usage patterns.

Related PR

Suggestions

  • Consider generating regular enum instead of const enum to maximize compatibility
  • Or provide a configuration option to choose between const enum and regular enum generation

Environment

  • TypeScript with verbatimModuleSyntax enabled (this's default option with TypeScript 5.9)
  • Frontend project importing generated proto enums
  • grpc-web version: experiencing issues since version 2.0
  • Currently reverted to version 1.5 as temporary workaround

Additional Context

  • This issue affects all frontend codebases relying on direct enum imports from generated proto definitions.
  • Temporary workaround: Reverted to grpc-web 1.5 to prevent project breakage
  • Breaking change impact: This prevents teams from upgrading to newer versions due to frontend compatibility issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions