-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Suggestion: Allow arbitrary property access when with string indexer #560
Comments
The proposal you've made makes your Dictionary type equivalent to |
I think that's how JS dictionaries, or 'string maps' work, but I agree that this can be fairly undesirable when there are many other methods, as in BTW, I think this should not exactly be equivalent to var num: number = el.dataset; // invalid
el.dataset = 4; // invalid |
We've gotten some feedback like this already from people who are e.g. using Angular scopes with no additional type information. The basic idea is that you should be able to have a type where normal property lookup and assignment rules are in place, but arbitrary property access using dot-notation instead of index-notation is also allowed. We would not change the behavior of existing code in terms of how objects with string indexers behave, so some other proposal along with strong motivating examples would be useful. |
We need something like [index:property] :Something . This will tell the compiler to map any property name and this property will be instance of Something. We know which is the object that carries the property and what is after the property's dot. but we don't know what the name of this property is, so we don't have code assistance. |
Just bumped into this myself... interface NumberDictionary {
[key: string]: number;
}
var dict: NumberDictionary = {};
dict['work']; // Works
dict.work; // Doesn't work
var { work } = dict; // Also works destructuring compiles to direct property lookup so for consistency it probably should work the same. |
I think it will be helpful to close this and add a link to the more recent #12596 |
Resulting JavaScript is valid but TypeScript blocks this.
Allowing this would allow general JavaScript dictionary use cases.
Example: from MDN.
el.dataset.id
is generally preferred toel.dataset['id']
.The text was updated successfully, but these errors were encountered: