3

With typescript, apparently it is possible to have an Array() type item where you specify the index of that array as an ID. While I always assumed this was only possible in the Object() type. Example:

items: String[] = []
items["someid"] = new SomeClass()
console.log(items)

Returns:

items:[someid: SomeClass]

Although I like this idea, is it ok to use it like this?

1 Answer 1

1

Actually, array can only have numerical indexes. However remember that every array is an object as well. Thus, you are assigning 'someid" as a new property to the items object. Equivalent of

const items = []; //items.length is 0 items.someid = {}; //items.length is still zero

If you want key => value property then use a Map

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.