Skip to content

Readonly prevents indexer assignment in class constructor #17845

Closed
@axefrog

Description

@axefrog

TypeScript Version: 2.4.2

Code

class Environment {
  readonly [key: string]: Domain;

  constructor (domains: Domain[]) {
    for (let domain of domains) {
      this[domain.name] = domain;
    }
  }
}

Expected behavior:

I'm assigning the values in the constructor, so I would assume that the readonly modifier would not take effect yet.

Actual behavior:

Index signature in type 'Environment' only permits reading.
(property) Domain.name: string

Is this a bug or does TypeScript disallow assignment to readonly indexers due to implementation difficulties?

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