Skip to content

Duplicated object key in const not detected when the key is a number preceded by - or + #38295

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

Closed
rigwild opened this issue May 1, 2020 · 1 comment · Fixed by #38607
Closed
Labels
Bug A bug in TypeScript
Milestone

Comments

@rigwild
Copy link

rigwild commented May 1, 2020

TypeScript Version: 3.9.0-dev.20200422

Search Terms:

  • Signed numbers
  • Object keys negative
  • readonly negative key
  • object key negative
  • object key duplicate
  • as const negative
  • object key operand
  • others...

Code

export const rotate = {
  "-45": "test",
  [-45]: "test" // Duplicate NOT detected
};

export const rotate2 = {
  "45": "test",
  [+45]: "test" // Duplicate NOT detected
};

export const rotate4 = {
  "45": "test",
  [45]: "test" // Duplicate detected
};

Expected behavior:
TypeScript should alert the user that there's a duplicated key.

Actual behavior:
Key duplication is not detected when the key is a number preceded by - or +.

Playground Link

Related Issues:
#37866 #38133 #37292

The bug seems to be there for a very long time (it has the same behavior on v2.4.1 on the playground).

@rigwild rigwild changed the title Objects as const containing negative numbers as key breaks Duplicated object key in const not detected when the key is a negative number May 1, 2020
@rigwild rigwild changed the title Duplicated object key in const not detected when the key is a negative number Duplicated object key in const not detected when the key is a number preceded by - or + May 1, 2020
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label May 8, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone May 8, 2020
@lissein
Copy link
Contributor

lissein commented May 9, 2020

After some investigations, I found that the expression between the square brackets is not evaluated but just used as text.

I would like to fix this issue but I don't really know where to put the stuff. The fix could be something similar to how typescript handles constant enum values (computeConstantValue in checker.ts).

This looks a little bit more complicated for such cases:

let b = 10

const a = {
  "46": 123,
  [b + 46]: 1234,
};

I think we can make it work by updating getPropertyNameForPropertyNameNode in utilities.ts but I'm not sure if it is the right place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
3 participants