You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
enumPort{HTTP=80}constgood={80: '😇',80: '😇',// Error, as expected: Duplicate identifier '80'.ts(2300)}constalsoGood={[80]: '😇',[80]: '😇',// Error, as expected: Duplicate identifier '80'.ts(2300)};constbad={[Port.HTTP]: '😩',[Port.HTTP]: '😩'// No error}
Output
"use strict";varPort;(function(Port){Port[Port["HTTP"]=80]="HTTP";})(Port||(Port={}));constgood={80: '😇',80: '😇',};constalsoGood={[80]: '😇',[80]: '😇',};constbad={[Port.HTTP]: '😩',[Port.HTTP]: '😩'// No error};
This made me waste a fair amount of time debugging. The bug was caused by one enum value overriding the other like this:
enumTextData{CSV,}enumBinaryData{MPM,STL}constdocuments={[TextData.CSV]: false,[BinaryData.MPM]: true,[BinaryData.STL]: false,}if(documents[TextData.CSV]){console.log('Oups! I didn’t expect this to be executed')}
The text was updated successfully, but these errors were encountered:
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
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
Output
Compiler Options
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:
The text was updated successfully, but these errors were encountered: