question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Readonly indexer and constructor usage: question

See original GitHub issue
class A {
    readonly x;
    readonly [x: string]: string;

    constructor() {
        this.x = 5;  // that's okay
        this["a"] = "s"; // that yields an error
    }
}

Is that by design that readonly indexers for the same type in constructor yield errors?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:1
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
sliktscommented, Jul 19, 2018

My use case is constructing a frozen array-like:

class FrozenArraylike<A> {
  readonly length: number
  readonly [i: number]: A

  constructor(iterable: Iterable<A>) {
    let i = 0
    forEach(iterable[Symbol.iterator](), (value: A) => {
      this[i] = value
      i += 1
    })
    this.length = i
    Object.freeze(this)
  }
}

this[i] = value obviously doesn’t work with the current rules, and I don’t understand the reasoning:

An index signature introduces no new properties and the constructor rule applies to properties.

An index signature defines unspecific extra properties, and the constructor is the place to set them if they’re readonly, because the object is created in the constructor so it’s local mutation.

1reaction
sliktscommented, Aug 16, 2019

I’d do this before the assignment:

// @ts-ignore: https://github.com/microsoft/TypeScript/issues/6781

The link being there to document why it’s done.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I make a read-only indexer in C#? - Stack Overflow
A read-only Indexer can be achieved by not including a set property in the declaration of the Indexer. To modify the Microsoft example....
Read more >
Declare and Initialize Read-only Auto Properties in C# ...
You have initialized the class with the help of the constructor, and the fields were changed with the help of the property accessors....
Read more >
Expression-Bodied Members in C# - GeeksforGeeks
The following example defines a Boolean method called IsEven() that ... The indexer is read-only, therefore the languages in the array ...
Read more >
Expression-bodied members - C# Programming Guide
Method; Read-only property; Property; Constructor; Finalizer; Indexer. Methods. An expression-bodied method consists of a single expression that ...
Read more >
Solved NOTE: I need full code in C# with outputs for - Chegg
Define class Student: (1) static and readonly field: (2) non-static/instance field: (3) Constructors Γ nverloading 1 \#\# for the above indexers, we need...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found