-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Readonly indexer and constructor usage: question #6781
Comments
I believe indexed assignments should be allowed in the constructor |
It's also interesting how it should behave in case of inheritance. class A {
readonly x;
readonly [x: string]: string;
}
class B {
constructor() {
super();
this.x = 5; // that's an error
this["a"] = "s"; // should this be an error, too?
}
} |
I don't think this is a bug. An index signature introduces no new properties and the constructor rule applies to properties. We could consider extending the rule to index signatures, but I'm not sure that's particularly useful. Is there are scenario that doesn't work with the existing rules? Note that the following does work because class A {
readonly x: string;
readonly [x: string]: string;
constructor() {
this.x = "s"; // Ok
this["x"] = "s"; // Ok, same as this.x
this["a"] = "s"; // Error, no declaration of a in this class
}
} |
It seems like any case for disallowing this also applies to non- Now we've added |
@zhuravlikjb can you share a repro with more real-world names (maybe outline the broad usage of the class)? We talked about this for a while and couldn't agree on what the developer intent of a read-only index signature on a class is. Understanding how you're using this would be helpful. |
@RyanCavanaugh Unfortunately, I develop a tool for TypeScript support, but written not in TypeScript, so this example is not a real-world thing, but just a test case, a theoretical question. If there are no real-user requests for this, you can freely close the issue. |
@zhuravlikjb thanks for the info. I'll leave this one awaiting feedback then. |
I just ran into this problem when I implemented a dictionary like class, the uscase is a table with items which contain an indexed value for each table column.
A workaraound-solution is to use the object.assign function in the constructor, but I think it would be better to have a consistent behavior of the compiler to allow indexed value assignments in the constructor. This stackoverflow discussion is linked to this issue: |
My use case is that I implement a lot of immutable structures. I don't allow imperative mutation of key-value pairs, and instead construct new instances of things with the desired changes. This means that read-only indexers are a must, but that the set of keys being assigned is only known at runtime, and established within the constructor. Certainly I can work around this by having my properties be held internally and accessed indirectly via a readonly indexer, but then I'm changing the runtime characteristics of my implementation not because it's best for the application's architecture, but because TypeScript won't play ball. In this case my workaround involves casting to |
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)
}
}
An index signature defines unspecific extra properties, and the constructor is the place to set them if they're |
Is there a workaround for this for numeric indices, other than using |
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. |
Like @slikts, I also need this for a frozen array-like class. It was very surprising to me that this doesn't work. |
Is that by design that readonly indexers for the same type in constructor yield errors?
The text was updated successfully, but these errors were encountered: