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

Object literals allow duplicate [Enum.member]: ... keys #49666

Closed
pushkine opened this issue Jun 24, 2022 · 2 comments Β· Fixed by #49848
Closed

Object literals allow duplicate [Enum.member]: ... keys #49666

pushkine opened this issue Jun 24, 2022 · 2 comments Β· Fixed by #49848
Labels
Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@pushkine
Copy link
Contributor

Bug Report

πŸ•— Version & Regression Information

ts stable & nightly

⏯ Playground Link

Playground Link

πŸ’» Code

const x = {
    [1]: 2,
    [1]: 2,
//  ^^^ ERROR: An object literal cannot have multiple properties with the same name. (1117)
};

enum Enum { foo = 1, }

const y = {
    [Enum.foo]: 2,
    [Enum.foo]: 2,
//  ^^^^^^^^^^ No Error
};

πŸ™ Actual behavior

Defining an object literal with duplicate keys throws an error for literal 1 but not for enum member Enum.foo

πŸ™‚ Expected behavior

Defining an object with duplicate keys from enum members throws an error

    [Enum.foo]: 2,
    [Enum.foo]: 2,
//  ^^^^^^^^^^ An object literal cannot have multiple properties with the same name
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Help Wanted You can do this Experience Enhancement Noncontroversial enhancements labels Jun 28, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jun 28, 2022
@Andrew-Martin1
Copy link

Andrew-Martin1 commented Oct 12, 2022

Note: This issue isn't specific to enums, it happens with string variables as well

const myString = "foo";

const y = {
    [myString]: 1,
    [myString]: 2,
//  ^^^^^^^^^^ No Error
};

Edit: it looks like this might be intentional as part of ECMAscript implementation https://stackoverflow.com/a/30617831

@nnguyen01
Copy link

nnguyen01 commented Jun 24, 2023

Is this released yet? Still seeing the error

const myString = "foo";

const y = {
    [myString]: 1,
    [myString]: 2,
//  ^^^^^^^^^^ No Error
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants