Skip to content

Suggestion: Allow arbitrary property access when with string indexer #560

Closed

Description

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions