Skip to content

Unable to use const enum in third-party API #1216

Open
@AlCalzone

Description

@AlCalzone

This is a followup to #829

The library @homebridge/ciao has this definition:
https://github.com/homebridge/ciao/blob/11e5abcd8656ce627517275013cc44bbb1bf7769/src/index.ts#L37-L40

export const enum Protocol {
  TCP = "tcp",
  UDP = "udp",
}

Its API expects this enum to be used for some option parameters, see https://github.com/homebridge/ciao/blob/11e5abcd8656ce627517275013cc44bbb1bf7769/src/CiaoService.ts#L78

This used to work with TS 5.x, but tsgo no longer accepts the existing code. I understand the intention is to not support const enums cross module, but the current behavior is problematic.

If I try to use the string directly, tsgo complains about a mismatch of types:

src/lib/server.ts:543:9 - error TS2322: Type '"tcp"' is not assignable to type 'Protocol | undefined'.

543         protocol: "tcp",
            ~~~~~~~~

--> I would expect this to work

If I use the imported const enum with isolatedModules, tsgo complains about the const enum:

src/lib/server.ts:543:19 - error TS2748: Cannot access ambient const enums when 'isolatedModules' is enabled.

543         protocol: Protocol.TCP,
                      ~~~~~~~~

Found 1 error in src/lib/server.ts:543

--> This seems fine to me

And if I use the imported const enum without isolatedModules, I end up with an undefined reference in the compiled JS output, without any warning that this might be a problem:

Image

--> I would expect this to be a compile-time error

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: EmitRelated to emit, AST printing, Program emit funcsDomain: Porting MetaMeta-level porting concernsDomain: Type CheckingRelated to type checking, grammar checking

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions