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

Enum-based constants can be used as computed property names more than once #41326

Closed
karol-majewski opened this issue Oct 29, 2020 · 1 comment

Comments

@karol-majewski
Copy link

karol-majewski commented Oct 29, 2020

TypeScript Version: 4.0.3

Search Terms: enum, 2300, computed property name, duplicate identifier, duplicates, twice

Expected behavior:

Compile-time error. It shouldn't matter if the constant is provided inline or pulled from an enum — a duplicate is still a duplicate. Enum values are known in compile-time, so it's possible to reject duplicate values.

Actual behavior:

The Duplicate identifier '80'.ts(2300) error is suppressed when enums are used.

Related Issues:

#6864 looks similar, but that one is concerned with values within the enum that defines them. I'm talking about using enum values inside an object literal.

Code

enum Port {
  HTTP = 80
}

const good = {
  80: '😇',
  80: '😇', // Error, as expected: Duplicate identifier '80'.ts(2300)
}

const alsoGood = {
  [80]: '😇',
  [80]: '😇', // Error, as expected: Duplicate identifier '80'.ts(2300)
};

const bad = {
  [Port.HTTP]: '😩',
  [Port.HTTP]: '😩' // No error
}
Output
"use strict";
var Port;
(function (Port) {
    Port[Port["HTTP"] = 80] = "HTTP";
})(Port || (Port = {}));
const good = {
    80: '😇',
    80: '😇',
};
const alsoGood = {
    [80]: '😇',
    [80]: '😇',
};
const bad = {
    [Port.HTTP]: '😩',
    [Port.HTTP]: '😩' // No error
};
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

This made me waste a fair amount of time debugging. The bug was caused by one enum value overriding the other like this:

enum TextData {
  CSV,
}

enum BinaryData {
  MPM,
  STL
}

const documents = {
  [TextData.CSV]: false,
  [BinaryData.MPM]: true,
  [BinaryData.STL]: false,
}

if (documents[TextData.CSV]) {
  console.log('Oups! I didn’t expect this to be executed')
}
@karol-majewski karol-majewski changed the title Enum-based constants can be used as computed properties more than once Enum-based constants can be used as computed property names more than once Oct 29, 2020
@IllusionMH
Copy link
Contributor

Duplicate of #25758 and not specific to enums.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants