1

In the TypeScript docs, it gives this example of a array interface:

interface Dictionary {
[index: string]: string;
}

I don't get this. I thought associative arrays such as myArray['First'] were not good practice.

Can someone explain this or give me an example of using this interface?

5
  • 1
    What makes you think such arrays are bad practice? One use case that comes to mind is localization - an associative array is a fine way to map a placeholder string to a translated string. Commented Feb 22, 2014 at 21:04
  • Because TsLint give me this when I tried it: object access via a string literal is disallowed. Commented Feb 22, 2014 at 21:06
  • 1
    That error talks about a string literal (using a predetermined and hardcoded string index such as 'First'), not a string-typed index in general. The string literal case can be problematic because not all tools understand it and they can skip over it when you use IDE features like "Find Usages"/"Find All References" or try to rename/refactor a property. Commented Feb 22, 2014 at 21:14
  • I see. So is there a better way to do this? Or just ignore the warning in this case? Commented Feb 22, 2014 at 21:22
  • You can disable that rule around the lines where you actually need to use this syntax. Commented Feb 22, 2014 at 21:24

1 Answer 1

1

Associative arrays are not a good idea if you use them as arrays. The associative array is great for implementing a hash map, or dictionary.

For more explanation of the dictionary use case check out this example : http://en.wikipedia.org/wiki/Associative_array#Example

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.