Skip to content

No TSC error for duplicate dynamic keys with static readonly values #26318

Closed
@tompere

Description

@tompere

TypeScript Version: 3.1.0-dev.20180809

Search Terms: dynamic key duplicate identifier

Code

const X = 'x'

enum Y {
  ONE = 'one',
  TWO = 'one'
}

// static key - 'duplicate identifier' error

const obj1 = {
  'y': 'wham',
  'y': 'bam' // as expected, tsc error `Duplicate identifier ''y''`
}

const obj2 = {
  y: 'wham',
  y: 'bam' // as expected, tsc error `Duplicate identifier 'y'`
}

// [the bug] dynamic key - no 'duplicate identifier' error

const obj3 = {
  [X]: 'wham',
  [X]: 'bam' // no tsc error!
}

const obj4 = {
  [Y.ONE]: 'wham',
  [Y.TWO]: 'bam' // no tsc error!
}

Expected behavior:
When assigning duplicate dynamic keys to plain objects, which can be statically inferred (e.g. enums or const with primitive type) - tsc should show a compilation error.

Actual behavior:
no compilation error.

Playground Link:
a long long long playground link ...

Related Issues: N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions