Skip to content
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

Closed
saschanaz opened this issue Aug 28, 2014 · 6 comments
Closed
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@saschanaz
Copy link
Contributor

interface Dictionary {
    [key: string]: any;
}
var dict: Dictionary = {};
dict['work']; // Works
dict.work; // Doesn't work

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 to el.dataset['id'].

<div id="user" data-id="1234567890" data-user="johndoe" data-date-of-birth>John Doe
</div>
var el = document.querySelector('#user');

// el.id == 'user'
// el.dataset.id === '1234567890'
// el.dataset.user === 'johndoe'
// el.dataset.dateOfBirth === ''

el.dataset.dateOfBirth = '1960-10-03'; // set the DOB.

// 'someDataAttr' in el.dataset === false

el.dataset.someDataAttr = 'mydata';
// 'someDataAttr' in el.dataset === true
@danquirk
Copy link
Member

The proposal you've made makes your Dictionary type equivalent to any, that seems pretty undesirable. Any type with a string indexer is now going to do property lookup as if it were an any?

@saschanaz
Copy link
Contributor Author

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 HTMLFormElement. Typos will be also allowed then, while the IDE autocomplete feature will try to prevent them.

BTW, I think this should not exactly be equivalent to any.

var num: number = el.dataset; // invalid
el.dataset = 4; // invalid

@RyanCavanaugh
Copy link
Member

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.

@ghost
Copy link

ghost commented Sep 25, 2015

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.

@mhegazy mhegazy added Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. and removed Needs More Info The issue still hasn't been fully clarified labels Feb 20, 2016
@tikotzky
Copy link

Just bumped into this myself...
What I noticed is that destructuring works but direct property lookup doesn't.
extending the example above...

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.

@saschanaz
Copy link
Contributor Author

I think it will be helpful to close this and add a link to the more recent #12596

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants