Skip to content

Comparability/type assertion failure when using enums and underlying literals #53400

Description

The regression reported here can be traced back to #53192. The minimal~ repro case for it is this:

enum AutomationMode {
  NONE = "",
  TIME = "time",
  SYSTEM = "system",
  LOCATION = "location",
}

interface ThemePreset {
  id: string;
}

interface Automation {
  mode: AutomationMode;
}

interface UserSettings {
  presets: ThemePreset[];
  automation: Automation;
}

interface ExtensionData {
  settings: UserSettings;
}

export function getMockData(): ExtensionData {
  return {
    settings: {
      presets: [],
      automation: {
        mode: "",
      },
    } as UserSettings,
  }
}

TS playground

Originally posted by Mateusz Burzyński (@Andarist) in #53192 (comment)

Activity

  1. fatcerberus commented on Mar 21, 2023

    @fatcerberus

    Isn't it intentional that string literals are not assignable to string-based enums, even if they match one of the enum members?

  2. Andarist commented on Mar 21, 2023

    @Andarist
    Contributor

    It's about comparability and it seems that this type of conversion is valid in such a context, for example, this is OK:

    const foo = "" as AutomationMode
  3. fatcerberus commented on Mar 21, 2023

    @fatcerberus

    Ah, I missed the type assertion as UserSettings so it just looked like an assignability failure, which was expected.

  4. ahejlsberg commented on Mar 21, 2023

    @ahejlsberg
    Member

    The fix here is to exclude the comparable relation from the optimization in #53192. I will put up a PR.

  5. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions