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

TS Unable to detect duplicate keys in object when using variables + computed properties #29239

Closed
joshburgess opened this issue Jan 2, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@joshburgess
Copy link

TypeScript Version:

3.3.0-dev.201xxxxx

Search Terms:

computed properties, duplicate keys

Code

// type error, as expected
const foo1 = {
  Key1: 1,
  Key1: 2
}

// ok, let's try with computed properties
const Key1 = 'Key1'
const Key2 = 'Key2'

// no type error? widened type to `string`, maybe?
const foo2 = {
  [Key1]: 1,
  [Key1]: 2
}

// type error when literals are used
const foo3 = {
  ['Key1']: 1,
  ['Key1']: 2
}

// now, let's try with enums
enum TestEnum {
  Key1 = 'Key1',
  Key2 = 'Key2'
}

// no type error? widened type to `string`, maybe?
const foo4 = {
  [TestEnum.Key1]: 1,
  [TestEnum.Key1]: 2
}

Expected behavior:

I found some weirdness around how typescript treats computed properties & duplicate keys. I would expect it to detect duplicate keys when passed variables with specific type literals, whether created via constants or enum members.

Actual behavior:

Duplicate keys are not detected when variables are used as computed properties. This is particularly a problem when trying to use enum members, because there is no alternative way to use them as keys in an object.

Playground Link:

https://bit.ly/2TrDznF

Related Issues:

Possibly these:

#26208

#18596

@ajafff
Copy link
Contributor

ajafff commented Jan 2, 2019

Duplicate of #25758

@joshburgess
Copy link
Author

Whoops. Sorry, @ajafff .

@weswigham weswigham added the Duplicate An existing issue was already created label Jan 3, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants