Is there any way in typescript to use objects as keys?


Is there any way in typescript to use objects as keys?



I have tow classes:


class A {
name: string;
age: number;

constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}

class B extends A {

}



Also i have an object where i want to use instances of these classes as keys:


const storage: Storage = {};



So it will look like this:


const a = new A('Neo', 25);
const b = new A('Smith', 31);
storage[a] = 'What are you trying to tell me? That I can dodge bullets?';
storage[b] = 'Never send a human to do a machine's job.'



And then i want to differ value by keys, like:


const keys = Object.keys(storage);
keys.forEach(key => {
if (key instanceof A) {
console.log('This is Neo');
} else if (key instanceof B) {
console.log('This is Smith');
}
})



How should looks like Storage interface, because in typescript


Storage



An index signature parameter type must be 'string' or 'number'





This use case looks like you'd be fine with just an array of pairs, like const storage: Array<[A, string]>, and storage.push([a, 'What are...']);. Why do you need to index into storage by an object? Can you show a use case where it matters?
– jcalz
Jul 2 at 12:53


const storage: Array<[A, string]>


storage.push([a, 'What are...']);


storage




1 Answer
1



Is there any way in typescript to use objects as keys?



No, not with object literals.



However, you can use a Map instead:



The Map object holds key-value pairs. Any value (both objects and primitive values) may be used as either a key or a value.


Map





How i can specify interface for Map that is should use as keys only instances of A and B class?
– Denis Yalomist
Jul 2 at 10:02


Map


A


B





You could subclass Map with your own implementation.
– str
Jul 2 at 10:04


Map





TypeScript’s Map takes two type parameters, so you can specify the types of your keys and values: new Map<K, V>()
– Aankhen
Jul 2 at 10:17


Map


new Map<K, V>()





Could you expand on "not with object literals" for the benefit of those who aren't familiar with the potential pitfalls of using Map? That is, since Map uses object identity for key equality, if you put a value into the map using an object as a key, you can only retrieve it using the same object... which can bite you if you ever deserialize or clone objects in your code.
– jcalz
Jul 2 at 13:02


Map


Map





@jcalz Not exactly sure what you mean. If you want to know more about Map, it is usually best to read the post on MDN and the linked specifications.
– str
Jul 3 at 16:23


Map






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages