镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@pushkine

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

Activity

  1. Andrew-Martin1 commented on Oct 12, 2022

    @Andrew-Martin1

    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

  2. nnguyen01 commented on Jun 24, 2023

    @nnguyen01

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions