Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
DanielRosenwasser opened this issue Mar 20, 2023 · 4 comments · Fixed by #53419
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@DanielRosenwasser
Copy link
Member

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 @Andarist in #53192 (comment)

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Mar 20, 2023
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 5.1.0 milestone Mar 20, 2023
@fatcerberus
Copy link

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

@Andarist
Copy link
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

@fatcerberus
Copy link

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

@ahejlsberg
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants